【问题标题】:numpy.maximum.reduce not returning maximum valuenumpy.maximum.reduce 不返回最大值
【发布时间】:2020-03-08 18:36:18
【问题描述】:

您好,我正在尝试查找已使用 numpy.maximum.reduce 的二进制掩码的最大值。我有一组二进制掩码,以便从所有掩码中找到最大值(根据我的说法,它指向图像中的最多边缘)并避免重叠。因此我使用numpy.maximum.reduce 来找到所有二进制像素的最大值。但结果并不如预期。它没有将白色区域显示为最大值,并将灰色区域作为最终输出掩码(最大值)。为了得到面具,我对边缘检测到的图像进行了膨胀和模糊。那么这会影响最终结果吗(意味着比较掩码以找到最多的边缘)?

Q1.找到最大值并避免重叠区域是否正确?

Q2.它会返回边缘最多的区域吗?

Q3.我应该使用蒙版还是原始边缘检测图像来获得最多边缘?

Results of combining masks Individual Masked Images

for file in glob.glob("images/*.jpg")):
        img = cv2.imread(file)  


        #edge detection
        canny = auto_canny(img)


        #Dilation(Morphological function to increase edge width)
        img_dilate = cv2.dilate(canny, (3,3), iterations = 1)


        #Gaussian Blur to blur the edges to remove noise
        mask= ndimage.gaussian_filter(img_dilate, sigma=5)
        mask[mask< 30] = 0
        mask[(mask>= 30) & (mask< 70)] = 30
        mask[(mask>= 70) & (mask< 110)] = 110
        mask[mask> 110] = 255



#       Retrieve regions from original images
        res = cv2.bitwise_and(img, img, mask = mask)
        mask_inv= cv2.bitwise_not(mask)


        #adding the extracted regions to background image
        main_ = cv2.bitwise_and(im_1, im_1, mask = mask_inv)
        result = cv2.add(main_, res)

        cv2.imshow("result",mask)
        cv2.waitKey(0)

        image_1.append(mask) #list of masks
        image_2.append(res) #list of all extracted regions


for i in range(0,len(image_1)):
    max_ = np.maximum.reduce([image_1[i]])
cv2.imshow("max",max_)
cv2.waitKey(0)

更新

max_img = np.zeros((image_1[0].shape[:3]),dtype=np.uint8)
max_img = np.maximum(max_img,image_1)
cv2.imshow("max",max_img)
cv2.waitKey(0)

使用此更新部分,我遇到了error: (-206) Unrecognized or unsupported array type in function cvGetMat

【问题讨论】:

  • np.maximum() 需要 2 个数组。你只用一个。见docs.scipy.org/doc/numpy/reference/generated/numpy.maximum.html。如果您想要任何给定数组的最大值,请使用 np.max()。它只使用一个数组并找到数组中的最大值。如果您尝试计算两个图像的逐像素最大值,请使用 np.maximim()
  • @fmw42 我尝试使用 np.maximum() 但它返回错误:参数数量无效
  • @fmw42 在我使用np.maximum.reduce 的代码中,我认为它只是将image_3 中最后一个列表项的结果返回给我。我想要的是比较所有列表项并返回所有图像的最大像素
  • 我不是 .reduce 方面的专家。但我知道 np.maximum 一次比较两个图像。所以我相信你需要做如下比较。创建一个相同大小的零图像。然后使用前一个最大值的结果获得每个图像之间的最大值。即 maximage = np.maximum(maximage, image_1),其中第一个 maximage 是零填充图像。
  • @fmw42 我使用 np.maximum() 进行了尝试,得到了error: (-206) Unrecognized or unsupported array type in function cvGetMat。我会更新我是如何尝试的

标签: python numpy opencv image-processing computer-vision


【解决方案1】:

这是一个使用 Python/OpenCV/Numpy 逐像素获取 4 个二进制掩码图像的最大值的简单示例

4 个面具:

import cv2
import numpy as np

# read masks
mask1 = cv2.imread('mask1.png')
mask2 = cv2.imread('mask2.png')
mask3 = cv2.imread('mask3.png')
mask4 = cv2.imread('mask4.png')

# check shape so that image of zeros (below) is created compatible
print(mask1.shape)

# make list
masks = [mask1, mask2, mask3, mask4]

# initialize maximum array to zeros
image_max = np.full((mask1.shape), (0,0,0), dtype=np.uint8)

# get maximum pair-wise from mask and previous maximum image
for mask in masks:
    image_max = np.maximum(image_max, mask)

# show results
cv2.imshow('maximum', image_max)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('mask1-4max.png', image_max)


【讨论】:

  • 我可以推荐np.zeros(mask1.shape, dtype=np.uint8)甚至np.zeros_like(mask1)
  • @alkasm 感谢您的建议。 “给猫剥皮”的方法很多。但我乐于学习不同的方法,尤其是那些更高效或优雅的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多