当我使用rever_external时,为什么我仍然可以获得孩子边界框?
我正在努力在相对简单的图像上正确获取轮廓。 我正在使用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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,
ret_external
正在正确执行预期。实际上,它忽略了它检测到的内部轮廓,但是您要忽略的轮廓属于另一个段。我的意思是新轮廓,因此
rever_external
与它们无关。您需要使用的是
ret_tree
:借助此,您将能够学习所有轮廓层次结构。 在这里是一个很好的例子。
In your case,
RETR_EXTERNAL
is doing correctly what it is expected.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
:With the help of this, you will be able to learn all of the contour hierarchy. Here is a well explained example of it.