天天看点

OBB目标检测的IOU计算

文章目录

  • ​​1. 基于opencv​​

1. 基于opencv

检测框格式:cx, cy, w, h, a.

def iou_rotate_calculate(boxes1, boxes2):
#    print("####boxes2:", boxes1.shape)
#    print("####boxes2:", boxes2.shape)
    area1 = boxes1[2] * boxes1[3]
    area2 = boxes2[2] * boxes2[3]
    r1 = ((boxes1[0], boxes1[1]), (boxes1[2], boxes1[3]), boxes1[4])
    r2 = ((boxes2[0], boxes2[1]), (boxes2[2], boxes2[3]), boxes2[4])
    int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]
    if int_pts is not None:
        order_pts = cv2.convexHull(int_pts, returnPoints=True)
        int_area = cv2.contourArea(order_pts)
        # 计算出iou
        ious = int_area * 1.0 / (area1 + area2 - int_area)
#        print(int_area)
    else:
        ious=0
    return ious


if __name__ == '__main__':
    b1 = [137.513216, 160.843612, 91.000000, 63.000000, 1.025758]
    b2 = [136.013216, 160.343612, 62.000000, 90.000000, 0.672247]
    print(iou_rotate_calculate(b1, b2))
    b1 = [139.957391, 162.524637, 65.467104, 68.134674, 5.839592]
    b2 = [140.013216, 160.343612, 62.000000, 90.000000, 5.804272]
    print(iou_rotate_calculate(b1, b2))