IOU是交并比(Intersection-over-Union)是目标检测中使用的一个概念是产生的候选框(candidate bound)与原标记框(ground truth bound)的交叠率,即它们的交集与并集的比值。最理想情况是完全重叠,即比值为1。在多目标跟踪中,用来判别跟踪框和目标检测框之间的相似度。
IoU是两个区域的交除以两个区域的并得出的结果
from __future__ import print_function
from numba import jit
import numpy as np
# 计算IOU
@jit
def iou(bb_test, bb_gt):
"""
计算预测框和目标框的交并比
:param bb_test:表示预测框的左上角和右下角的坐标 [x1, y1, x2, y2]
:param bb_gt: 表示目标框的左上角和右下角的坐标 [x1, y1, x2, y2]
:return:
"""
# 交集区域左上角坐标的最大值
xx1 = np.maximum(bb_test[0], bb_gt[0])
yy1 = np.maximum(bb_test[1], bb_gt[1])
# 交集区域右下角坐标的最大值
xx2 = np.maximum(bb_test[2], bb_gt[2])
yy2 = np.maximum(bb_test[3], bb_gt[3])
# 交集区域的宽
w = np.maximum(0, xx2 - xx1)
# 交集区域的高
h = np.maximum(0, yy2 - yy1)
# 交集区域的面积
hw = h * w
# 并集的面积
s = (bb_test[2] - bb_test[0]) * (bb_test[3] - bb_test[1]) + (bb_gt[2] - bb_gt[0]) * (bb_gt[3] - bb_gt[1]) - hw
return hw/s
if __name__ == '__main__':
print(iou([133, 59, 310, 213], [182, 94, 327, 248]))
print(iou([182, 94, 327, 248], [133, 59, 310, 213]))