简体   繁体   English

图像python opencv中的轮廓检测有缺陷

[英]Flawed contour detection in an image python opencv

I would like to detect the contour of the giant black blob in the following image: 我想在下图中检测到巨大的黑色斑点的轮廓:

在此处输入图片说明

So I used Canny edge detection to find the edges using the following code: 因此,我使用Canny边缘检测通过以下代码查找边缘:

edged = cv2.Canny(image, 30, 200)

在此处输入图片说明

Then, when I try to find the contour, it gives me only half on the blob. 然后,当我尝试找到轮廓时,它只给我一半的斑点。

在此处输入图片说明

This is the code I used to find the contour: 这是我用来查找轮廓的代码:

# find contours in the edged image, keep only the largest
# ones, and initialize our screen contour
(cnts, _) = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

# Scan the contours for largest item
array_sizes = []

for numpoints in cnts:
    array_sizes.append(len(numpoints))

largest = max(array_sizes)
second = second_largest(array_sizes)
index_of_object = array_sizes.index(largest)

# Largest detected contour
screenCnt = cnts[index_of_object] 

Is there any alteration I can make in the original image to get a full and more accurate detection of the large black blob? 我可以对原始图像进行任何更改,以便对大的黑色斑点进行全面,更准确的检测吗? All help is appreciated. 感谢所有帮助。

The problem is in your workflow. 问题出在您的工作流程中。

Remove the Canny operator from your processing. 从您的处理中删除Canny运算符。 It will give you an edge image from your blobs. 它将为您提供来自斑点的边缘图像。 Once you process the Canny image with contourFinder it will treat the edge segments as objects. 一旦使用ContourFinder处理Canny图像,它将把边缘段视为对象。 Your largest contour will be of the longest edge segment. 您最大的轮廓将是最长的边缘段。 If you zoom into your Canny image you'll see that there are gaps where your current result ends. 如果放大您的Canny图像,您会发现当前结果结束时存在间隙。

To get the contour of the blob skip Canny. 要获取斑点的轮廓,请跳过Canny。 Maybe you have to invert the image if findContours treats white as foreground. 如果findContours将白色视为前景,则可能必须反转图像。

Please get some knowledge and understanding of those methods so you won't run into such traps again. 请获得对这些方法的一些知识和理解,以免再次遇到此类陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM