yolov5 bbox_iou代码
时间: 2023-07-17 09:10:01 浏览: 188
yolov5-5版本代码
下面是YOLOv5中计算两个边界框的IoU(Intersection over Union)的代码:
```python
def bbox_iou(box1, box2):
"""
计算两个边界框的IoU(Intersection over Union)
参数:
box1 (list): 第一个边界框的坐标 [x1, y1, x2, y2]
box2 (list): 第二个边界框的坐标 [x1, y1, x2, y2]
返回:
float: 两个边界框的IoU值
"""
# 计算两个边界框的面积
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
# 计算两个边界框的交集坐标
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
# 计算交集的面积
intersection_area = max(0, x2 - x1) * max(0, y2 - y1)
# 计算并返回IoU值
iou = intersection_area / (area1 + area2 - intersection_area)
return iou
```
使用示例:
```python
box1 = [50, 50, 100, 100]
box2 = [70, 70, 120, 120]
iou = bbox_iou(box1, box2)
print("IoU:", iou)
```
输出结果为:
```
IoU: 0.14285714285714285
```
这是计算两个边界框的IoU的基本代码,你可以根据自己的需求进行修改和扩展。
阅读全文