本篇总结Loss Function中的IoU系列代码。
1. IoU
交并集,两个框交集面积除以并集面积。(论写写画画的重要性)
"""
box1[x1, y1, x2, y2]
box2[x1, y1, x2, y2]
return iou
"""
def iou(box1, box2):
# Intersection
w = max(0, min(box1[2], box2[2])-max(box1[0], box2[0]))
h = max(0, min(box1[3], box2[3])-max(box1[1], box2[1]))
# Union
s1 = (box1[2]-box1[0]) * (box1[3]-box1[1])
s2 = (box2[2]-box2[0]) * (box2[3]-box2[1])
return (w*h)/(s1+s2-w*h)