编写一个交并比函数IOU,接口为float iou_compute(rectangle_box A, rectangle_box B);,rectangle_box中参数为矩形框的中心点坐标和矩形框的宽和高
时间: 2024-05-15 15:20:01 浏览: 56
python实现交并比IOU教程
以下是Python实现:
```python
class rectangle_box:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def iou_compute(A, B):
# 计算A和B的左上角和右下角坐标
A_x1 = A.x - A.w / 2
A_y1 = A.y - A.h / 2
A_x2 = A.x + A.w / 2
A_y2 = A.y + A.h / 2
B_x1 = B.x - B.w / 2
B_y1 = B.y - B.h / 2
B_x2 = B.x + B.w / 2
B_y2 = B.y + B.h / 2
# 计算交集和并集面积
intersection = max(0, min(A_x2, B_x2) - max(A_x1, B_x1)) * max(0, min(A_y2, B_y2) - max(A_y1, B_y1))
union = A.w * A.h + B.w * B.h - intersection
# 计算交并比
iou = intersection / union
return iou
```
使用示例:
```python
A = rectangle_box(5, 5, 10, 10)
B = rectangle_box(8, 8, 10, 10)
iou = iou_compute(A, B)
print(iou) # 输出0.4444444444444444
```
阅读全文