-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIou_compute.py
30 lines (30 loc) · 1.09 KB
/
Iou_compute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
# author:"Zhang Shuyu"
def IOU( box1, box2 ):
"""
:param box1:[x1,y1,x2,y2] 左上角的坐标与右下角的坐标
:param box2:[x1,y1,x2,y2]
:return: iou_ratio--交并比
"""
width1 = abs(box1[2] - box1[0])
height1 = abs(box1[1] - box1[3]) # 这里y1-y2是因为一般情况y1>y2,为了方便采用绝对值
width2 = abs(box2[2] - box2[0])
height2 = abs(box2[1] - box2[3])
x_max = max(box1[0],box1[2],box2[0],box2[2])
y_max = max(box1[1],box1[3],box2[1],box2[3])
x_min = min(box1[0],box1[2],box2[0],box2[2])
y_min = min(box1[1],box1[3],box2[1],box2[3])
iou_width = x_min + width1 + width2 - x_max
iou_height = y_min + height1 + height2 - y_max
if iou_width <= 0 or iou_height <= 0:
iou_ratio = 0
else:
iou_area = iou_width * iou_height # 交集的面积
box1_area = width1 * height1
box2_area = width2 * height2
iou_ratio = iou_area / (box1_area + box2_area - iou_area) # 并集的面积
return iou_ratio
box1 = [1,3,4,1]
box2 = [2,4,5,2]
print(IOU(box1,box2))