get mask from contour with OpenCV

痞子三分冷 提交于 2020-05-15 02:07:16

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/32401806/get-mask-from-contour-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!