Example #1
0
int main(void)
{
	GetHistogram gh;
	ObjectFinder of;
	cv::Mat img1, img2;
	cv::Mat imgROI;
	cv::Mat histogram;
	cv::Mat backProject;

	img1 = cv::imread("..\\..\\..\\source\\1.jpg");
	if(!img1.data)
		return 0;

	imgROI = img1(cv::Rect(80,300,35,40));
	cv::rectangle(img1,cv::Rect(110,260,35,40),cv::Scalar(0,0,255));
	cv::namedWindow("img1");
	cv::imshow("img1", img1);
	//获得imgROI的直方图
	histogram = gh.getHueHistogram(imgROI, 65);
	cv::normalize(histogram,histogram,1.0);
	of.setHistogram(histogram);
	//获取并处理第二幅图像
	cv::Mat hsv; 
	std::vector<cv::Mat> hue;
	img2 = cv::imread("..\\..\\..\\source\\2.jpg");
	cv::cvtColor(img2, hsv, CV_BGR2HSV);
	cv::split(hsv, hue);
	cv::threshold(hue[1], hue[1], 65, 255, cv::THRESH_BINARY);//这个是为了将低饱和度的图像从反向投影结果中剔除
	//对于第二幅图进行反向投影,使用第一幅图ROI的直方图
	backProject = of.finder(img2);
	cv::bitwise_and(backProject, hue[1], backProject);
	cv::namedWindow("backProject");
	cv::imshow("backProject",backProject);
	//均值漂移
	cv::Rect rect(110,260,35,40);
	cv::rectangle(img2, rect, cv::Scalar(0,0,255));
	cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);//最大迭代次数是10,移动距离阈值是0.01
	cv::meanShift(backProject,rect,criteria);
	cv::rectangle(img2, rect, cv::Scalar(0,255,0));
	cv::namedWindow("img2");
	cv::imshow("img2",img2);

	cv::waitKey(0);
	return 0;
}
int main()
{
	// Read reference image
	cv::Mat image= cv::imread("../images/baboon1.jpg");
	if (!image.data)
		return 0;

	// Define ROI
	cv::Mat imageROI= image(cv::Rect(110,260,35,40));
	cv::rectangle(image, cv::Rect(110,260,35,40), cv::Scalar(0,0,255));

	// Display image
	cv::namedWindow("Image 1");
	cv::imshow("Image 1",image);

	// Get the Hue histogram
	int minSat=65;
	ColorHistogram hc;
	cv::MatND colorhist= hc.getHueHistogram(imageROI, minSat);

	ObjectFinder finder;
	finder.setHistogram(colorhist);
	finder.setThreshold(0.2f);

	// Second image
	image= cv::imread("../images/baboon3.jpg");

 	// Display image
	cv::namedWindow("Image 2");
	cv::imshow("Image 2",image);

	// Convert to HSV space
	cv::Mat hsv;
	cv::cvtColor(image, hsv, CV_BGR2HSV);

	// Split the image
	vector<cv::Mat> v;
	cv::split(hsv,v);

	// Eliminate pixels with low saturation
	cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
	cv::namedWindow("Saturation");
	cv::imshow("Saturation",v[1]);

    // Get back-projection of hue histogram
	int ch[1]={0};
	cv::Mat result= finder.find(hsv,0.0f,180.0f,ch,1);

	cv::namedWindow("Result Hue");
	cv::imshow("Result Hue",result);

	// Eliminate low stauration pixels
	cv::bitwise_and(result,v[1],result);
	cv::namedWindow("Result Hue and raw");
	cv::imshow("Result Hue and raw",result);

	cv::Rect rect(110,260,35,40);
	cv::rectangle(image, rect, cv::Scalar(0,0,255));

	cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);
	cout << "meanshift= " << cv::meanShift(result,rect,criteria) << endl;

	cv::rectangle(image, rect, cv::Scalar(0,255,0));

	// Display image
	cv::namedWindow("Image 2 result");
	cv::imshow("Image 2 result",image);

	cv::waitKey();
	return 0;
}