/*
* processFrame
*
* creates a threshold image of frame, splits the thresholded image into left and
* right halves and then tracks for the configured color in each half, setting the
* left and right paddle positions accordingly
*
* preconditions:	frame must be a valid Mat object representing a single frame from
*					from a VideoCapture object
* postconditions:	sets left and right paddles according to color detected in the
*					left and right halves of the frame, respectively
*/
void ColorPaddleDetector::processFrame(Mat &frame)
{
	flip(frame, frame, 1);

	Mat thres;
	createThresholdImg(frame, thres);

	// create left and right threshold images for seperate color detection in
	// left and right sides of the frame
	int x = thres.cols / 2;
	int y = thres.rows;
	Mat thresholdLeft(thres, Rect(0, 0, 320, 480));
	Mat thresholdRight(thres, Rect(320, 0, 320, 480));

	// detect motion in the left and right frames 
	detectMotion(thresholdLeft, frame, IS_RED);
	detectMotion(thresholdRight, frame, IS_BLUE);
}
/*
* processFrame
*
* uses sequential images to detect motion in the left and right halves of the frame.
*
* preconditions:	frame must be a valid Mat object representing a single frame from 
*					from a VideoCapture object
* postconditions:	sets left and right paddles according to motion detected in the
*					left and right halves of the frame, respectively
*/
void MotionPaddleDetector::processFrame(Mat& frame) {
	Mat frame2, gray, gray2, thres, diff;

	// use sequential images (frame and frame2) for motion detection

	// read in frame and convert to grayscale
	m_vid->read(frame);
	flip(frame, frame, 1);
	cvtColor(frame, gray, COLOR_BGR2GRAY);

	// read in frame2 and convert to grayscale
	m_vid->read(frame2);
	flip(frame2, frame2, 1);
	cvtColor(frame2, gray2, COLOR_BGR2GRAY);

	// create difference image of frame1 and frame2 after being converted to
	// grayscale images
	absdiff(gray, gray2, diff);

	// threshold difference
	threshold(diff, thres, THRESHOLD_SENSITIVITY, 255, THRESH_BINARY);

	// blur the image. output will be an intensity image
	blur(thres, thres, cv::Size(BLUR_SIZE, BLUR_SIZE));

	// threshold intensity image to get binary image (after blurring)
	threshold(thres, thres, THRESHOLD_SENSITIVITY, 255, THRESH_BINARY);

	// split threshold (now binary image) into left and right halves
	int x = thres.cols / 2;
	int y = thres.rows;
	Mat thresholdLeft(thres, Rect(0, 0, x, y));
	Mat thresholdRight(thres, Rect(x, 0, x, y));

	// detect motion in each half of the binary image
	detectMotion(thresholdLeft, frame, IS_RED);
	detectMotion(thresholdRight, frame, IS_BLUE);
}