-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_iou.py
76 lines (57 loc) · 1.9 KB
/
get_iou.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def xywh(bboxA, bboxB):
"""
This function computes the intersection over union between two
2-d boxes (usually bounding boxes in an image.
Attributes:
bboxA (list): defined by 4 values: [xmin, ymin, width, height].
bboxB (list): defined by 4 values: [xmin, ymin, width, height].
(Order is irrelevant).
Returns:
IOU (float): a value between 0-1 representing how much these boxes overlap.
"""
xA, yA, wA, hA = bboxA
areaA = wA * hA
xB, yB, wB, hB = bboxB
areaB = wB * hB
overlap_xmin = max(xA, xB)
overlap_ymin = max(yA, yB)
overlap_xmax = min(xA + wA, xB + wB)
overlap_ymax = min(yA + hA, yB + hB)
W = overlap_xmax - overlap_xmin
H = overlap_ymax - overlap_ymin
if min(W, H) < 0:
return 0
intersect = W * H
union = areaA + areaB - intersect
return intersect / union
def xyXY(bboxA, bboxB):
"""
Similar to the above function, this one computes the intersection over union
between two 2-d boxes. The difference with this function is that it accepts
bounding boxes in the form [xmin, ymin, XMAX, YMAX].
Attributes:
bboxA (list): defined by 4 values: [xmin, ymin, XMAX, YMAX].
bboxB (list): defined by 4 values: [xmin, ymin, XMAX, YMAX].
Returns:
IOU (float): a value between 0-1 representing how much these boxes overlap.
"""
xminA, yminA, xmaxA, ymaxA = bboxA
widthA = xmaxA - xminA
heightA = ymaxA - yminA
areaA = widthA * heightA
xminB, yminB, xmaxB, ymaxB = bboxB
widthB = xmaxB - xminB
heightB = ymaxB - yminB
areaB = widthB * heightB
xA = max(xminA, xminB)
yA = max(yminA, yminB)
xB = min(xmaxA, xmaxB)
yB = min(ymaxA, ymaxB)
W = xB - xA
H = yB - yA
if min(W, H) < 0:
return 0
intersect = W * H
union = areaA + areaB - intersect
iou = intersect / union
return iou