Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cv2.findContours无法检测轮廓_Python_Opencv_Image Processing_Vision_Opencv Contour - Fatal编程技术网

Python cv2.findContours无法检测轮廓

Python cv2.findContours无法检测轮廓,python,opencv,image-processing,vision,opencv-contour,Python,Opencv,Image Processing,Vision,Opencv Contour,我有两张二值图像,我试图检测其中白色斑块的轮廓(拼贴右侧的粉红色轮廓就是轮廓结果) 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,

我有两张二值图像,我试图检测其中白色斑块的轮廓(拼贴右侧的粉红色轮廓就是轮廓结果)

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)
使用此
轮廓
变量,我在找到的点周围绘制矩形。
我不明白为什么它对Contour1有效,但当它们看起来非常相似时,对Contour2无效。

错误:二值图像在Contour2中有一个薄的白色边框框,但在Contour1中没有(我的错!)。因为我只要求外部轮廓,
cv2.RETR\u external
in

image, contours, hierarchy = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
因为Contour2只检测到了最外层的盒子,因此它的孩子们都没有被画出来。但在Contour1中,二值图像周围没有白色边框,因此检测到内部白色斑点


解决方案:使用
cv2.RETR\u列表
cv2.RETR\u CCOMP

请编辑您的问题以提供一个。完成,@Alexanderreynold显示每个图像的
轮廓中有许多轮廓
?轮廓1为102,轮廓2为1查看每个步骤的输出图像(转换颜色、阈值、放大)在找到轮廓之前,先看看图像发生了什么。看看这是否显示出任何问题。