Example #1
0
int main()
{
	
	double time;
	CvScalar hsv_min;
	CvScalar hsv_max;

#ifdef VIDEO
	CvCapture* capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Table Tennis.flv");
	//CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Test\\motionTracking(ZC)\\bouncingBall.avi");
	//CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\PingPongTest.avi");
	//CvCapture *capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\WIN_20150803_103818.MP4");//WIN_20150803_102356.MP4
#else
	CvCapture* capture = cvCreateCameraCapture(0);
#endif
	if (!capture)
	{
		printf("ERROR ACQUIRING VIDEO FEED\n");
		getchar();
		return -1;
	}

	CvSize size = cvSize(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
	IplImage* frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
	IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
	IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1);
	cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5, 0, 1, 1);


	while (1)
	{
#ifdef VIDEO

		capture = cvCreateFileCapture("E:\\Evan Lin\\CLUB\\PingPong\\OPENCV\\Table Tennis.flv");
		if (!capture)
		{
			printf("ERROR ACQUIRING VIDEO FEED\n");
			getchar();
			return -1;
		}
		while (cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES) < cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT) - 1)
		{
			time = (double)cvGetTickCount();
			frame = cvQueryFrame(capture);
			if (!frame)
			{
				fprintf(stderr, "ERROR: frame is null...\n");
				getchar();
				exit(1);
			}
			cvCvtColor(frame, hsv_frame, CV_BGR2HSV);
			hsv_min = cvScalar(H_MIN, S_MIN, S_MIN, 0);
			hsv_max = cvScalar(H_MAX, S_MAX, V_MAX, 0);
			cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded);
			cvErode(thresholded, thresholded, NULL, 1);
			cvDilate(thresholded, thresholded, NULL, 1);
			//cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9);

			if (debugMode == true)
			{
				createTrackbars();
				cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE);
				cvShowImage("HSV", thresholded);
			}
			else
			{
				cvDestroyWindow("trackbarControl");
				cvDestroyWindow("HSV");
			}

			if (trackingEnabled)
			{
				searchForMovement(thresholded, frame);
			}

			if (showEnabled)
			{
				time = ((double)cvGetTickCount() - time) / cvGetTickFrequency();
				double fps = 100000.0 / time;
				char str[20];
				sprintf(str, "FPS:%.2f", fps);
				cvPutText(frame, str, cvPoint(0, 30), &font, cvScalar(255, 0, 0));
			}

			cvShowImage("Frame1", frame);

			switch (cvWaitKey(33))
			{
			case 27: //'esc' key has been pressed, exit program.
				//cvReleaseMemStorage();
				cvReleaseCapture(&capture);
				cvDestroyAllWindows();
				return 0;
			case 116: //'t' has been pressed. this will toggle tracking
				trackingEnabled = !trackingEnabled;
				if (trackingEnabled == false) printf("Tracking disabled.\n");
				else printf("Tracking enabled.\n");
				break;
			case 100: //'d' has been pressed. this will debug mode
				debugMode = !debugMode;
				if (debugMode == false) printf("Debug mode disabled.\n");
				else printf("Debug mode enabled.\n");
				break;
			case 115: //'s' has been pressed. this will debug mode
				showEnabled = !showEnabled;
				if (showEnabled == false) printf("Show mode disabled.\n");
				else printf("Show mode enabled.\n");
				break;
			case 112: //'p' has been pressed. this will pause/resume the code.
				pause = !pause;
				if (pause == true)
				{
					printf("Code paused, press 'p' again to resume\n");
					while (pause == true)
					{
						//stay in this loop until 
						switch (cvWaitKey())
						{
						case 112:
							//change pause back to false
							pause = false;
							printf("Code resumed.\n");
							break;
						}
					}
				}
			}
		}
	}
#else
		time = (double)GetTickCount();
		//time = (double)cvGetTickCount();
		frame = cvQueryFrame(capture);
		if (!frame)
		{
			fprintf(stderr, "ERROR: frame is null...\n");
			getchar();
			exit(1);
		}
		cvCvtColor(frame, hsv_frame, CV_BGR2HSV);
		hsv_min = cvScalar(H_MIN, S_MIN, S_MIN, 0);
		hsv_max = cvScalar(H_MAX, S_MAX, V_MAX, 0);
		cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded);
		cvErode(thresholded, thresholded, NULL, 1);
		cvErode(thresholded, thresholded, NULL, 1);
		cvErode(thresholded, thresholded, NULL, 1);
		cvDilate(thresholded, thresholded, NULL, 1);
		//cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9);

		if (debugMode == true)
		{
			createTrackbars();
			cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE);
			cvShowImage("HSV", thresholded);
		}
		else
		{
			cvDestroyWindow("trackbarControl");
			cvDestroyWindow("HSV");
		}

		if (trackingEnabled)
		{
			searchForMovement(thresholded, frame);
		}

		if (showEnabled)
		{
			//time = ((double)cvGetTickCount() - time) / cvGetTickFrequency();
			//double fps = 100000.0 / time;
			time = (double)GetTickCount() - time;
			double fps = 1000.0 / time;
			char str[20];
			sprintf(str, "FPS:%.2f", fps);
			cvPutText(frame, str, cvPoint(0, 30), &font, cvScalar(255, 0, 0));
		}

		cvShowImage("Frame1", frame);

		
		}
int main_track(){

	//some boolean variables for added functionality
	bool objectDetected = false;
	//these two can be toggled by pressing 'd' or 't'
	bool debugMode = false;
	bool trackingEnabled = false;
	//pause and resume code
	bool pause = false;
	//set up the matrices that we will need
	//the two frames we will be comparing
	Mat frame1,frame2;
	//their grayscale images (needed for absdiff() function)
	Mat grayImage1,grayImage2;
	//resulting difference image
	Mat differenceImage;
	//thresholded difference image (for use in findContours() function)
	Mat thresholdImage;
	//video capture object.
	VideoCapture capture;
	capture.open(0);	
	if(!capture.isOpened()){
			cout<<"ERROR ACQUIRING VIDEO FEED\n";
			getchar();
			return -1;
		}
	while(1){

		//we can loop the video by re-opening the capture every time the video reaches its last frame

		//capture.open("C:\\Users\\Ran_the_User\\Documents\\GitHub\\3Drobot\\camera3D\\src\\externals\\bouncingBall.avi");
	
	

		//check if the video has reach its last frame.
		//we add '-1' because we are reading two frames from the video at a time.
		//if this is not included, we get a memory error!
		//while(capture.get(CV_CAP_PROP_POS_FRAMES)<capture.get(CV_CAP_PROP_FRAME_COUNT)-1)
		{

			//read first frame
			capture.read(frame1);
			//convert frame1 to gray scale for frame differencing
			cv::cvtColor(frame1,grayImage1,COLOR_BGR2GRAY);
			//copy second frame
			capture.read(frame2);
			//convert frame2 to gray scale for frame differencing
			cv::cvtColor(frame2,grayImage2,COLOR_BGR2GRAY);
			//perform frame differencing with the sequential images. This will output an "intensity image"
			//do not confuse this with a threshold image, we will need to perform thresholding afterwards.
			cv::absdiff(grayImage1,grayImage2,differenceImage);
			//threshold intensity image at a given sensitivity value
			cv::threshold(differenceImage,thresholdImage,SENSITIVITY_VALUE,255,THRESH_BINARY);
			if(debugMode==true){
				//show the difference image and threshold image
				cv::imshow("Difference Image",differenceImage);
				cv::imshow("Threshold Image", thresholdImage);
			}else{
				//if not in debug mode, destroy the windows so we don't see them anymore
				cv::destroyWindow("Difference Image");
				cv::destroyWindow("Threshold Image");
			}
			//blur the image to get rid of the noise. This will output an intensity image
			cv::blur(thresholdImage,thresholdImage,cv::Size(BLUR_SIZE,BLUR_SIZE));
			//threshold again to obtain binary image from blur output
			cv::threshold(thresholdImage,thresholdImage,SENSITIVITY_VALUE,255,THRESH_BINARY);
			if(debugMode==true){
				//show the threshold image after it's been "blurred"

				imshow("Final Threshold Image",thresholdImage);

			}
			else {
				//if not in debug mode, destroy the windows so we don't see them anymore
				cv::destroyWindow("Final Threshold Image");
			}

			//if tracking enabled, search for contours in our thresholded image
			if(trackingEnabled){

				searchForMovement(thresholdImage,frame1);
			}

			//show our captured frame
			imshow("Frame1",frame1);
			//check to see if a button has been pressed.
			//this 10ms delay is necessary for proper operation of this program
			//if removed, frames will not have enough time to referesh and a blank 
			//image will appear.
			switch(waitKey(10)){

			case 27: //'esc' key has been pressed, exit program.
				return 0;
			case 116: //'t' has been pressed. this will toggle tracking
				trackingEnabled = !trackingEnabled;
				if(trackingEnabled == false) cout<<"Tracking disabled."<<endl;
				else cout<<"Tracking enabled."<<endl;
				break;
			case 100: //'d' has been pressed. this will debug mode
				debugMode = !debugMode;
				if(debugMode == false) cout<<"Debug mode disabled."<<endl;
				else cout<<"Debug mode enabled."<<endl;
				break;
			case 112: //'p' has been pressed. this will pause/resume the code.
				pause = !pause;
				if(pause == true){ cout<<"Code paused, press 'p' again to resume"<<endl;
				while (pause == true){
					//stay in this loop until 
					switch (waitKey()){
						//a switch statement inside a switch statement? Mind blown.
					case 112: 
						//change pause back to false
						pause = false;
						cout<<"Code Resumed"<<endl;
						break;
					}
				}
				}


			}


		}
		//release the capture before re-opening and looping again.
		//capture.release();
	}

	return 0;

}