
NMS(Non-Maximum Suppression,非极大值抑制)是目标检测中常用的一种后处理技术,用于消除冗余的检测框,保留最有可能的检测结果。其主要目的是解决多个边界框重叠的问题,确保每个目标只有一个边界框。
以下是一个简单的 NMS 算法的 Python 实现:
def non_max_suppression(boxes, scores, iou_threshold):
# boxes: (N, 4) array of [x1, y1, x2, y2] coordinates
# scores: (N,) array of confidence scores
# iou_threshold: IoU threshold for suppression
# 获取按置信度分数降序排列的索引
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
# 选择置信度分数最高的边界框
i = order[0]
keep.append(i)
# 计算当前最佳检测框与其他候选框的IoU
xx1 = np.maximum(boxes[i, 0], boxes[order[1:], 0])
yy1 = np.maximum(boxes[i, 1], boxes[order[1:], 1])
xx2 = np.minimum(boxes[i, 2], boxes[order[1:], 2])
yy2 = np.minimum(boxes[i, 3], boxes[order[1:], 3])
w = np.maximum(0.0, xx2 - xx1)
h = np.maximum(0.0, yy2 - yy1)
inter = w * h
area = (boxes[order[1:], 2] - boxes[order[1:], 0]) * (boxes[order[1:], 3] - boxes[order[1:], 1])
iou = inter / (area + (boxes[i, 2] - boxes[i, 0]) * (boxes[i, 3] - boxes[i, 1]) - inter)
# 抑制IoU超过阈值的边界框
inds = np.where(iou <= iou_threshold)[0]
order = order[inds + 1]
return keep原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。