-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_iou.py
83 lines (66 loc) · 2.85 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
77
78
79
80
81
82
83
# This is the python code for calculating iou between pred_box and gt_box
# author:Forest 2019.7.19
import numpy as np
def get_iou(pred_box, gt_box):
"""
pred_box : the coordinate for predict bounding box
gt_box : the coordinate for ground truth bounding box
return : the iou score
the left-down coordinate of pred_box:(pred_box[0], pred_box[1])
the right-up coordinate of pred_box:(pred_box[2], pred_box[3])
"""
# 1.get the coordinate of inters
ixmin = max(pred_box[0], gt_box[0])
ixmax = min(pred_box[2], gt_box[2])
iymin = max(pred_box[1], gt_box[1])
iymax = min(pred_box[3], gt_box[3])
iw = np.maximum(ixmax-ixmin+1., 0.)
ih = np.maximum(iymax-iymin+1., 0.)
# 2. calculate the area of inters
inters = iw*ih
# 3. calculate the area of union
uni = ((pred_box[2]-pred_box[0]+1.) * (pred_box[3]-pred_box[1]+1.) +
(gt_box[2] - gt_box[0] + 1.) * (gt_box[3] - gt_box[1] + 1.) -
inters)
# 4. calculate the overlaps between pred_box and gt_box
iou = inters / uni
return iou
def get_max_iou(pred_boxes, gt_box):
"""
calculate the iou multiple pred_boxes and 1 gt_box (the same one)
pred_boxes: multiple predict boxes coordinate
gt_box: ground truth bounding box coordinate
return: the max overlaps about pred_boxes and gt_box
"""
# 1. calculate the inters coordinate
if pred_boxes.shape[0] > 0:
ixmin = np.maximum(pred_boxes[:, 0], gt_box[0])
ixmax = np.minimum(pred_boxes[:, 2], gt_box[2])
iymin = np.maximum(pred_boxes[:, 1], gt_box[1])
iymax = np.minimum(pred_boxes[:, 3], gt_box[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
# 2.calculate the area of inters
inters = iw * ih
# 3.calculate the area of union
uni = ((pred_boxes[:, 2] - pred_boxes[:, 0] + 1.) * (pred_boxes[:, 3] - pred_boxes[:, 1] + 1.) +
(gt_box[2] - gt_box[0] + 1.) * (gt_box[3] - gt_box[1] + 1.) -
inters)
# 4.calculate the overlaps and find the max overlap ,the max overlaps index for pred_box
iou = inters / uni
iou_max = np.max(iou)
nmax = np.argmax(iou)
return iou, iou_max, nmax
if __name__ == "__main__":
# test1
pred_box = np.array([50, 50, 90, 100])
gt_box = np.array([70, 80, 120, 150])
print("The overlap of pred_box and gt_box:", get_iou(pred_box, gt_box))
# test2
pred_bboxes = np.array([[15, 18, 47, 60],
[50, 50, 90, 100],
[70, 80, 120, 145],
[130, 160, 250, 280],
[25.6, 66.1, 113.3, 147.8]])
gt_bbox = np.array([70, 80, 120, 150])
print(get_max_iou(pred_bboxes, gt_bbox))