IOU python实现
一、IOU函数
1. 给定两个box的中心坐标和w,h
2. 用左上角+右下角坐标计算比较方便
3. 相交部分的左上角取最大值,右下角取最小值
4. 相交部分面积,长宽和0作比较,可以囊括所有情况
def IOU(box1, box2): #box2 = [x1,y1,h1,w1] #box2 = [x2,y2,h2,w2] x1,y1,h1,w1 = box1 x2,y2,h2,w2 = box2 # 用四个角坐标计算比较方便 left1 = x1 - w1/2 right1 = x1 + w1/2 top1 = y1 - h1/2 bottom2 = y1 + h1/2 left2 = x2 - w2/2 right2 = x2 + w2/2 top2 = y2 - h2/2 bottom2 = y2 + h2/2 # 相交部分的四个角坐标 left = max(left1, left2) top = max(top1,top2) right = min(right1, right2) bottom = min(bottom1, bottom2) #相交部分的面积 inter = max(0, right - left) * max(0, bottom - top) area1 = h1 * w1 area2 = h2 * w2 union = area1 + area2 - inter iou = inter / union return iou