## 目标检测 IOU 计算

Posted by wxianfeng Mon, 03 Mar 2014 07:13:00 GMT

iou 为面积的交并比，在目标检测中非常常见，英文为 Intersection-over-Union。

python 代码:

```class IOU:

def calc_iou(self, bbox1, bbox2):
"""
calc iou
:param bbox1: (x1,y1,x2,y2)
:param bbox2: (x1,y1,x2,y2)
:return:
"""
w_1 = abs(bbox1[0] - bbox1[2])
h_1 = abs(bbox1[1] - bbox1[3])
area_1 = w_1 * h_1

w_2 = abs(bbox2[0] - bbox2[2])
h_2 = abs(bbox2[1] - bbox2[3])
area_2 = w_2 * h_2

min_x_1 = min(bbox1[0], bbox1[2])
min_y_1 = min(bbox1[1], bbox1[3])
max_x_1 = max(bbox1[0], bbox1[2])
max_y_1 = max(bbox1[1], bbox1[3])

min_x_2 = min(bbox2[0], bbox2[2])
min_y_2 = min(bbox2[1], bbox2[3])
max_x_2 = max(bbox2[0], bbox2[2])
max_y_2 = max(bbox2[1], bbox2[3])

overlap_min_x = max(min_x_1, min_x_2)
overlap_min_y = max(min_y_1, min_y_2)
overlap_max_x = min(max_x_1, max_x_2)
overlap_max_y = min(max_y_1, max_y_2)

overlap_w = max(0, overlap_max_x - overlap_min_x)
overlap_h = max(0, overlap_max_y - overlap_min_y)
overlap_area = overlap_w * overlap_h

iou = overlap_area / (area_1 + area_2 - overlap_area)

return iou```

This entry was posted on Mon, 03 Mar 2014 07:13:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags ,