代码之家  ›  专栏  ›  技术社区  ›  user121

如何在图像上叠加掩模,如何保留掩模值对应的二维点?

  •  0
  • user121  · 技术社区  · 7 年前

    我对python中的图像处理是新手,希望能就两个问题提供一些建议。

    我有个形象: image 以及它的面具: enter image description here

    import cv2
    import matplotlib.pyplot as plt
    
    mask = cv2.imread('img_mask.jpg')
    img = cv2.imread('img.jpg')
    

    1) 我有以下(x,y)像素位置:

    pt1 = 43.35, 22.49
    pt2 = 49.035, 46.985
    pt3 = 18.326, 21.822
    

    在遮罩上,在 pt1 pt2 0 pt3 16 . 给定三个(x,y)像素位置作为列表,以及提供的掩码。如何有效地筛选值为的位置 0 在面具上?

    thresholded masked image ,然后将其覆盖在原始图像上,这样 thresholded mask image 是从值为16的原始掩模中的像素位置获得的掩模仅具有值为16的图像。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Vardan Agarwal    7 年前

    第一部分我不知道为什么你的点不是整数。我下载了问题中的掩码,这个代码可以用来打印像素值为0的对。我以灰度格式读取掩码。

    img = cv2.imread('mask.jpg',0)
    for i in range(0,img.shape[0]):
        for j in range(0,img.shape[1]):
            if img[i,j] == 0:
                print(i,j)
    

    对于第二部分正常阈值可以使用。有关详细信息,请参阅opencv文档 thresholding

    zoom = cv2.resize(img, None, fx = 4, fy = 4, interpolation = cv2.INTER_CUBIC)
    ret,thresh1 = cv2.threshold(zoom, 16, 255, cv2.THRESH_BINARY)
    ret,thresh2 = cv2.threshold(zoom, 17, 255, cv2.THRESH_BINARY)
    output = cv2.bitwise_xor(thresh1, thresh2)
    cv2.imshow('threshold with 16', thresh1)
    cv2.imshow('threshold with more than 16', thresh2)
    cv2.imshow('result', output)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    mask with threshold as 16

    mask with threshold as 17

    阈值2

    mask with only pixels=16

    输出