NMS算法实现

NMS算法(非极大值抑制)是目标检测算法中经典的后处理步骤,其本质是搜索局部最大值,抑制非极大值元素。主要利用目标检测框以及对应的置信度分数,设置一定的阈值来删除重叠较大的边界框。

其算法流程如下:

根据置信度得分进行排序

选择置信度最高的目标检测框添加到输出列表中,将其从检测框列表中删除

计算该检测框与剩余候选检测框的IOU

删除IOU大于阈值的检测框

重复上述4步,直至检测框列表为空

复制代码
import numpy as np


def nms(dets, thresh):  # x1, y1, x2, y2, score
    x1, y1, x2, y2, scores = dets[:, 0], dets[:, 1], dets[:, 2], dets[:, 3], dets[:, 4]
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)  # 各个方框的面积
    order = scores.argsort()[::-1]  # 按置信度排序后的index, 作为候选集
    keep = []  # 保存筛选出来的方框的index
    while order.size > 0:

        i = order[0]  # 当前置信度最大的方框
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, (xx2 - xx1 + 1))
        h = np.maximum(0.0, (yy2 - yy1 + 1))
        inter = w * h  # 当前置信度最大的框和其他所有框的相交面积
        overlap = inter / (areas[i] + areas[order[1:]] - inter)
        inds = np.where(overlap <= thresh)[0]  # 交并比小于thresh的仍然保留在候选集里, 大的过滤掉
        order = order[inds + 1]  # inds + 1对应原来order中overlap小于thresh的项
    return keep


if __name__ == '__main__':
    detections = [
        [10, 20, 100, 100, 0.9],
        [20, 10, 110, 100, 0.88],
        [20, 20, 110, 110, 0.86],
        [40, 50, 200, 200, 0.95],
        [45, 52, 198, 202, 0.87]
    ]
    detections = np.array(detections)
    keeps = nms(detections, 0.5)
    print(detections[keeps])
相关推荐
小O的算法实验室4 分钟前
2026年ASOC,基于人工势场的差分进化算法改进框架,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
爱学习的张大9 分钟前
具身智能论文精读(五):OpenVLA
人工智能·算法
刘大猫.1 小时前
宝马发布全新AI智能座舱助手 能理解用户复杂出行需求
人工智能·算法·机器学习·ai·大模型·算力·ai智能座舱助手
如何原谅奋力过但无声1 小时前
【灵神高频面试题合集01-03】相向双指针、滑动窗口
数据结构·python·算法·leetcode
leoufung1 小时前
LeetCode 42:接雨水 —— 从“矩形法”到双指针的完整思考过程
java·算法·leetcode
_日拱一卒2 小时前
LeetCode:543二叉树的直径
算法·leetcode·职场和发展
汉克老师2 小时前
GESP2025年3月认证C++五级( 第一部分选择题(9-15))
c++·算法·高精度计算·二分算法·gesp5级·gesp五级
穿条秋裤到处跑3 小时前
每日一道leetcode(2026.04.28):获取单值网格的最小操作数
算法·leetcode·职场和发展
leoufung3 小时前
LeetCode 68. Text Justification 题解:贪心与实现细节
算法·leetcode·职场和发展
WL_Aurora3 小时前
【每日一题】前缀和
python·算法