面试题-手撕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;
}
相关推荐
minji...21 分钟前
Linux 进程信号(二)信号的保存,sigset_t,sigprocmask,sigpending
linux·运维·服务器·网络·数据结构·c++·算法
庞轩px23 分钟前
模拟面试回答第十三问:JVM内存模型
jvm·面试·职场和发展
罗湖老棍子38 分钟前
最大数(信息学奥赛一本通- P1549)(洛谷-P1198)
数据结构·算法·线段树·单点修改 区间求最大值
人工智能AI技术1 小时前
计算机专业面试必看!90%学生都踩过的算法面雷区
人工智能·面试
小O的算法实验室2 小时前
2026年KBS,赏金猎人优化算法+多无人机移动边缘计算与路径规划,深度解析+性能实测
算法·无人机·边缘计算
xlp666hub2 小时前
深度剖析 Linux Input 子系统(3):从零写一个 Input 驱动,最详细手把手(附完整代码)
linux·面试
用户5671504710212 小时前
OpenClaw 记忆管理系统技术文档
算法
Cosolar2 小时前
吃透这5种Agent模式,搞定智能体开发
人工智能·面试·全栈
935962 小时前
练习题53-60
算法·深度优先
霖大侠3 小时前
Wavelet Meets Adam: Compressing Gradients forMemory-Efficient Training
人工智能·深度学习·算法·机器学习·transformer