重叠度

ioU

2023-03-10

IoU 是[[目标检测]]中计算目标检测结果和实际结果重叠度的一种常用指标,它通过计算检测框和真实框的交集和与并集的比例,来衡量检测结果的准确性。

IoU 的取值范围是 (0, 1),值越接近 1 表示检测结果与实际结果之间的重叠度越高,表示检测结果越准确,而值越接近 0 表示检测结果与实际结果之间的重叠越小,表示结果越不准确。

两个矩形框 A 和 B,左上角和右下角的坐标为:

  • A(x 1_A, y 1_A, x 2_A, y 2_A)
  • B(x 1_B, y 1_B, x 2_B, y 2_B)

两个矩形框的交集面积 S(intersection) 为:

1
S(intersection) = max(0, x2_A - x1_A) * max(0, y2_A - y1_A) ∩
2
max(0, x2_B - x1_B) * max(0, y2_B - y1_B)

两个矩形框的并集面积 S(union)为:

1
S(union) = max(0, x 2_A - x 1_A) * max(0, y 2_A - y 1_A) ∪
2
max(0, x 2_B - x 1_B) * max(0, y 2_B - y 1_B)

则它们之间的 IOU 为:

1
IOU = S(intersection) / S(union)

[[Python]] 实现:

1
def compute_iou(box1: list, box2: list) -> float:
2
"""计算两个矩形的重叠度
3
4
Args: xyxy 形式
5
box1 (tup / list): (x1, y1, x2, y2)
6
box2 (tup / list): (x1, y1, x2, y2)
7
8
Returns:
9
float: 两个边界框的 IoU
10
"""
11
12
x1min, y1min, x1max, y1max = box1
13
x2min, y2min, x2max, y2max = box2
14
15
# 计算相交区域的左上角和右下角坐标
16
xmin = max(x1min, x2min)
17
ymin = max(y1min, y2min)
18
xmax = min(x1max, x2max)
19
ymax = min(y1max, y2max)
20
21
# 计算相交区域面积
22
inter_area = max(0, xmax - xmin) * max(0, ymax - ymin)
23
24
# 计算两个矩形的面积
25
box1_area = (y1max - y1min) * (x1max - x1min)
26
box2_area = (y2max - y2min) * (x2max - x2min)
27
union_area = box1_area + box2_area - inter_area
28
29
# 返回 IoU
30
return inter_area / union_area

如果数据是 xywh 形式, 需要先转为 xyxy:

1
def xywh_to_xyxy(xywh: list) -> tuple:
2
"""xywh 转 xyxy
3
4
Args:
5
xywh (list / tuple): [x, y, w, h]
6
7
Returns:
8
tuple: (xmin, ymin, xmax, ymax)
9
"""
10
11
x, y, w, h = xywh
12
13
xmin, ymin = x - w / 2, y - h / 2
14
xmax, ymax = x + w / 2, y + h / 2
15
16
return (xmin, ymin, xmax, ymax)

xyxy 转 xywh:

1
def xyxy_to_xywh(xyxy: list) -> tuple:
2
"""xyxy 转 wywh
3
4
Args:
5
xywh (list / tuple): [xmin, ymin, xmax, ymax]
6
7
Returns:
8
tuple: (x, y, w, h)
9
"""
10
11
xmin, ymin, xmax, ymax = xyxy
12
13
x, y = (xmin + xmax) / 2, (ymin + ymax) / 2
14
w, h = xmax - xmin, ymax - ymin
15
16
return (x, y, w, h)