简体   繁体   English

在最终图像上应用轮廓遮罩

[英]Applying contoured mask on final image

While trying to segment a plant from its background, I came across a problem After creating a mask by hue value and using the close and open operator on it, I get into the following situation: 尝试从背景中分割植物时,遇到一个问题。在通过色相值创建遮罩并在其上使用close和open运算符后,遇到以下情况: 遮罩和原始图像

After this I wanted to remove the small bits in edges of the image, I did this by the following operation: 此后,我想去除图像边缘的小部分,我通过以下操作进行了此操作:

_, 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)

When this is applied, the specks have been removed, but when applied by: 应用此选项时,斑点已被删除,但通过以下方式应用:

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

I get the following result: 我得到以下结果:

不工作

As can be seen, the mask is not correctly applied on the image, does anybody know why this is happening? 可以看出,蒙版没有正确地应用到图像上,有人知道为什么会这样吗?

Instead of using mask_final in cv2.drawContours function, create a new mask of same shape as original image and do something like below: 不要在cv2.drawContours函数中使用mask_finalcv2.drawContours创建一个与原始图像形状相同的新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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM