面试题-手撕NMS(非极大值抑制)

非极大值抑制(Non-Maximum Suppression,NMS)是一种常用于目标检测和计算机视觉中的算法,用于去除重叠的边界框,保留最可能是真实目标的边界框。

其核心就是对一组检测框,找出其中属于同一个类别且分数最高的那个框,然后把和这个框的IOU值大于阈值的那些框都删掉。

在NMS中,其实用到了计算IOU的方法,可以参考:面试题-手撕IOU计算

下面是代码:

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

struct BoundingBox {
    float x1, y1, x2, y2;
    float score;
    int category;
};

bool compareScores(const BoundingBox& a, const BoundingBox& b) {
    return a.score > b.score; // 按照置信度分数降序排序
}

float intersectionArea(const BoundingBox& rect1, const BoundingBox& rect2) {
    float overlapWidth = std::max(0.0f, std::min(rect1.x2, rect2.x2) - std::max(rect1.x1, rect2.x1));
    float overlapHeight = std::max(0.0f, std::min(rect1.y2, rect2.y2) - std::max(rect1.y1, rect2.y1));

    return overlapWidth * overlapHeight;
}

float calculateIOU(const BoundingBox& rect1, const BoundingBox& rect2) {
    float area1 = (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
    float area2 = (rect2.x2 - rect2.x1) * (rect2.y2 - rect2.y1);

    float intersection = intersectionArea(rect1, rect2);
    float unionArea = area1 + area2 - intersection;

    return intersection / unionArea;
}

std::vector<int> nms(const std::vector<BoundingBox>& bboxes, float threshold) {
    std::vector<int> keep;
    std::vector<bool> suppressed(bboxes.size(), false);

    std::vector<BoundingBox> sortedBBoxes = bboxes;
    std::sort(sortedBBoxes.begin(), sortedBBoxes.end(), compareScores);

    for (size_t i = 0; i < sortedBBoxes.size(); ++i) {
        if (suppressed[i]) continue;

        keep.push_back(i);

        for (size_t j = i + 1; j < sortedBBoxes.size(); ++j) {
            if (!suppressed[j] && sortedBBoxes[i].category == sortedBBoxes[j].category) {
                float iou = calculateIOU(sortedBBoxes[i], sortedBBoxes[j]);
                if (iou > threshold) {
                    suppressed[j] = true; // 标记重叠的边界框为已抑制
                }
            }
        }
    }

    return keep;
}

int main() {
    std::vector<BoundingBox> bboxes = {
        {10, 10, 50, 50, 0.9, 1},
        {20, 20, 60, 60, 0.85, 2},
        {30, 30, 70, 70, 0.95, 1},
        {40, 40, 80, 80, 0.75, 2}
    };

    float threshold = 0.5;
    std::vector<int> indices = nms(bboxes, threshold);

    std::cout << "Indices to keep after NMS: ";
    for (auto idx : indices) {
        std::cout << idx << " ";
    }
    std::cout << std::endl;

    return 0;
}
相关推荐
卷毛的技术笔记6 分钟前
从“拆东墙补西墙”到“最终一致”:分布式事务在Spring Boot/Cloud中的破局之道
java·spring boot·分布式·后端·spring cloud·面试·rocketmq
王老师青少年编程17 分钟前
csp信奥赛C++高频考点专项训练之贪心算法 --【排序贪心】:魔法
c++·算法·贪心·csp·信奥赛·排序贪心·魔法
wearegogog12319 分钟前
基于和差波束法的单脉冲测角MATLAB实现
人工智能·算法·matlab
AI科技星27 分钟前
灵魂商数(SQ) · 全域数学统一定义【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
晓觉儿28 分钟前
【GPLT】2026年第十一届团队程序设计天梯赛赛后题解(已写2h,存档中)
数据结构·c++·算法·深度优先·图论
We་ct34 分钟前
LeetCode 322. 零钱兑换:动态规划入门实战
前端·算法·leetcode·typescript·动态规划
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 394. 字符串解码 | C++ 单栈回压法
c++·算法·leetcode
穿条秋裤到处跑1 小时前
每日一道leetcode(2026.04.22):距离字典两次编辑以内的单词
算法·leetcode
淘矿人1 小时前
Claude辅助算法设计与优化
人工智能·python·算法·microsoft·github·bug·pygame
流年如夢1 小时前
自定义类型进阶:联合与枚举
java·c语言·开发语言·数据结构·数据库·c++·算法