IoU,即Intersection over Union,用于計算兩個圖的重疊度,用于計算兩個标注框(bounding box)之間的相關度,值越高,相關度越高。在NMS(Non-Maximum Suppression,非極大值抑制)或計算mAP(mean Average Precision)中,都會使用IoU判斷兩個框的相關性。
如圖:
代碼實作:
def bb_intersection_over_union(boxA, boxB):
boxA = [int(x) for x in boxA]
boxB = [int(x) for x in boxB]
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
iou = interArea / float(boxAArea + boxBArea - interArea)
return iou