///////////////////////////////////////////////////////
// Panel::CalibrateCamera() Description
///////////////////////////////////////////////////////
void Panel::CalibrateCamera(string sFilePath)
{
	help();

	//! [file_read]
	Settings s;
	const string inputSettingsFile = sFilePath;
	FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings
	if (!fs.isOpened())
	{
		cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl;
//		return -1;
	}
	fs["Settings"] >> s;
	fs.release();                                         // close Settings file
	//! [file_read]

	//FileStorage fout("settings.yml", FileStorage::WRITE); // write config as YAML
	//fout << "Settings" << s;

	if (!s.goodInput)
	{
		cout << "Invalid input detected. Application stopping. " << endl;
//		return -1;
	}

	vector<vector<Point2f> > imagePoints;
	Mat cameraMatrix, distCoeffs;
	Size imageSize;
	int mode = s.inputType == Settings::IMAGE_LIST ? CAPTURING : DETECTION;
	clock_t prevTimestamp = 0;
	const Scalar RED(0, 0, 255), GREEN(0, 255, 0);
	const char ESC_KEY = 27;
	int counter = 1;

	//! [get_input]
	for (;;)
	{
		Mat view;
		bool blinkOutput = false;

		view = s.nextImage();

		//-----  If no more image, or got enough, then stop calibration and show result -------------
		if (mode == CAPTURING && imagePoints.size() >= (size_t)s.nrFrames)
		{
			if (runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints))
				mode = CALIBRATED;
			else
				mode = DETECTION;
		}
		if (view.empty())          // If there are no more images stop the loop
		{
			// if calibration threshold was not reached yet, calibrate now
			if (mode != CALIBRATED && !imagePoints.empty())
				runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints);
			break;
		}
		//! [get_input]

		imageSize = view.size();  // Format input image.
		if (s.flipVertical)    flip(view, view, 0);

		//! [find_pattern]
		vector<Point2f> pointBuf;

		bool found;
		switch (s.calibrationPattern) // Find feature points on the input format
		{
		case Settings::CHESSBOARD:
			found = findChessboardCorners(view, s.boardSize, pointBuf,
				CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
			break;
		case Settings::CIRCLES_GRID:
			found = findCirclesGrid(view, s.boardSize, pointBuf);
			break;
		case Settings::ASYMMETRIC_CIRCLES_GRID:
			found = findCirclesGrid(view, s.boardSize, pointBuf, CALIB_CB_ASYMMETRIC_GRID);
			break;
		default:
			found = false;
			break;
		}
		//! [find_pattern]
		//! [pattern_found]
		if (found)                // If done with success,
		{
			// improve the found corners' coordinate accuracy for chessboard
			if (s.calibrationPattern == Settings::CHESSBOARD)
			{
				Mat viewGray;
				cvtColor(view, viewGray, COLOR_BGR2GRAY);
				cornerSubPix(viewGray, pointBuf, Size(11, 11),
					Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
			}

			if (mode == CAPTURING &&  // For camera only take new samples after delay time
				(!s.inputCapture.isOpened() || clock() - prevTimestamp > s.delay*1e-3*CLOCKS_PER_SEC))
			{
				imagePoints.push_back(pointBuf);
				prevTimestamp = clock();
				blinkOutput = s.inputCapture.isOpened();
			}

			// Draw the corners.
			drawChessboardCorners(view, s.boardSize, Mat(pointBuf), found);
		}
		//! [pattern_found]
		//----------------------------- Output Text ------------------------------------------------
		//! [output_text]
		string msg = (mode == CAPTURING) ? "100/100" :
			mode == CALIBRATED ? "Calibrated" : "Press 'g' to start";
		int baseLine = 0;
		Size textSize = getTextSize(msg, 1, 1, 1, &baseLine);
		Point textOrigin(view.cols - 2 * textSize.width - 10, view.rows - 2 * baseLine - 10);

		if (mode == CAPTURING)
		{
			if (s.showUndistorsed)
				msg = format("%d/%d Undist", (int)imagePoints.size(), s.nrFrames);
			else
				msg = format("%d/%d", (int)imagePoints.size(), s.nrFrames);
		}

		putText(view, msg, textOrigin, 1, 1, mode == CALIBRATED ? GREEN : RED);

		if (blinkOutput)
			bitwise_not(view, view);
		//! [output_text]
		//------------------------- Video capture  output  undistorted ------------------------------
		//! [output_undistorted]
		if (mode == CALIBRATED && s.showUndistorsed)
		{
			Mat temp = view.clone();
			undistort(temp, view, cameraMatrix, distCoeffs);
		}
		//! [output_undistorted]
		//------------------------------ Show image and check for input commands -------------------
		//! [await_input]
		
		namedWindow("Image View" + to_string(counter), WINDOW_NORMAL);
		resizeWindow("Image View" + to_string(counter), 640, 480);
		imshow("Image View" + to_string(counter), view);
		char key = (char)waitKey(s.inputCapture.isOpened() ? 50 : s.delay);

		cout << "Image " << to_string(counter) << " Completed" << endl;
		counter++;

		if (key == ESC_KEY)
			break;

		if (key == 'u' && mode == CALIBRATED)
			s.showUndistorsed = !s.showUndistorsed;

		if (s.inputCapture.isOpened() && key == 'g')
		{
			mode = CAPTURING;
			imagePoints.clear();
		}
		//! [await_input]
	}

	// -----------------------Show the undistorted image for the image list ------------------------
	//! [show_results]
	if (s.inputType == Settings::IMAGE_LIST && s.showUndistorsed)
	{
		Mat view, rview, map1, map2;
		initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
			getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
			imageSize, CV_16SC2, map1, map2);

		m_mainMap1 = map1;
		m_mainMap2 = map2;

		for (size_t i = 0; i < s.imageList.size(); i++)
		{
			view = imread(s.imageList[i], 1);
			if (view.empty())
				continue;
			remap(view, rview, map1, map2, INTER_LINEAR);
			imshow("Image View", rview);
			char c = (char)waitKey();
			if (c == ESC_KEY || c == 'q' || c == 'Q')
				break;
		}
	}
	//! [show_results]

//	return 0;

}
///////////////////////////////////////////////////////////////////
// Panel::MaskWithColor() 
// Description:
///////////////////////////////////////////////////////////////////
void Panel::MaskWithColor(string sImgPath, string color, bool debug)
{
	Mat HSV, Mask, Mask1, Mask2, MaskResult;
	// Show the original image
	if(!ShowImage(sImgPath, "Original"))
		return;
	Mat image;
	if (m_roi.width && m_roi.width <= m_Image.cols && m_roi.height && m_roi.height <= m_Image.rows)
		image = m_pPanel->m_Image(m_roi);
	else
		image = m_pPanel->m_Image;
	// Convert the color HSV Format
	cvtColor(image, HSV, CV_BGR2HSV);
	// The inRange() function will create a mask with 
	// only the pixels of color in the range specified
	if (color == "Blue")
	{
		inRange(HSV, Scalar(105, 100, 30), Scalar(131, 255, 255), Mask);
	}
	else if (color == "Red")
	{
		inRange(HSV, Scalar(0, 100, 30), Scalar(6, 255, 255), Mask1);
		inRange(HSV, Scalar(165, 100, 30), Scalar(179, 255, 255), Mask2);
		bitwise_or(Mask1, Mask2, Mask);
	}
	else if (color == "panel")
	{
		// test code
		int h1Lo = 0, s1Lo = 55, v1Lo = 115;
		int h1Hi = 20, s1Hi = 120, v1Hi = 210;
		int h2Lo = 0, s2Lo = 0, v2Lo = 0;
		int h2Hi = 0, s2Hi = 0, v2Hi = 0;
		namedWindow("InRange Tester", CV_WINDOW_NORMAL);
		cvCreateTrackbar("Hue Lo 1:", "InRange Tester", &h1Lo, 180);
		cvCreateTrackbar("Hue Hi 1:", "InRange Tester", &h1Hi, 180);
		cvCreateTrackbar("Sat Lo 1", "InRange Tester", &s1Lo, 255);
		cvCreateTrackbar("Sat Hi 1", "InRange Tester", &s1Hi, 255);
		cvCreateTrackbar("Val Lo 1", "InRange Tester", &v1Lo, 255);
		cvCreateTrackbar("Val Hi 1", "InRange Tester", &v1Hi, 255);
		cvCreateTrackbar("Hue Lo 2:", "InRange Tester", &h2Lo, 180);
		cvCreateTrackbar("Hue Hi 2:", "InRange Tester", &h2Hi, 180);
		cvCreateTrackbar("Sat Lo 2", "InRange Tester", &s2Lo, 255);
		cvCreateTrackbar("Sat Hi 2", "InRange Tester", &s2Hi, 255);
		cvCreateTrackbar("Val Lo 2", "InRange Tester", &v2Lo, 255);
		cvCreateTrackbar("Val Hi 2", "InRange Tester", &v2Hi, 255);

		while (true)
		{
			inRange(HSV, Scalar(h1Lo, s1Lo, v1Lo), Scalar(h1Hi, s1Hi, v1Hi), Mask1);
			inRange(HSV, Scalar(h2Lo, s2Lo, v2Lo), Scalar(h2Hi, s2Hi, v2Hi), Mask2);
			bitwise_or(Mask1, Mask2, Mask);

			image.copyTo(MaskResult, Mask);
			namedWindow("Mask Result", CV_WINDOW_NORMAL);
			imshow("Mask Result", MaskResult);
			if (waitKey(50) == 27)
				break;
		}
	}
	image.copyTo(MaskResult, Mask);

	if (color == "Red" || color == "Blue")
	{
		// Use the below lines to debug red or blue color mask 
		// namedWindow("Mask Result", CV_WINDOW_AUTOSIZE);
		// imshow("Mask Result", MaskResult);
		DetectBlob(MaskResult, debug);
	}
}
Example #3
0
void GazeTracker::draw(Mat& image)
{
	Mat faceEyesInfo;
	Mat focus;
	
	double t = (double)cvGetTickCount();//用来计算算法执行时间
	detector.detectFaceAndEyes(image, 1);
	t = (double)cvGetTickCount() - t;//相减为算法执行的时间  
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) ); 

	if(detector.isRecorded || detector.isFaceDetected)
	{
		faceEyesInfo = detector.getFaceAndEyePos();
	}
	else
	{
		faceEyesInfo.data = NULL;
	}

	if(isClicked == true)
	{
		float s[1][2];
		s[0][0] = click.x;
		s[0][1] = click.y;
		Mat output(1, 2, CV_32FC1, s);

		if(faceEyesInfo.data == NULL)
		{
			cout << "fail to detect eyes!" << endl;
		}

		else
		{
//			focusFinder.train(faceEyesInfo, SampleTypes::ROW_SAMPLE, output);
//			cout << "eyes detected and trained!" << endl;
//			focusFinder.save(MODULEPATH);

			//写入记录
			recorder << detector.faceCenter.x << ' ' << detector.faceCenter.y << ' '
					 << detector.faceSize.x << ' '
					 << detector.leftEyeCenter.x << ' ' << detector.leftEyeCenter.y << ' '
					 << detector.leftEyeSize.x << ' '
					 << detector.rightEyeCenter.x << ' ' << detector.rightEyeCenter.y << ' '
					 << detector.rightEyeSize.x << ' ' 
					 << s[0][0] << ' ' << s[0][1] << endl;

			cout << detector.faceCenter.x << ' ' << detector.faceCenter.y << ' '
				 << detector.faceSize.x << ' '
				 << detector.leftEyeCenter.x << ' ' << detector.leftEyeCenter.y << ' '
				 << detector.leftEyeSize.x << ' '
				 << detector.rightEyeCenter.x << ' ' << detector.rightEyeCenter.y << ' '
				 << detector.rightEyeSize.x << ' ' 
				 << s[0][0] << ' ' << s[0][1] << endl;

		}
	}

	isClicked = false;

	if(detector.isRecorded)
	{
		int faceRadius, leftEyeRadius, rightEyeRadius;

		faceRadius = 1;
		circle(image, Point(detector.faceCenter.x, detector.faceCenter.y),
				faceRadius, CV_RGB(0,0,255), 3, 8, 0);

		leftEyeRadius = (detector.leftEyeSize.x + detector.leftEyeSize.y) / 3;
		leftEyeRadius = 1;
		circle(image, Point(detector.leftEyeCenter.x, detector.leftEyeCenter.y),
				leftEyeRadius, CV_RGB(0,0,255), 3, 8, 0);

		rightEyeRadius = (detector.rightEyeSize.x + detector.rightEyeSize.y) / 3;
		rightEyeRadius = 1;
		circle(image, Point(detector.rightEyeCenter.x, detector.rightEyeCenter.y),
				rightEyeRadius, CV_RGB(0,0,255), 3, 8, 0);
	}

	else if(detector.isFaceDetected)
	{
		int faceRadius = (detector.faceSize.x + detector.faceSize.y) / 3;
		circle(image, Point(detector.faceCenter.x, detector.faceCenter.y),
				faceRadius, CV_RGB(0,0,255), 3, 8, 0);
	}

	Mat bigImg(800, 1100, CV_8UC3);
	resize(image, bigImg, bigImg.size());
	imshow(windowName, bigImg);
	waitKey(1);
	
}
void PoseEstimator::compute(vector<Point>& points_in)
{
	points_current = points_in;
	// compute_optimized();

	string pose_name_dist_min = "";
	float dist_min = 9999;

	int index = -1;
	for (vector<Point>& points : points_collection)
	{
		++index;

		Mat cost_mat = compute_cost_mat(points_current, points, false);
		float dist = compute_dtw(cost_mat);

		if (dist < dist_min)
		{
			dist_min = dist;
			PoseEstimator::points_dist_min = points;
			pose_name_dist_min = names_collection[index];

			if (labels_collection.size() > index)
				PoseEstimator::labels_dist_min = labels_collection[index];
		}
	}

	bool boolean0 = record_pose;
	bool boolean1 = target_pose_name != "";
	bool boolean2 = points_current.size() > 500;

	if ((boolean0 && boolean1 && boolean2) || force_record_pose)
	{
		force_record_pose = false;
		save(target_pose_name);
		cout << pose_name_dist_min << "->" << target_pose_name << " " << to_string(dist_min) << endl;
	}

	if (dist_min != 9999)
	{
		Mat image_dist_min = Mat::zeros(HEIGHT_SMALL, WIDTH_SMALL, CV_8UC3);

		static vector<Scalar> colors;
		if (colors.size() == 0)
		{
			colors.push_back(Scalar(255, 0, 0));
			colors.push_back(Scalar(0, 153, 0));
			colors.push_back(Scalar(0, 0, 255));
			colors.push_back(Scalar(153, 0, 102));
			colors.push_back(Scalar(102, 102, 102));
		}

		int label_indexes[1000];
		int label_indexes_count = 0;

		{
			int label_index = -1;
			for (Point& pt : PoseEstimator::labels_dist_min)
			{
				++label_index;
				for (int i = pt.x; i <= pt.y; ++i)
				{
					label_indexes[i] = label_index;
					++label_indexes_count;
				}
			}
		}
		{
			int index = -1;
			Point pt_old = Point(-1, -1);
			for (Point& pt : PoseEstimator::points_dist_min)
			{
				++index;
				if (index >= label_indexes_count)
					continue;

				int label_index = label_indexes[index];

				if (pt_old.x != -1)
					line(image_dist_min, pt, pt_old, colors[label_index], 1);

				pt_old = pt;
			}
		}

		if (show)
			imshow("image_dist_min", image_dist_min);
	}

	string pose_name_temp;
	accumulate_pose(pose_name_dist_min, 10, pose_name_temp);

	if (pose_name_temp != "")
		pose_name = pose_name_temp;

	if (show)
		cout << pose_name_temp << endl;

	Mat image_current = Mat::zeros(HEIGHT_SMALL, WIDTH_SMALL, CV_8UC1);

	Point pt_old = Point(-1, -1);
	for (Point& pt : points_current)
	{
		if (pt_old.x != -1)
			line(image_current, pt, pt_old, Scalar(254), 1);

		pt_old = pt;
	}

	if (show)
	{
		imshow("image_current", image_current);
		waitKey(1);
	}
}
void compute_optimized()
{
	const int divisions = 5;
	const int generations = 10;
	const int initial_seed_num = 10;
	const int min_seed_num = 1;
	const int max_seed_num = 10;

	const int items_in_each_division = points_collection.size() / divisions;
	const int points_collection_size = points_collection.size();

	int total_iterations = 0;

	bool checker[1000] { 0 };
	unordered_map<int, float> division_dist_min_checker;
	unordered_map<float, int> dist_min_division_checker;
	unordered_map<float, bool> dist_min_checker;
	unordered_map<float, int> dist_min_index_checker;

	float dist_min_selected = 9999;
	vector<Point> database_points_selected;

	for (int i = 0; i < generations; ++i)
	{
		for (int a = 0; a < (points_collection_size - 1); a += items_in_each_division)
		{
			int index_min = a;
			int index_max = a + items_in_each_division;

			vector<int> seed_indexes;
			for (int b = index_min; b <= index_max; ++b)
				seed_indexes.push_back(b);

			if (seed_indexes.size() == 0)
				continue;

			int seed_num = initial_seed_num;
			if (i > 0)
			{
				float dist_min = division_dist_min_checker[a];
				int index_division = dist_min_index_checker[dist_min];
				seed_num = map_val(index_division, 0, divisions - 1, min_seed_num, max_seed_num);
			}

			for (int b = 0; b < seed_num; ++b)
			{
				int k = get_random(0, seed_indexes.size() - 1);
				int seed_index = seed_indexes[k];

				if (checker[seed_index] == true)
					continue;

				checker[seed_index] = true;

				vector<Point> database_points = points_collection[seed_index];
				float dist = compute_dtw_dist(points_current, database_points);

				if (dist < dist_min_selected)
				{
					dist_min_selected = dist;
					database_points_selected = database_points;
				}

				++total_iterations;

				float dist_min = division_dist_min_checker.count(a) > 0 ? division_dist_min_checker[a] : 9999;
				if (dist >= dist_min)
					continue;

				dist_min = dist;
				while (dist_min_checker.count(dist_min) > 0)
					dist_min += 0.001;

				dist_min_checker[dist_min] = true;
				division_dist_min_checker[a] = dist_min;
				dist_min_division_checker[dist_min] = a;
			}
		}

		vector<float> dist_min_vec;
		for (int a = 0; a < (points_collection_size - 1); a += items_in_each_division)
			dist_min_vec.push_back(division_dist_min_checker[a]);

		std::sort(dist_min_vec.begin(), dist_min_vec.end(), decreasing());

		int index = -1;
		for (float& dist_min : dist_min_vec)
		{
			++index;
			dist_min_index_checker[dist_min] = index;
		}
	}

	Mat image_haha = Mat::zeros(HEIGHT_SMALL, WIDTH_SMALL, CV_8UC1);
	draw_contour(database_points_selected, image_haha, 254, 1, 0);
	imshow("image_haha", image_haha);

	waitKey(1);

	cout << total_iterations << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
// 	initModule_nonfree();//if use SIFT or SURF  
// 	Ptr<FeatureDetector> detector = FeatureDetector::create( "SIFT" );  
// 	Ptr<DescriptorExtractor> descriptor_extractor = DescriptorExtractor::create( "SIFT" );  
// 	Ptr<DescriptorMatcher> descriptor_matcher = DescriptorMatcher::create( "BruteForce" );  
// 	if( detector.empty() || descriptor_extractor.empty() )  
// 		throw runtime_error("fail to create detector!");  
// 
// 	Mat img1 = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test3.jpg");  
// 	Mat img2 = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test2.jpg");  
// 
// 	//detect keypoints;  
// 	vector<KeyPoint> keypoints1,keypoints2;  
// 	detector->detect( img1, keypoints1 );  
// 	detector->detect( img2, keypoints2 );  
// 	cout <<"img1:"<< keypoints1.size() << " points  img2:" <<keypoints2.size()   
// 		<< " points" << endl << ">" << endl;  
// 
// 	//compute descriptors for keypoints;  
// 	cout << "< Computing descriptors for keypoints from images..." << endl;  
// 	Mat descriptors1,descriptors2;  
// 	descriptor_extractor->compute( img1, keypoints1, descriptors1 );  
// 	descriptor_extractor->compute( img2, keypoints2, descriptors2 );  
// 
// 	cout<<endl<<"Descriptors Size: "<<descriptors2.size()<<" >"<<endl;  
// 	cout<<endl<<"Descriptor's Column: "<<descriptors2.cols<<endl  
// 		<<"Descriptor's Row: "<<descriptors2.rows<<endl;  
// 	cout << ">" << endl;  
// 
// 	//Draw And Match img1,img2 keypoints  
// 	Mat img_keypoints1,img_keypoints2;  
// 	drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);  
// 	drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);  
// 	imshow("Box_in_scene keyPoints",img_keypoints1);  
// 	imshow("Box keyPoints",img_keypoints2);  
// 
// 	descriptor_extractor->compute( img1, keypoints1, descriptors1 );    
// 	vector<DMatch> matches;  
// 	descriptor_matcher->match( descriptors1, descriptors2, matches );  
// 
// 	Mat img_matches;  
// 	drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);  
// 
// 	imshow("Mathc",img_matches);  
// 	waitKey(0);  
// 	return 0;  
// 	return 0;
	Mat image = imread("E:\\DayBreakZcs\\ImageProcessing\\source\\test4.jpg");  
	Mat descriptors;  
	vector<KeyPoint> keypoints;  
	SimpleBlobDetector::Params params;  
	params.minThreshold = 10;  
	params.maxThreshold = 100;  
	params.thresholdStep = 10;  
	params.minArea = 10;   
	params.minConvexity = 0.3;  
	params.minInertiaRatio = 0.01;  
	params.maxArea = 8000;  
	params.maxConvexity = 10;  
	params.filterByColor = false;  
	params.filterByCircularity = false;  
	SimpleBlobDetector blobDetector( params );  
	blobDetector.create("SimpleBlob");  
	blobDetector.detect( image, keypoints );  
	drawKeypoints(image, keypoints, image, Scalar(255,0,0));  
	imshow("Mathc",image);  
	waitKey(0); 
}