C++实现nms(非极大值抑制)

#include<iostream> 
#include<vector> 
#include<algorithm>
#include<cmath>
using namespace std; 
typedef struct Bbox
{
    float x;
    float y;
    float w;
    float h;
    float score;
}Bbox;

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

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

float iou(BBox box1, BBox box2)
{
    float x1 = std::max(box1.x, box2.x);
    float y1 = std::max(box1.y, box2.y);
    float x2 = std::min((box1.x + box1.w),
        (box2.x + box2.w));
    float 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;
}

int *keep;

vector<Bbox> nms(std::vector<Bbox>& vec_boxs, float threshold)
{
	vector<BBox>results;
	vector<Bbox>dets;
	vector<Bbox>::iterator iter;
	vector<BBox>vec_Boxs;
	vec_Boxs.resize(vec_boxs.size());

	int indx;
	
	for(indx = 0;indx<vec_boxs.size();indx++){
            vec_Boxs[indx].x = vec_boxs[indx].x;
            vec_Boxs[indx].y = vec_boxs[indx].y;
            vec_Boxs[indx].w = vec_boxs[indx].w;
            vec_Boxs[indx].h = vec_boxs[indx].h;
            vec_Boxs[indx].score = vec_boxs[indx].score;
            vec_Boxs[indx].indx = indx;
    	}

    while (vec_Boxs.size() > 0)
    {
        std::sort(vec_Boxs.begin(), vec_Boxs.end(), sort_score);     
        results.push_back(vec_Boxs[0]);        
        for (int i = 1; i <vec_Boxs.size() ; i++)
        {
            float iou_value = iou(vec_Boxs[0], vec_Boxs[i]);
            if (iou_value > threshold)
            {        	
                vec_Boxs.erase(vec_Boxs.begin()+i);
                i--;                
            }
        }
        vec_Boxs.erase(vec_Boxs.begin());
        
    }
    dets.resize(results.size());
        keep = new int[results.size()];
        for (int i = 0;i<results.size();i++){
        	dets[i].x  =results[i].x;
        	dets[i].y  =results[i].y;
        	dets[i].w  =results[i].w;
        	dets[i].h  =results[i].h;
        	dets[i].score  =results[i].score;
        	keep[i] = results[i].indx;
//        	cout<<dets[i].score<<"for"<<' '<<keep[i] <<endl;
		}
    return dets;
}

int main(){
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值