简体   繁体   English

如何将 cv2 矩形边界框合并为多边形? (不是通过重叠/阈值)

[英]How can I merge cv2 rectangle bounding boxes into polygons? (Not by overlap/threshold)

I have multiple rectangle bounding boxes that I know are part of the same object (parts of a newspaper article), as in the first image.我有多个矩形边界框,我知道它们是同一对象的一部分(报纸文章的一部分),如第一张图片所示。 I'm trying to work out a way to merge these into one polygon bounding box, for the whole article, as in the second image.对于整篇文章,我正在尝试找出一种方法将它们合并到一个多边形边界框中,如第二张图片所示。

I have seen lots of solutions based on merging overlapping bounding boxes, but I don't care if they're overlapping or not - I already know they're part of the same article.我已经看到很多基于合并重叠边界框的解决方案,但我不在乎它们是否重叠 - 我已经知道它们是同一篇文章的一部分。 In some cases the headline is quite far away (for example above a picture), so solutions based on padding don't work either.在某些情况下,标题很远(例如在图片上方),因此基于填充的解决方案也不起作用。

I feel like there should be a cv2 function that does this, but if there is, I am missing it.我觉得应该有一个cv2函数可以做到这一点,但如果有,我就错过了。 Any suggestions would be super helpful.任何建议都会非常有帮助。

Code to create these two images:创建这两个图像的代码:

# Individual bounding boxes

image_0 = cv2.imread('63976500-anderson-herald-bulletin-Jun-18-1968-p-64.jpg')
# Black box, to reproduce: image_0 = np.zeros((5000, 6000, 3), dtype = "uint8")

bbox_list = [[195, 3455, 633, 4213], [658, 3427, 1094, 4222], [1120, 3435, 1553, 4473], [295, 3421, 531, 3451], [201, 3313, 1548, 3409]]

for bbox in bbox_list:
    image_0 = cv2.rectangle(image_0, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0,255,0), 10)

cv2.imwrite("original_bboxes.jpg", image_0)


# Grouped bounding box

image_1 = cv2.imread('63976500-anderson-herald-bulletin-Jun-18-1968-p-64.jpg')
# Black box, to reproduce: image_1 = np.zeros((5000, 6000, 3), dtype = "uint8")

coordinates = np.array([[195,3313],[195,4222],[1120,4222],[1120,4473],[1553,4473],[1553,3313]], np.int32)

image_1 = cv2.polylines(image_1, [coordinates], True, (0,255,0), 10)

cv2.imwrite("grouped_bboxes.jpg", image_1)

单独的边界框

合并边界框

You could draw the convex hull of the contour points (this was drawn by hand):您可以绘制轮廓点的凸包(这是手工绘制的):1

Then keep only the outer contour and try the polygonal approximation.然后只保留外轮廓并尝试多边形近似。 I must admit that I cannot think of a smarter way of getting only vertical and horizontal lines.我必须承认,我想不出一个更聪明的方法来只获得垂直和水平线。

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

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