4/19/2022

python calculate intersect of union area between two box

 refer to code:

..

def IoU(box1, box2):
# box = (x1, y1, x2, y2)
box1_area = (box1[2] - box1[0] + 1) * (box1[3] - box1[1] + 1)
box2_area = (box2[2] - box2[0] + 1) * (box2[3] - box2[1] + 1)

# obtain x1, y1, x2, y2 of the intersection
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
x11, y11, x12, y12 = box1
x21, y21, x22, y22 = box2
if x21<x11 and y21<y11 and x22>x12 and y22>y12:
return 1.0
if x21>x11 and y21>y11 and x22<x12 and y22<y12:
return 1.0
# compute the width and height of the intersection
w = max(0, x2 - x1 + 1)
h = max(0, y2 - y1 + 1)

inter = w * h
iou = inter / (box1_area + box2_area - inter)
return iou

..


Thank you

www.marearts.com


No comments:

Post a Comment