How to calculate intersection over union for more than two bounding boxes?
Asked Answered
O

1

2

I have more than two bounding boxes where I want to calculate the intersection over union. I have done it for two bounding boxes but is there a way to accommodate more than two bounding boxes?

I have tried this with two bounding boxes

def intersection_over_union(self,image,humanRegion_bbs, belongings_bbs):
    """
    Calculate overlap between two bounding boxes [x, y, w, h] as
    the area of intersection over the area of unity
    """
    if len(humanRegion_bbs)== 4 and len(belongings_bbs) == 4 :
        x1, y1, w1, h1 = (humanRegion_bbs[0], humanRegion_bbs[1],
                          humanRegion_bbs[2], humanRegion_bbs[3])
        x2, y2, w2, h2 = (belongings_bbs[0], belongings_bbs[1],
                          belongings_bbs[2], belongings_bbs[3])
        w_I = min(x1 + w1, x2 + w2) - max(x1, x2)
        h_I = min(y1 + h1, y2 + h2) - max(y1, y2)
        if w_I <= 0 or h_I <= 0:  # no overlap
            return 0
        intersection_area = w_I * h_I
        union = w1 * h1 + w2 * h2 - intersection_area
        # intersection over union
        iou = intersection_area / union
        return iou

I want to check the intersection over union between humanRegion_bbs and an array of belongings_bbs. The array of belongings_bbs is of length 4.

How should I apporach this problem?

Originality answered 7/12, 2018 at 1:34 Comment(3)
Possible dupeCoquina
@Coquina sorry. the link mentioned by you does not answer the question I have postedOriginality
You need to clarify your question. How exactly are you defining the values of intersection and union when there are multiple belongings_bbs rectangles?Deliquescence
C
1

You can follow this self-explanatory function.

(box1_x1, box1_y1, box1_x2, box1_y2) = box1
(box2_x1, box2_y1, box2_x2, box2_y2) = box2

# The (yi1, xi1, yi2, xi2) coordinates of the intersection of box1 and box2.
xi1 = max(box1_x1,box2_x1)
yi1 = max(box1_y1,box2_y1)
xi2 = min(box1_x2,box2_x2)
yi2 = min(box1_y2,box2_y2)
inter_width = (xi2 - xi1)
inter_height = (yi2 - yi1)  
inter_area = inter_width * inter_height if inter_width >0 and inter_height>0 else 0

# Union(A,B) = A + B - Inter(A,B)
box1_area = (box1_x2 - box1_x1)*(box1_y2 - box1_y1)
box2_area = (box2_x2 - box2_x1)*(box2_y2 - box2_y1)
union_area = box1_area + box2_area - inter_area

iou = inter_area / union_area
Caught answered 7/2, 2023 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.