def cluster_bounding_boxes(self, contours):
bounding_boxes = []
for i in range(len(contours)):
x1,y1,w1,h1 = cv2.boundingRect(contours[i])
parent_bounding_box = self.get_parent_bounding_box(bounding_boxes, i)
if parent_bounding_box is None:
parent_bounding_box = self.BoundingBox(Rect(x1, y1, w1, h1))
parent_bounding_box.members.append(i)
bounding_boxes.append(parent_bounding_box)
for j in range(i+1, len(contours)):
if self.get_parent_bounding_box(bounding_boxes, j) is None:
x2,y2,w2,h2 = cv2.boundingRect(contours[j])
rect = Rect(x2, y2, w2, h2)
distance = parent_bounding_box.rect.distance_to_rect(rect)
if distance < 100:
parent_bounding_box.update_rect(self.extend_rectangle(parent_bounding_box.rect, rect))
parent_bounding_box.members.append(j)
return bounding_boxes
评论列表
文章目录