Python计算机视觉里的IOU计算

其中x1,y1;x2,y2分别表示两个矩形框的中心点

def calcIOU(x1, y1, w1, h1, x2, y2, w2, h2):
    if((abs(x1 - x2) < ((w1 + w2)/ 2.0)) and (abs(y1-y2) < ((h1 + h2)/2.0))):
        left = max((x1 - (w1 / 2.0)), (x2 - (w2 / 2.0)))
        upper = max((y1 - (h1 / 2.0)), (y2 - (h2 / 2.0)))

        right = min((x1 + (w1 / 2.0)), (x2 + (w2 / 2.0)))
        bottom = min((y1 + (h1 / 2.0)), (y2 + (h2 / 2.0)))

        inter_w = abs(left - right)
        inter_h = abs(upper - bottom)
        inter_square = inter_w * inter_h
        union_square = (w1 * h1)+(w2 * h2)-inter_square

        calcIOU = inter_square/union_square * 1.0
        print("calcIOU:", calcIOU)
    else:
        print("No intersection!")

    return calcIOU
def main():
    calcIOU(1, 2, 2, 2, 2, 1, 2, 2)

if __name__ == '__main__':
    main()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
目标检测是计算机视觉中的重要任务,它旨在检测图像或视频中的特定目标,并给出它们的位置和边界框。在目标检测中,通常会使用一些评估指标,如真阳性(True Positive,TP)和假阳性(False Positive,FP)来衡量算法的性能。 TP表示正确检测到的目标数目,而FP表示算法错误检测到的目标数目。下面是一个使用Python计算TP和FP的示例代码: ```python def calculate_tp_fp(ground_truth, detection_result): tp = 0 fp = 0 for bbox in detection_result: found_match = False for gt_bbox in ground_truth: iou = calculate_iou(bbox, gt_bbox) if iou >= 0.5: # 设定IOU阈值 found_match = True break if found_match: tp += 1 else: fp += 1 return tp, fp def calculate_iou(box1, box2): # 计算两个边界框的交并比(IoU) # 这省略具体实现 return iou # 测试示例 ground_truth = [[10, 10, 50, 50], [70, 80, 120, 150]] detection_result = [[15, 20, 45, 55], [80, 90, 110, 140], [200, 200, 250, 250]] tp, fp = calculate_tp_fp(ground_truth, detection_result) print("True Positives: {}".format(tp)) print("False Positives: {}".format(fp)) ``` 在上述示例代码中,`calculate_tp_fp`函数接收两个参数,`ground_truth`表示真实目标的边界框列表,`detection_result`表示算法检测到的边界框列表。函数通过计算每个检测结果的IOU与真实目标的IOU是否大于等于0.5来决定其是TP还是FP。最后,返回的`tp`和`fp`即为计算得到的真阳性和假阳性的数目。 这只是一个简单的示例代码,实际情况中可能需要考虑更多的细节和优化。希望以上回答对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值