我有多个矩形边界框,它们属于同一个对象(报纸文章的某些部分),如第一张图片所示。我正在尝试找到一种方法将它们合并为一个多边形边界框,用于整篇文章,就像第二张图片一样。
我已经看到了很多基于合并重叠边界框的解决方案,但是我不在乎它们是否重叠,因为我已经知道它们属于同一篇文章。在某些情况下,标题相当远(例如在图片上方),因此基于填充的解决方案也不起作用。
我感觉应该有一个cv2
函数可以做到这一点,但如果有的话,我会忽略它。任何建议都将非常有帮助。
创建这两个图像的代码:
# 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)