用numpy计算IOU

这段代码定义了一个名为`iou_match_grid`的函数,用于计算两个边界框集合之间的IoU(Intersection over Union)。输入参数`box1`和`box2`是包含多个边界框的numpy数组,每个边界框由四个坐标表示。函数首先对边界框坐标进行拆分,然后计算交集和并集的面积,最后得出IoU。这个函数可能在目标检测或图像处理的上下文中使用。
摘要由CSDN通过智能技术生成
import numpy as np

box1 = np.array([[10, 10, 50, 50],
                [70, 70, 120, 120]])

box2 = np.array([[10, 10, 50, 50],
                [100, 100, 150, 150],
                [80, 80, 160, 160],
                [130, 130, 200, 200]])


def iou_match_grid(box1, box2):

    x11, y11, x12, y12 = np.split(box1, 4, axis=1)
    x21, y21, x22, y22 = np.split(box2, 4, axis=1)

    xa = np.maximum(x11, np.transpose(x21))
    xb = np.minimum(x12, np.transpose(x22))
    ya = np.maximum(y11, np.transpose(y21))
    yb = np.minimum(y12, np.transpose(y22))

    area_inter = np.maximum(0, (xb - xa + 1)) * np.maximum(0, (yb - ya + 1))

    area_1 = (x12 - x11 + 1) * (y12 - y11 + 1)
    area_2 = (x22 - x21 + 1) * (y22 - y21 + 1)
    area_union = area_1 + np.transpose(area_2) - area_inter

    iou = area_inter / area_union

    return iou


print(iou_match_grid(box1, box2))

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值