簡體   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