Ejemplo n.º 1
0
void LKTracker::ShowMotion(cv::Mat& image)
{
	MotionVector::iterator iter;

	// TRUE- 1 is Right 	|	 FALSE- 0 is Left
	int motionR, motionL, motionLast, motionLongestL, motionLongestR;
	double motionSumL, motionSumR;

	for(iter = this->regions.begin(); iter != this->regions.end(); ++iter)
	{
		Motion *motion = (*iter);

		motionR = 0;
		motionL = 0;
		motionLast = 0;
		motionLongestR = 0;
		motionLongestL = 0;
		motionSumL = 0;
		motionSumR = 0;

		cv::Rect origin = motion->getRect();

		cv::rectangle(image, origin, CV_RGB(255, 0, 255), 2);

		for(int i = 0; i < motion->getVx().rows; i++)
		{
			for(int j = 0; j < motion->getVx().cols; j++)
			{
				double x_component = motion->getVx().at<double>(i,j);
				double y_component = motion->getVy().at<double>(i,j);

				//std::cout << "vx " << x_component << " vy " << y_component << std::endl;

				cv::Point p1 = cv::Point(j+origin.x, i+origin.y);
				cv::Point p2 = cv::Point(j+x_component+origin.x, i + y_component+origin.y);

				// distance ?????
				// if(cv::norm(px1-px2) < magnitude_treshold || cv::norm(py1-py2) < magnitude_treshold)
				
				if(cv::norm(p1-p2) >= magnitude_treshold)
				{
					// Draw the vector
					cv::circle ( image , p1 , 4 , cv::Scalar(0,255,0) , 2 , 8 );
					cv::line(image, p1, p2, CV_RGB(255, 0, 0), 2);
					cv::circle ( image , p2 , 1 , cv::Scalar(0,255,0) , 2 , 8 );
				}

				if(cv::norm(p1-p2) < 5)
				{
					continue;
				}

				if(detectMotion(p1, p2) == 1) {
					motionSumR += std::abs(x_component);
					motionR++;
				} else if(detectMotion(p1, p2) == 0) {
					motionSumL += std::abs(x_component);
					motionL++;
				}

				/*
				// detect gestures
				if(detectMotion(p1, p2) == 1 && motionLast == 1)
				{
					++motionR;
					motionLast = 1;
				}
				else if (detectMotion(p1, p2) == 1 && motionLast == 0)
				{
					motionR = 1;
					motionLast = 1;

					// check if last sequence of LEFTS- 0 was longest one
					if (motionLongestL < motionL)
					{
						motionLongestL = motionL;
					}
				}
				else if(detectMotion(p1, p2) == 0 && motionLast == 0)
				{
					++motionL;
					motionLast = 0;
				}
				else if(detectMotion(p1, p2) == 0 && motionLast == 1)
				{
					motionL = 1;
					motionLast = 0;

					// check if last sequence of RIGHTS- 1 was longest one
					if (motionLongestR < motionR)
					{
						motionLongestR = motionR;
					}
				}
				*/
				
			}
		}

		// print detected motion
		/*if (motionLongestR>motionLongestL)
			std::cout << "RIGHT" << std::endl;
		else if (motionLongestR<motionLongestL)
			std::cout << "LEFT" << std::endl;

			*/
		double av_motion_l = motionSumL / motionL;
		double av_motion_r = motionSumR / motionR;

		cv::Point p(origin.x+50, origin.y+50);
		if (av_motion_r>av_motion_l) {
			cv::putText(image, "R", p, cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,255,0), 3);
		}
		else if (av_motion_r<av_motion_l) {
			cv::putText(image, "L", p, cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,255,0), 3);
		}
	}
}