opencv 去除玻璃蒙版,使用OpenCV从轮廓获取蒙版

在尝试从轮廓创建图像掩模时,遇到图像目标区域未正确填充的问题。通过`cv.findContours`获取了轮廓,然后用`cv.drawContours`绘制到一个全零掩模上,但结果始终为空。问题出在使用了RGB颜色值(0, 255, 0),而掩模是单通道的,因此应该使用单色值(255)或(255, 255, 255)。修改`drawContours`的参数后,成功生成了图像掩模。
摘要由CSDN通过智能技术生成

I would like to get an image mask from the contour (it exists only 1 contour) I have computed thanks to cv.findContours.

However, while my contour variable is not empty, I do not manage to retrieve an image mask using cv.drawContours, my destination image being always empty.

Here is my code:

img = mosaicImage[:,:,0].astype('uint8')

contours, _ = cv.findContours(img.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

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

cv.drawContours(mask, contours, -1, (0,255,0),1)

I hope you could help!

Thanks

解决方案

you are setting color (0,255,0) to the mask, but the mask is single channel so you draw the contour in color 0.

try

cv.drawContours(mask, contours, -1, (255),1)

or

cv.drawContours(mask, contours, -1, (255,255,255),1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值