Esempio n. 1
0
int main()
{
	VideoCapture capture(0);
	if(!capture.isOpened())
	{
		cout<<"Camera could not be opened";
		exit(0);
	}

	Mat frame;
	capture.read(frame);
	
	Rect rect(220,100,170,170);
	
	Mat imageROI= frame(rect);
	
	Mat hsv;
	ColorHistogram hc;
	ContentFinder finder;
	int minSat=65;int channels[]={0};
	namedWindow("image 2");
	while(1)
	{
		MatND colorhist=hc.getHueHistogram(imageROI,minSat);
		finder.setHistogram(colorhist);
		int a=capture.read(frame);	
		if(!a)
		{
			exit(0);
		}
		cv::cvtColor(frame, hsv, CV_BGR2HSV);
		std::vector<cv::Mat> v;
		cv::split(hsv,v);
		threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
		// Get back-projection of hue histogram
		Mat result= finder.find(hsv,0.0f,180.0f,channels,1);
		// Eliminate low stauration pixels
		cv::bitwise_and(result,v[1],result);
		cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);
		
		cv::CamShift(result,rect,criteria);
		
		cv::rectangle(frame, rect, cv::Scalar(0,0,255));
		imshow("image 2",frame);
		int c= cvWaitKey(10);
		if(char(c)==27)
		{
			exit(0);
		}
		//cv::imshow("Image",result);
		Mat imageROI= frame(rect);
	}
	cv::waitKey(0);
	return 0;
}
int main()
{
	// Read input image
    cv::Mat image= cv::imread("waves.jpg",0);
	if (!image.data)
		return 0; 

	// define image ROI
	cv::Mat imageROI;
	imageROI= image(cv::Rect(216,33,24,30)); // Cloud region

	// Display reference patch
	cv::namedWindow("Reference");
	cv::imshow("Reference",imageROI);

	// Find histogram of reference
	Histogram1D h;
	cv::Mat hist= h.getHistogram(imageROI);
	cv::namedWindow("Reference Hist");
	cv::imshow("Reference Hist",h.getHistogramImage(imageROI));

	// Create the content finder
	ContentFinder finder;

	// set histogram to be back-projected
	finder.setHistogram(hist);
	finder.setThreshold(-1.0f);

	// Get back-projection
	cv::Mat result1;
	result1= finder.find(image);

	// Create negative image and display result
	cv::Mat tmp;
	result1.convertTo(tmp,CV_8U,-1.0,255.0);
	cv::namedWindow("Backprojection result");
	cv::imshow("Backprojection result",tmp);

	// Get binary back-projection
	finder.setThreshold(0.12f);
	result1= finder.find(image);

	// Draw a rectangle around the reference area
	cv::rectangle(image, cv::Rect(216, 33, 24, 30), cv::Scalar(0, 0, 0));

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

	// Display result
	cv::namedWindow("Detection Result");
	cv::imshow("Detection Result",result1);

	// Load color image
	ColorHistogram hc;
    cv::Mat color= cv::imread("waves.jpg");

	// extract region of interest
	imageROI= color(cv::Rect(0,0,100,45)); // blue sky area

	// Get 3D colour histogram (8 bins per channel)
	hc.setSize(8); // 8x8x8
	cv::Mat shist= hc.getHistogram(imageROI);

	// set histogram to be back-projected
	finder.setHistogram(shist);
	finder.setThreshold(0.05f);

	// Get back-projection of color histogram
	result1= finder.find(color);

	cv::namedWindow("Color Detection Result");
	cv::imshow("Color Detection Result",result1);

	// Second color image
	cv::Mat color2= cv::imread("dog.jpg");

	cv::namedWindow("Second Image");
	cv::imshow("Second Image",color2);

	// Get back-projection of color histogram
	cv::Mat result2= finder.find(color2);

	cv::namedWindow("Result color (2)");
	cv::imshow("Result color (2)",result2);

	// Get ab color histogram
	hc.setSize(256); // 256x256
	cv::Mat colorhist= hc.getabHistogram(imageROI);

	// display 2D histogram
	colorhist.convertTo(tmp,CV_8U,-1.0,255.0);
	cv::namedWindow("ab histogram");
	cv::imshow("ab histogram",tmp);

	// set histogram to be back-projected
	finder.setHistogram(colorhist);
	finder.setThreshold(0.05f);

	// Convert to Lab space
	cv::Mat lab;
	cv::cvtColor(color, lab, CV_BGR2Lab);

	// Get back-projection of ab histogram
	int ch[2]={1,2};
	result1= finder.find(lab,0,256.0f,ch);

	cv::namedWindow("Result ab (1)");
	cv::imshow("Result ab (1)",result1);

	// Second colour image
	cv::cvtColor(color2, lab, CV_BGR2Lab);

	// Get back-projection of ab histogram
	result2= finder.find(lab,0,256.0,ch);

	cv::namedWindow("Result ab (2)");
	cv::imshow("Result ab (2)",result2);

	// Draw a rectangle around the reference sky area
    cv::rectangle(color,cv::Rect(0,0,100,45),cv::Scalar(0,0,0));
	cv::namedWindow("Color Image");
	cv::imshow("Color Image",color);

	
	// Get Hue colour histogram
	hc.setSize(180); // 180 bins
	colorhist= hc.getHueHistogram(imageROI);

	// set histogram to be back-projected
	finder.setHistogram(colorhist);

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

	// Get back-projection of hue histogram
	ch[0]=0;
	result1= finder.find(hsv,0.0f,180.0f,ch);

	cv::namedWindow("Result Hue (1)");
	cv::imshow("Result Hue (1)",result1);

	// Second colour image
	color2= cv::imread("dog.jpg");

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

	// Get back-projection of hue histogram
	result2= finder.find(hsv,0.0f,180.0f,ch);

	cv::namedWindow("Result Hue (2)");
	cv::imshow("Result Hue (2)",result2);

	cv::waitKey();
	return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
/*

    //创建ColorHistogram 对象
    ColorHistogram ch;

    // load the image
    Mat image = imread("/Users/Haoyang/Downloads/Pics/waves.jpg");

    namedWindow("Origianl");
    imshow("Original",image);

    if(image.data) {
        // reduce color
        colorReduce(image,32);
        // set ROI
        Mat blueROI = image(Rect(0,0,165,75));

        namedWindow("ROI");
        imshow("ROI",blueROI);

        // compute the histogram
        cv::MatND hist = ch.getColorHistogram(blueROI);
        // 创建ContentFinder 对象
        ContentFinder *finder = new ContentFinder;
        finder->setHistogram(hist);
        finder->setThreshold(0.05f);

//        std::cout << finder->getThreshold() << std::endl;
        std::cout << finder->getMinValue() << std::endl;
        std::cout << finder->getMaxValue() << std::endl;


        // Get back-projection of color histgram

        cv::Mat result = finder->find(image,finder->getMinValue(),
                                      finder->getMaxValue(),finder->getChannels(),
                                      3);
        namedWindow("Result");
        imshow("Result",result);

    }
    else
        std::cerr << "你妹,读文件出错!" << std::endl;

*/
    /*****************************************************************/
    /********************** MEAN SHIFT ALGORITHM *********************/

    // 读入第一张图片
    Mat image1 = imread("/Users/Haoyang/Downloads/Pics/baboon1.jpg");
    // 获取脸部,作为ROI
    Mat image1ROI = image1(Rect(110,260,35,40));
//    namedWindow("temp",cv::WINDOW_AUTOSIZE);
//    imshow("temp",image1ROI);

    // Get the hue histogram
    int minSat = 65;
    ColorHistogram hc;
    cv::MatND colorhist = hc.getHueHistogram(image1ROI,minSat);
    
    ContentFinder *finder = new ContentFinder;
    // 将得到的脸部的 1D hue histogram作为参数输入,
    // 用来设置ContentFinder 的私有变量
    finder->setHistogram(colorhist);
    
    // 打开第二张图像
    Mat image2 = imread("/Users/Haoyang/Downlaods/Pics/baboon2.jpg");
    // Convert to hsv color space
    Mat hsv;
    cv::cvtColor(image2, hsv,CV_BGR2HSV);
    // split the image
    std::vector<Mat> v;
    cv::split(image2,v);
    // Identify pixels with low saturation
    cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
    
    // next, obtain the back-projection of the hue channel of this image
    // using the previously obtained histogram
    // Get back - projection of hue histogram, using find method
    Mat result = finder->find(hsv,0.0f,180,hc.getChannel(),1);
    // Eliminate low staturation pixels
    cv::bitwise_and(result,v[1],result);

    Rect rect(110,260,35,40);
    cv::rectangle(image2,rect,cv::Scalar(0,0,255));
    TermCriteria criteria(TermCriteria::MAX_ITER,10,0.01);
//    cv::meanShift(result,rect,criteria);
    cv::meanShift(result,rect,criteria);

    namedWindow("Result");
    imshow("Result",result);


    
    return a.exec();
}
int main(){


	ColorHistogtam hc;

	Mat image = imread("baboon1.jpg");
	Mat imageROI = image(Rect(110, 260,35,40));
	rectangle(image, Rect(110, 260, 35, 40), Scalar(0,0,255));

	Mat window(image, Rect(0, 2*image.rows/5, image.cols, image.rows/2));
	namedWindow("Image");
	imshow("Image", window);

	int minSat = 65;

	MatND colorhist = hc.getHueHistogram(imageROI,minSat);
	
	ContentFinder finder;
	finder.setHistogram(colorhist);
	finder.setThreshold(0.2f);

	Mat hsv;
	cvtColor(image, hsv, CV_BGR2HSV);

	vector<Mat> v;
	split(hsv,v);
	threshold(v[1],v[1], minSat, 255, THRESH_BINARY);
	namedWindow("Satruation mask");
	imshow("Satruation mask", v[1]);



	image = imread("baboon4.jpg");
	Mat window2(image, Rect(0, 2*image.rows/5, image.cols, image.rows /2));

	namedWindow("Image 2");
	imshow("Image 2", window2);

	cvtColor(image, hsv, CV_BGR2HSV);

	int ch[1] = {0};
	finder.setThreshold(-1.0f);
	Mat result = finder.find(hsv, 0.0f, 180.0f, ch, 1);

	namedWindow("Backprojection on second image");
	imshow("Backprojection on second image", result);

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

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

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

	Mat window2r(image, Rect(0,2*image.rows/5, image.cols, image.rows/2));

	namedWindow("Image 2 result");
	imshow("image 2 result", window2r);

	waitKey();
}