2DBox和3DBox和旋转Box的iou计算

序言

面试过程中遇到的问题,如何对2Dbox、3Dbox和旋转box求iou。

2D Box计算代码

def cal_2dbox(box1, box2):
    """
    box format: xyxy
    """
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])
    union = (x2 - x1) * (y2 - y1)
    area1 = (box1[2]-box1[0]) * (box1[3]-box1[1])
    area2 = (box2[2]-box2[0]) * (box2[3]-box2[1])
    iou = union/(area1+area2-union)
    return iou

3D Box计算代码

def cal_2dbox(box1, box2):
    """
    box format: xyxy
    """
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    x2 = min(box1[2], box2[2])
    y2 = min(box1[3], box2[3])
    union = (x2 - x1) * (y2 - y1)
    area1 = (box1[2]-box1[0]) * (box1[3]-box1[1])
    area2 = (box2[2]-box2[0]) * (box2[3]-box2[1])
    iou = union/(area1+area2-union)
    return iou

旋转 Box计算代码

def cal_skewbox(box1, box2):
    """
    box format: cxcywh angle
    """
    area1 = box1[2]*box1[3]
    area2 = box2[2]*box2[3]
    box1_inp = ((box1[0], box1[1]), (box1[2], box1[3]), box1[4])
    box2_inp = ((box2[0], box2[1]), (box2[2], box2[3]), box2[4])
    int_pts = cv2.rotatedRectangleIntersection(tuple(box1_inp), tuple(box2_inp))[1]
    #order_pts = cv2.convexHull(int_pts, returnPoints=True)
    union = cv2.contourArea(int_pts)
    iou = union/(area1+area2-union)
    return iou

结果

if __name__ == "__main__":
    #2D
    box1_2d = [50, 100, 100, 200]
    box2_2d = [50, 50, 100, 200]
    iou_2d = cal_2dbox(box1_2d, box2_2d)
    print("二维box的计算结果:", iou_2d)
    #3D
    box1_3d = [50, 100, 30, 100, 200, 60]
    box2_3d = [50, 50, 30, 100, 200, 90]
    iou_3d = cal_3dbox(box1_3d, box2_3d)
    print("三维box的计算结果:", iou_3d)
    #skew
    box1_skew = [50, 100, 100, 200, 0]
    box2_skew = [50, 100, 100, 200, 90]
    iou_skew = cal_skewbox(box1_skew, box2_skew)
    print("旋转box的计算结果:", iou_skew)

终端结果:

二维box的计算结果: 0.6666666666666666
三维box的计算结果: 0.3333333333333333
旋转box的计算结果: 0.3333333333333333
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超超爱AI

土豪请把你的零钱给我点

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值