目标检测iou代码示例
时间: 2023-06-03 21:06:51 浏览: 86
IOU(交并比)是用于目标检测评估的常用度量。它表示两个区域的重叠部分占比。具体来说,它是两个区域的交集(重叠部分)除以两个区域的并集(总共的部分)。
IOU的计算公式如下:
IOU = Area of Intersection / Area of Union
IOU值越大,两个区域重叠度越大。通常来说,当IOU值大于0.5时,我们才认为两个区域是“相似”的。
下面是一个示例代码,用来计算两个矩形的IOU值:
```
def calculate_iou(box1, box2):
# Calculate the x-y co-ordinates of the rectangles
x1_left, y1_top, x1_right, y1_bottom = box1
x2_left, y2_top, x2_right, y2_bottom = box2
# Calculate the area of the rectangles
rect1_area = (x1_right - x1_left) * (y1_bottom - y1_top)
rect2_area = (x2_right - x2_left) * (y2_bottom - y2_top)
# Find the overlapping area
overlap_x1 = max(x1_left, x2_left)
overlap_y1 = max(y1_top, y2_top)
overlap_x2 = min(x1_right, x2_right)
overlap_y2 = min(y1_bottom, y2_bottom)
overlap_area = max(0, overlap_x2 - overlap_x1) * max(0, overlap_y2 - overlap_y1)
# Calculate the IOU
iou = overlap_area / (rect1_area + rect2_area - overlap_area)
return iou
```
在上面的代码中,输入参数`box1`和`box2`是两个矩形的坐标。每个矩形都是由左上角和右下角的坐标表示的。坐标用4元组表示,分别是左上
阅读全文