当前位置:   article > 正文

nms算法c++实现_nmsboxes源码 c++

nmsboxes源码 c++
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct Bbox
{
    int x;
    int y;
    int w;
    int h;
    float score;

}Bbox;

bool sort_score(Bbox box1,Bbox box2)
{
    return (box1.score > box2.score);

}

float iou(Bbox box1,Bbox box2)
{
    int x1 = std::max(box1.x,box2.x);
    int y1 = std::max(box1.y,box2.y);
    int x2 = std::min((box1.x + box1.w),(box2.x + box2.w));
    int y2 = std::min((box1.y + box1.h),(box2.y + box2.h));
    float over_area = (x2 - x1) * (y2 - y1);
    float iou = over_area/(box1.w * box1.h + box2.w * box2.h-over_area);
    return iou;
}
//方法1
vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
{
    std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
    std::vector<Bbox>del(vec_boxs.size(),false);
    for(int i =0; i<vec_boxs.size();i++)
    {
        for (int j =0;j<vec_boxs.size();j++)
        {
            float iou_value =iou(vec_boxs[i],vec_boxs[j]);
            if(iou_value>threshold)
            {
                del[j]=true;
            }
        }
    }
     std::vector<Bbox>results;
    for(const auto i :del)
    {
        if(!del[i]) results.push_back(vec_box[i]);
    }
    return results;
}
//方法2  这种执行效率更高
vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
{
    vector<Bbox>results;
    while(vec_boxs.size() > 0)
    {
        std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
        results.push_back(vec_boxs[0]);
        for(int i =0;i <vec_boxs.size()-1;i++)
        {
            float iou_value =iou(vec_boxs[0],vec_boxs[i+1]);
            if (iou_value >threshold)
            {
                vec_boxs.erase(vec_boxs[i+1]);
            }
        }
        vec_boxs.erase(vec_boxs[0]);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号