计算宽:
W=min(A1, B1) - max(A0, B0);
若A,B不重合,则W小于0。
同理可以计算高H。
可得如下coding:
#A[x, y, wight, height],B类似;x,y是中心坐标
def IOU(rectangle A, rectangleB):
W = min(A[0]+0.5*A[2], B[0]+0.5*B[2]) - max(A[0]-0.5*A[2], B[0]-0.5*B[2])
H = min(A[1]+0.5*A[3], B[1]+0.5*B[3]) - max(A[1]-0.5*A[3], B[1]-0.5*B[3])
if W <= 0 or H <= 0:
return 0;
SA = A[2] * A[3]
SB = B[2] * B[3]
cross = W * H
return cross/(SA + SB - cross)