昇腾 - AscendCL C++应用开发 目标检测中的非极大值抑制NMS和计算候选边界框之间的交并比IOU

昇腾 - AscendCL C++应用开发 目标检测中的非极大值抑制(NMS,Non-Maximum Suppression)涉及计算候选边界框之间的交并比(IOU,Intersection over Union)

flyfish

结构体 BBox: 定义了一个边界框的数据结构,包含中心坐标、宽高、置信度分数、类别索引和输出索引。
函数 IOU: 计算两个边界框之间的交并比。
函数 NMS: 实现非极大值抑制算法,通过逐步移除与当前最佳边界框的IOU超过给定阈值的边界框来筛选出最佳边界框集合。

#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
struct BBox {
    float x;         // 边界框中心的x坐标
    float y;         // 边界框中心的y坐标
    float w;         // 边界框的宽度
    float h;         // 边界框的高度
    float score;     // 置信度分数
    int classIndex;  // 类别索引
    int index;       // 输出缓冲区的索引
};

// 计算两个边界框的交并比(IOU)
float IOU(const BBox& box1, const BBox& box2) {
    // 计算每个边界框的左上角和右下角坐标
    float x1_min = box1.x - box1.w / 2.0f;
    float y1_min = box1.y - box1.h / 2.0f;
    float x1_max = box1.x + box1.w / 2.0f;
    float y1_max = box1.y + box1.h / 2.0f;

    float x2_min = box2.x - box2.w / 2.0f;
    float y2_min = box2.y - box2.h / 2.0f;
    float x2_max = box2.x + box2.w / 2.0f;
    float y2_max = box2.y + box2.h / 2.0f;

    // 计算相交区域的左上角和右下角坐标
    float inter_x_min = std::max(x1_min, x2_min);
    float inter_y_min = std::max(y1_min, y2_min);
    float inter_x_max = std::min(x1_max, x2_max);
    float inter_y_max = std::min(y1_max, y2_max);

    // 计算相交区域的宽度和高度
    float inter_w = std::max(0.0f, inter_x_max - inter_x_min);
    float inter_h = std::max(0.0f, inter_y_max - inter_y_min);

    // 计算相交面积
    float inter_area = inter_w * inter_h;

    // 计算每个边界框的面积
    float box1_area = box1.w * box1.h;
    float box2_area = box2.w * box2.h;

    // 计算交并比(IOU)
    float iou = inter_area / (box1_area + box2_area - inter_area);
    return iou;
}

// 实现NMS算法
std::vector<BBox> NMS(const std::vector<BBox>& boxes, float iouThreshold) {
    // 按置信度分数从高到低排序
    std::vector<BBox> sortedBoxes = boxes;
    std::sort(sortedBoxes.begin(), sortedBoxes.end(), [](const BBox& a, const BBox& b) {
        return a.score > b.score;
    });

    std::vector<BBox> result;

    // 遍历每一个候选框
    while (!sortedBoxes.empty()) {
        // 选择置信度最高的框
        BBox bestBox = sortedBoxes.front();
        result.push_back(bestBox);

        // 移除与最佳框IOU大于阈值的所有框
        sortedBoxes.erase(std::remove_if(sortedBoxes.begin(), sortedBoxes.end(),
                                         [&](const BBox& box) {
                                             return IOU(bestBox, box) > iouThreshold;
                                         }),
                          sortedBoxes.end());
    }

    return result;
}

int main() {
    // 示例边界框
    std::vector<BBox> boxes = {
        {100.0, 100.0, 50.0, 50.0, 0.9, 0, 0},
        {102.0, 102.0, 50.0, 50.0, 0.85, 0, 1},
        {200.0, 200.0, 50.0, 50.0, 0.75, 1, 2},
        {220.0, 220.0, 50.0, 50.0, 0.7, 1, 3}
    };

    // 进行NMS
    float iouThreshold = 0.5;
    std::vector<BBox> filteredBoxes = NMS(boxes, iouThreshold);

    // 输出结果
    for (const auto& box : filteredBoxes) {
        std::cout << "Box: (" << box.x << ", " << box.y << "), w=" << box.w
                  << ", h=" << box.h << ", score=" << box.score
                  << ", classIndex=" << box.classIndex << std::endl;
    }

    return 0;
}

输出

Box: (100, 100), w=50, h=50, score=0.9, classIndex=0
Box: (200, 200), w=50, h=50, score=0.75, classIndex=1
Box: (220, 220), w=50, h=50, score=0.7, classIndex=1

移除与最佳框IOU大于阈值的所有框

  1. std::remove_if :首先调用 std::remove_if 将所有不符合条件的元素移到容器的前面,并将符合条件的元素移到后面。此时容器的大小并没有变化,且不符合条件的元素仍然存在于容器的内存中。
auto newEnd = std::remove_if(container.begin(), container.end(), condition);
  1. erase :接着,调用 erase 使用 std::remove_if 返回的迭代器范围来删除从“逻辑末尾”到实际末尾的元素。最终,这个操作会减少容器的大小,并真正删除那些符合条件的元素。
container.erase(newEnd, container.end());

这种组合方式可以避免多次调用 erase 来删除每个满足条件的元素,因为删除单个元素的代价较高,而 std::remove_if 只会遍历一次容器,并通过 erase 进行一次性删除。

例子

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    // 移除所有偶数
    auto newEnd = std::remove_if(vec.begin(), vec.end(), [](int x) {
        return x % 2 == 0;  // 条件:为偶数时移除
    });

    // 真正从容器中删除它们
    vec.erase(newEnd, vec.end());

    // 输出结果
    for (int x : vec) {
        std::cout << x << " ";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西笑生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值