简体   繁体   English

找不到精确的轮廓检测(opencv,python)

[英]Not able to find exact contour detection (opencv, python)

I am working on this image: 我正在处理此图像:

I used the following code: 我使用以下代码:

input_img = cv2.imread(input_image)
img = cv2.imread(input_image, 0)

kernel = np.ones((5,5),np.uint8)
# morphological_img = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
# # morphological_img = cv2.threshold(morphological_img, morphological_img, 128, 255, cv2.THRESH_BINARY_INV)
# # img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

# morphological_img = cv2.medianBlur(morphological_img, 5)


canny_img = cv2.Canny(input_img, 100, 200)
_, contours, hierarchy = cv2.findContours(canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # get contours

and got these contours: 得到了这些轮廓:

I have tried all these features like blurring, thresholding, etc. but I'm not getting the contours I am expecting. 我已经尝试了所有这些功能,例如模糊,阈值等,但是我没有得到期望的轮廓。 I need to find the black contour and a violet contour like containers but what I am getting contours around the text instead of their background container. 我需要找到类似容器的黑色轮廓和紫色轮廓,但是我正在围绕文本而不是背景容器获得轮廓。

Sorry for my bad english. 对不起,我的英语不好。 If you need anything else, please ask. 如果您还有其他需要,请询问。

I am answering my own question after researching a lot thinking that it can help someone who is stuck at same problem. 经过大量研究后,我在回答自己的问题,认为它可以帮助遇到同样问题的人。

As the question is here...we are not able to find background containers but with all the present techniques(like thresholding, blurring etc)...I was not able to get. 由于问题在这里...我们无法找到背景容器,但是使用所有现有技术(例如阈值处理,模糊处理等)...我无法获得。

So the approach is to add borders(size=10 or more will work) and choose the colour which is not present in the image will give you all the contours you need. 因此,方法是添加边框(尺寸等于或大于10可以使用),然后选择图像中不存在的颜色将为您提供所需的所有轮廓。

bordersize = 10
img = cv2.copyMakeBorder(img, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[247, 248, 188] )

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

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