IoU 计算
假设:预测框坐标为$(x_1,y_1,w_1,h_1)$,真实目标框坐标为$(x_2,y_2,w_2,h_1)$
计算过程:
- 判断预测框与真实框时候重合
- 计算重合的面积以及IoU
Python程序实现:
def IOU(Prediction, GroundTruth):
x1 = Prediction[0]
y1 = Prediction[1]
width1 = Prediction[2]
height1 = Prediction[3]
x2 = GroundTruth[0]
y2 = GroundTruth[1]
width2 = GroundTruth[2]
height2 = GroundTruth[3]
endx = max(x1+width1, x2+width2)
startx = min(x1, x2)
width = width1 + width2 - (endx - startx)
endy = max(y1+height1, y2+height2)
starty = min(y1, y2)
height = height1 + height2 - (endy - starty)
if width <=0 or height<=0:
ratio = 0
else:
Area = width * height
Area1 = width1 * height1
Area2 = width2 * height2
ratio = (Area * 1. / (Area1 + Area2 - Area)).item()
return ratio