繁体   English   中英

使用OpenCV从轮廓中获取掩码

[英]get mask from contour with OpenCV

我想从轮廓中获得一个图像蒙版(它只存在1个轮廓)我已经计算了cv.findContours。

但是,虽然我的轮廓变量不为空,但我无法使用cv.drawContours检索图像蒙版,我的目标图像始终为空。

这是我的代码:

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)

我希望你能帮忙!

谢谢

您正在为蒙版设置颜色(0,255,0),但蒙版是单个通道,因此您可以绘制颜色为0的轮廓。

尝试

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

要么

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

暂无
暂无

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

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