当我使用rever_external时,为什么我仍然可以获得孩子边界框?

发布于 2025-01-26 15:42:03 字数 1294 浏览 2 评论 0原文

我正在努力在相对简单的图像上正确获取轮廓。 我正在使用rever_external,因此,如果我的理解很好,此设置应该忽略嵌套在父轮廓内的轮廓,但我仍然会得到孩子轮廓。

它们在最后一个数字(8)中非常明显,在第一个数字(左上角)中不太明显。

那我在这里想念什么?还是有更好的方法只能获取父界框? 在下面略有简化的脚本,主要是为了显示问题。

img_tmp = sample.copy()
img_rgb = cv2.cvtColor(sample, cv2.COLOR_GRAY2RGB) # Original to RGB to distinguish bounding boxes

print('original image\n')
showImage(img_tmp,8,cmap=cm.gray)

# Threshold and get contours
img_tmp = cv2.threshold(img_tmp, 230, 255,cv2.THRESH_BINARY)[1]
edged = cv2.Canny(img_tmp, 100, 250) #low_threshold, high_threshold
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sort_contours(cnts, method="left-to-right")[0]

print('\nthreshed and edged image\n')
showImage(edged,8,cmap=cm.gray)
 
for c in cnts:
    # compute the bounding box of the contour and isolate ROI
    (x, y, w, h) = cv2.boundingRect(c)
    roi = img_tmp[y:y + h, x:x + w]
    # append to rgb original
    cv2.rectangle(img_rgb, (x, y), (x + w, y + h), (0,255 , 0), 2)

print('\nOuter boxes on original, but also inner...\n')
showImage(img_rgb,8) 

I'm struggling to get my contours right on a relative simple image.
I'm using RETR_EXTERNAL so if my understanding is well this setting should ignore any contour that's nested inside the parent contours, yet I still get child contours.

They are very noticeable in the last digit (the 8) and less noticeable in the first digit (upper left corner).

So what am I missing here? Or are there better ways to only get the parent bounding box?
Below slightly simplified script, mainly to show the problem.

img_tmp = sample.copy()
img_rgb = cv2.cvtColor(sample, cv2.COLOR_GRAY2RGB) # Original to RGB to distinguish bounding boxes

print('original image\n')
showImage(img_tmp,8,cmap=cm.gray)

# Threshold and get contours
img_tmp = cv2.threshold(img_tmp, 230, 255,cv2.THRESH_BINARY)[1]
edged = cv2.Canny(img_tmp, 100, 250) #low_threshold, high_threshold
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sort_contours(cnts, method="left-to-right")[0]

print('\nthreshed and edged image\n')
showImage(edged,8,cmap=cm.gray)
 
for c in cnts:
    # compute the bounding box of the contour and isolate ROI
    (x, y, w, h) = cv2.boundingRect(c)
    roi = img_tmp[y:y + h, x:x + w]
    # append to rgb original
    cv2.rectangle(img_rgb, (x, y), (x + w, y + h), (0,255 , 0), 2)

print('\nOuter boxes on original, but also inner...\n')
showImage(img_rgb,8) 

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2025-02-02 15:42:03

在您的情况下,ret_external正在正确执行预期。

cv_retr_external仅检索极端的外观。

实际上,它忽略了它检测到的内部轮廓,但是您要忽略的轮廓属于另一个段。我的意思是新轮廓,因此rever_external与它们无关。

您需要使用的是ret_tree

ret_tree检索所有轮廓,并重建一个完整的层次结构
嵌套轮廓。

借助此,您将能够学习所有轮廓层次结构。 在这里是一个很好的例子。

In your case, RETR_EXTERNAL is doing correctly what it is expected.

CV_RETR_EXTERNAL retrieves only the extreme outer contours.

Actually, its is ignoring the inner contours which it has detected but the contours you want to ignore belong to a different segment. I mean new contours so RETR_EXTERNAL has nothing to do with them.

What you need to use is that RETR_TREE:

RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of
nested contours.

With the help of this, you will be able to learn all of the contour hierarchy. Here is a well explained example of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文