neo*_*n29 6 python opencv mask contour
由于 cv.findContours,我想从轮廓中获取图像蒙版(它只存在 1 个轮廓)。
但是,虽然我的轮廓变量不为空,但我无法使用 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)
Run Code Online (Sandbox Code Playgroud)
我希望你能帮忙!
谢谢
您正在为遮罩设置颜色 (0,255,0),但遮罩是单通道,因此您以颜色 0 绘制轮廓。
尝试
cv.drawContours(mask, contours, -1, (255),1)
Run Code Online (Sandbox Code Playgroud)
或者
cv.drawContours(mask, contours, -1, (255,255,255),1)
Run Code Online (Sandbox Code Playgroud)