NMS C++实现

转载https://blog.csdn.net/avideointerfaces/article/details/88551325

 

#include 
#include 
using namespace std;

typedef struct{
	int x;
	int y;
	int width;
	int height;
}Rect;

typedef struct{
	Rect box;
	float score;
	int index;
}BBox;
bool compScore(BBox a, BBox b) {
	if(a.score>b.score) 
		return true;
	else 
		return false;
}
static float get_boxs_iou(Rect rect1, Rect rect2)
{
	int xx1, yy1, xx2, yy2;
 
	xx1 = max(rect1.x, rect2.x);
	yy1 = max(rect1.y, rect2.y);
	xx2 = min(rect1.x + rect1.width - 1, rect2.x + rect2.width - 1);
	yy2 = min(rect1.y + rect1.height - 1, rect2.y + rect2.height - 1);
 
	int insection_width, insection_height;
	insection_width = max(0, xx2 - xx1 + 1);
	insection_height = max(0, yy2 - yy1 + 1);
 
	float insection_area, union_area, iou;
	insection_area = float(insection_width) * insection_height;
	union_area = float(rect1.width*rect1.height + rect2.width*rect2.height - insection_area);
	iou = insection_area / union_area;
	return iou;
}
 
void nms(vector&boxes,vector&scores,float confThreshold, float nmsThreshold, vector &indices ){
	BBox b_;
	vector b_s;
	int i,j;

	//b_s包括 [box坐标,box score,box index],保留index是为了下面根据前两项获取当前index
	for(i=0;inmsThreshold){
				b_s.erase(b_s.begin()+j);
				b_s_size=b_s.size();
			}
		}
	}
}

 

你可能感兴趣的:(算法)