[英]unable to get contour properly
我正在玩 openCV 並執行以下簡單任務。
1) 讀取圖像
2) 閾值化
3)尋找輪廓。
4)在空白圖像中繪制所有輪廓。
5) 繪制個人輪廓。
在虛擬圖像上繪制所有輪廓看起來不錯,而繪制單個輪廓會產生分散的輪廓,如下圖所示。
原來的:
所有輪廓:
單一輪廓:
請在下面找到代碼。
import cv2
import numpy as np
#Reading Image.
srcImg = cv2.imread("./bottle.jpeg")
#Color Conversion.
grayedImg = cv2.cvtColor(srcImg,cv2.COLOR_RGB2GRAY)
__, thresholdedImg = cv2.threshold(grayedImg, 240, 255, cv2.THRESH_BINARY)
#Noice Removal
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
erodeImage = cv2.erode(thresholdedImg,kernel, iterations=1)
dilatedImg = cv2.dilate(erodeImage,kernel, iterations=1)
_, contours, _ = cv2.findContours(dilatedImg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#draw All Contours.
dummyImg = np.zeros(grayedImg.shape, dtype=grayedImg.dtype)
cv2.drawContours(dummyImg, contours, -1, 255, 1)
cv2.imshow("All Contours", dummyImg)
cv2.imwrite("allContours.jpeg",dummyImg)
#draw Individual Contours.
mask = np.zeros(dummyImg.shape[:2], dtype= dummyImg.dtype)
isolatedImg = cv2.drawContours(mask, contours[9], -1, 255, 1)
cv2.imshow("Indivial Contours.", isolatedImg)
cv2.imwrite("single.jpeg",isolatedImg)
cv2.waitKey(0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.