c++版 nms

 

目前这个代码运行会报一些错误,还要再修改修改才行

#include<iostream>
#include<vector>
#include<algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


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

class Solution {
public:

    static bool sort_score(Bbox box1,Bbox box2){
        return box1.score > box2.score ? true : false;
    }


    float iou(Bbox box1,Bbox box2){
        int x1 = max(box1.x,box2.x);
        int y1 = max(box1.y,box2.y);
        int x2 = min(box1.x+box1.w,box2.x+box2.w);
        int y2 = min(box1.y+box1.h,box2.y+box2.h);
        float over_area = (y2 - y1)*(x2 - x1);
        return over_area/(box1.w * box1.h + box2.w * box2.h - over_area);
    }

    vector<Bbox> nms(std::vector<Bbox>&vec_boxs,float threshold)
    {
        vector<Bbox>results;
        std::sort(vec_boxs.begin(),vec_boxs.end(),sort_score);
        while(vec_boxs.size() > 0)
        {
            results.push_back(vec_boxs[0]);
            int index = 1;
            while(index < vec_boxs.size()){
                float iou_value = iou(vec_boxs[0],vec_boxs[index]);
                if(iou_value > threshold)
                    vec_boxs.erase(vec_boxs.begin() + index);
                else
                    index++;
            }
            vec_boxs.erase(vec_boxs.begin());
        }
    }
};

int main(){
    Solution a;
    vector<Bbox> input;

    Bbox box1 = {1,2,3,4,0.3};
    Bbox box2 = {2,3,4,5,0.4};
    input.push_back(box1);
    input.push_back(box2);
    vector<Bbox> res;
    res = a.nms(input,0.2);
    for(int i = 0;i < res.size();i++)
        printf("%d %d %d %d %f",res[i].x,res[i].y,res[i].w,res[i].h,res[i].score);
    return 0;
}

 https://blog.csdn.net/qq_32582681/article/details/81352758

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值