【问题标题】:Applying contoured mask on final image在最终图像上应用轮廓蒙版
【发布时间】:2019-11-29 18:26:15
【问题描述】:

在尝试从背景中分割植物时,我遇到了一个问题 通过色调值创建蒙版并在其上使用关闭和打开运算符后,我进入以下情况:

之后我想去除图像边缘的小块,我通过以下操作做到了这一点:

_, cont, heir = cv2.findContours(mask_final, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contour_sizes = [cv2.contourArea(contour) for contour in cont]
for con, size in zip(cont, contour_sizes):
    if size > 5000:
        mask_final = cv2.drawContours(mask_final, [con], -1, (255, 255, 255), cv2.FILLED)

应用时,斑点已被移除,但应用时:

final = cv2.bitwise_and(img_rgb,img_rgb, mask = mask_final)

我得到以下结果:

可以看出,蒙版没有正确应用到图像上,有人知道为什么会这样吗?

【问题讨论】:

  • 在涂抹遮罩之前清洁遮罩上的斑点,而不是清洁图像上的斑点。

标签: python numpy opencv computer-vision mask


【解决方案1】:

不要在cv2.drawContours函数中使用mask_final,而是创建一个与原始图像相同形状的新mask并执行如下操作:

mask = np.zeros(img.shape, np.uint8)

for con, size in zip(contours, contour_sizes):
    if size > 5000:
        mask = cv2.drawContours(mask, [con], -1, (255, 255, 255), cv2.FILLED)

final = cv2.bitwise_and(img_rgb, img_rgb, mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY))

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多