【发布时间】:2017-11-19 23:14:11
【问题描述】:
我有两张二值图像,我正在尝试检测其中白色斑块的轮廓(拼贴右侧的粉红色轮廓是轮廓结果)。
cv2.contourFind() 适用于 Contour1:
但对于 Contour2,它的行为很奇怪:
这是它的函数调用
#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 看起来非常相似时却失败了。
【问题讨论】:
-
请编辑您的问题以提供minimal, complete, and verifiable example。
-
完成,@AlexanderReynolds
-
每张图片在
contours中有多少个轮廓? -
Contour1 为 102,Contour2 为 1
-
查看每一步的输出图像(转换颜色、阈值化、膨胀),并在找到轮廓之前查看图像发生了什么。看看这是否表明存在任何问题。
标签: python opencv image-processing vision opencv-contour