NMS代码c++版本

NMS代码c++版本

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

using namespace std;

class NMS {
public:
    NMS(float iouTheta, float scoreTheta, vector<vector<float>> cubesRawShapes) 
        : iouTheta(iouTheta), scoreTheta(scoreTheta), cubesRawShapes(cubesRawShapes) {}

    vector<vector<float>> getboxShapesIouFiltered() { return boxShapesIouFiltered; }

    float computeIou(const vector<float>& cubeShape1, const vector<float>& cubeShape2)
    {
        float h = min(cubeShape1[1], cubeShape2[1]) - max(cubeShape1[0], cubeShape2[0]);
        float w = min(cubeShape1[3], cubeShape2[3]) - max(cubeShape1[2], cubeShape2[2]);
        
        if (h <= 0 || w <= 0) return 0.0f;
        
        float intersection = h * w;
        float area1 = (cubeShape1[1] - cubeShape1[0]) * (cubeShape1[3] - cubeShape1[2]);
        float area2 = (cubeShape2[1] - cubeShape2[0]) * (cubeShape2[3] - cubeShape2[2]);
        
        return intersection / (area1 + area2 - intersection);
    }

    void nmsProcess()
    {
        for (const auto& cubeRawShapes : cubesRawShapes)
        {
            if (cubeRawShapes[4] >= scoreTheta)
            {
                boxShapesScoreFiltered.emplace_back(cubeRawShapes);    
                boxShapesScoreFilteredVis.emplace_back(true);
            }
        }

        while (true)
        {
            int maxScoreShapeIdx = -1;
            float maxScore = -1.0f;
            for (int i = 0; i < boxShapesScoreFiltered.size(); ++i)
            {
                if (boxShapesScoreFilteredVis[i] && boxShapesScoreFiltered[i][4] > maxScore)
                {
                    maxScoreShapeIdx = i;
                    maxScore = boxShapesScoreFiltered[i][4];
                }
            }

            if (maxScoreShapeIdx == -1) break;

            boxShapesScoreFilteredVis[maxScoreShapeIdx] = false;
            boxShapesIouFiltered.emplace_back(boxShapesScoreFiltered[maxScoreShapeIdx]);

            for (int i = 0; i < boxShapesScoreFiltered.size(); ++i)
            {
                if (boxShapesScoreFilteredVis[i] && computeIou(boxShapesIouFiltered.back(), boxShapesScoreFiltered[i]) >= iouTheta)
                {
                    boxShapesScoreFilteredVis[i] = false;
                }
            }
        }
    }

private:
    float iouTheta = 0.f;
    float scoreTheta = 0.f;
    vector<vector<float>> cubesRawShapes{};
    vector<vector<float>> boxShapesScoreFiltered{};
    vector<bool> boxShapesScoreFilteredVis{};
    vector<vector<float>> boxShapesIouFiltered{};
};

int main()
{
    vector<vector<float>> cubesRawShapes = { {30, 10, 200, 200, 0.95},  // [minX, minY, maxX, maxY, score]
                                             {25, 15, 180, 220, 0.98},  
                                             {35, 40, 190, 170, 0.96},
                                             {60, 60, 90, 90, 0.3},
                                             {20, 30, 40, 50, 0.1} };

    const float iouTheta = 0.5;
    const float scoreTheta = 0.5;

    NMS nms(iouTheta, scoreTheta, cubesRawShapes);
    nms.nmsProcess();

    vector<vector<float>> cubesFilterShapes = nms.getboxShapesIouFiltered();

    for (int i = 0; i < cubesFilterShapes.size(); i++)
    {
        for (int j = 0; j < cubesFilterShapes[i].size(); j++)
        {
            cout << cubesFilterShapes[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星光技术人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值