深度学习八—— 手撕 IoU

文章目录

  • 8. 手撕IoU
    • 矩形框的IoU计算
    • 手撕 IoU

8. 手撕IoU

矩形框的IoU计算

深度学习八—— 手撕 IoU_第1张图片

矩阵 T \textbf{T} T的左下角坐标为 ( X 0 , Y 0 ) (X_0, Y_0) (X0,Y0),右上角坐标为 ( X 1 , Y 1 ) (X_1, Y_1) (X1,Y1)

矩阵 G \textbf{G} G的左下角坐标为 ( A 0 , B 0 ) (A_0, B_0) (A0,B0),右上角坐标为 ( A 1 , B 1 ) (A_1, B_1) (A1,B1)

则IoU的计算过程如下:

I o U = a r e a ( R O I T ) ∩ a r e a ( R O I G ) S X Y + S A B − a r e a ( R O I T ) ∩ a r e a ( R O I G ) IoU = \frac{area(ROI_T) \cap area(ROI_G)}{S_{XY} + S_{AB} - area(ROI_T) \cap area(ROI_G)} IoU=SXY+SABarea(ROIT)area(ROIG)area(ROIT)area(ROIG)

手撕 IoU

# A:
# 左下角坐标(left_x,left_y)
# 右上角坐标(right_x,right_y)
# B:
# 左下角坐标(left_x,left_y)
# 右上角坐标(right_x,right_y)
 
def IOU(rectangle A, rectangleB):
    W = min(A.right_x, B.right_x) - max(A.left_x, B.left_x)
    H = min(A.right_y, B.right_y) - max(A.left_y, B.left_y)
    if W <= 0 or H <= 0:
        return 0;
    SA = (A.right_x - A.left_x) * (A.right_y - A.left_y)
    SB = (B.right_x - B.left_x) * (B.right_y - B.left_y)
    cross = W * H
    return cross/(SA + SB - cross)

你可能感兴趣的:(#,深度学习基础,深度学习,python,人工智能)