我有两个二进制图像,我试图检测它们中的白色斑块的轮廓(右侧拼贴画中粉色轮廓是轮廓结果)。
对于Contour1,
但对于Contour2,它表现得很奇怪:
这是它的函数调用:
对于Contour1,
cv2.contourFind()
工作得很好:
![Contour1 Image & Result](https://istack.dev59.com/aVWLO.webp)
![Contour2 Image & Result](https://istack.dev59.com/Fqake.webp)
#Convert Image to grayscale
img = cv2.imread(file_name)
img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilated = cv2.dilate(mask, kernel, iterations=2)
image, contours, hierarchy = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
[x, y, w, h] = cv2.boundingRect(contour)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
使用变量contours
,我在找到的点周围画出矩形。我不明白为什么对于Contour1它有效,但对于看起来非常相似的Contour2却失败了。
contours
有多少轮廓? - alkasm