图像处理——Edge Boxes边缘检测

前言

传统的边缘检测对一些内容,色彩比较丰富的图像,提取出来的边缘并不理想,ECCV2014来自于微软研究院的Piotr等人的《Edge Boxes: Locating Object Proposals from Edges》这个文章,采用的是纯图像的方法实现了目标检测的算法,也是基于物体的边缘分割。这个算法对边缘的提取要好过传统的canny算法。如果想要深入了解可以看大神的论文。

Edge Boxes

1.检测代码

void edgebox(Mat &src,Mat &dst, modelInit &model, paraClass &o)
{
	Mat I = src.clone();
	assert(I.rows != 0 && I.cols != 0);

	clock_t begin = clock();
	model.opts.nms = 1;
	Mat I_resize;
	float shrink = 4;
	resize(I, I_resize, Size(), 1 / shrink, 1 / shrink);
	tuple<Mat, Mat, Mat, Mat> detect = edgesDetect(I_resize, model, 4);
	Mat E, O, unuse1, unuse2;
	tie(E, O, unuse1, unuse2) = detect;
	E = edgesNms(E, O, 2, 0, 1, model.opts.nThreads);
	Mat bbs;
	cout << 1 << endl;
	bbs = edgebox_main(E, O, o) * shrink;
	cout << "time:" << ((double)clock() - begin) / CLOCKS_PER_SEC << "s" << endl;

	I.copyTo(dst);


	//for top10 box scores

	for (int i = 0; i < model.opts.showboxnum; i++) {

		//draw the bbox
		Point2f p1(bbs.at<float>(i, 0), bbs.at<float>(i, 1));
		Point2f p2(bbs.at<float>(i, 0) + bbs.at<float>(i, 2), bbs.at<float>(i, 1) + bbs.at<float>(i, 3));
		Point2f p3(bbs.at<float>(i, 0), bbs.at<float>(i, 1) + bbs.at<float>(i, 3));
		Point2f p4(bbs.at<float>(i, 0) + bbs.at<float>(i, 2), bbs.at<float>(i, 1));


		int tlx = (int)bbs.at<float>(i, 0);
		int tly = (int)bbs.at<float>(i, 1);
		//brx may be bigger than I.cols-1
		//bry may be bigger than I.rows-1
		int brx = std::min((int)(bbs.at<float>(i, 0) + bbs.at<float>(i, 2)), I.cols - 1);
		int bry = std::min((int)(bbs.at<float>(i, 1) + bbs.at<float>(i, 3)), I.rows - 1);

		Mat box;
		box = I.colRange(tlx, brx).rowRange(tly, bry);

		rectangle(dst, p1, p2, Scalar(0, 255, 0), 1);
		Point2f ptext(bbs.at<float>(i, 0), bbs.at<float>(i, 1) - 3);
		putText(dst, to_string(bbs.at<float>(i, 4)), ptext, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0), 1);

	}
}

void  edgeDetection(Mat &src, Mat &dst, modelInit &model, paraClass &o)
{
	Mat I = src.clone();
	
	assert(I.rows != 0 && I.cols != 0);

	///clock_t begin = clock();
	model.opts.nms = 1;
	Mat I_resize;
	float shrink = 4;
	
	tuple<Mat, Mat, Mat, Mat> detect = edgesDetect(I, model, 4);
	Mat E, O, unuse1, unuse2;
	tie(E, O, unuse1, unuse2) = detect;
	E = edgesNms(E, O, 2, 0, 1, model.opts.nThreads);
	Mat bbs;
	bbs = edgebox_main(E, O, o) * shrink;

	double E_min, E_max;
	cv::minMaxLoc(E, &E_min, &E_max);
	dst = (E - E_min) / (E_max - E_min) * 255;
	dst.convertTo(dst, CV_8U);
}

2.运行效果


你可能感兴趣的:(opencv)