OPENCV Gesture Action Recognition - Rock Paper Scissors


As an OpenCV beginner, in order to exercise my ability, I decided to write a gesture recognition program myself. The result is better than the program found on the Internet, so I share it~~

To get a better result, I used background subtraction. The first frame of the camera is taken as the background, and the first frame is subtracted from all subsequent frames, which greatly reduces the impurities.

The matchShapes () function used by the comparison method , which was introduced in the previous blog, will not be discussed here. You can refer to https://blog.csdn.net/wangshuai610/article/details/79913600Click to open the link

Use the return value obtained by the matchShapes () function to make judgments.

Do the same processing for all images, binarization ----> median filter ---> erosion ---> dilation ---> find contours, and then use the matchShapes () function between each frame image and the template image to match. The final result is judged. Complete code download link https://download.csdn.net/myClick to open the link

<textarea readonly="readonly" name="code" class="c++"> void main()
{	 
    VideoCapture cap(0);
    if(!cap.isOpened()) //Check whether the opening is successful
         return;
    Mat frame;
	Mat background;
	Mat dst;
	int count=0;
    while(1)
    {
        cap>>frame;
        if(!frame.empty())
        {
			count++;
			if(count==1)
				background = frame.clone(); //Extract the first frame as the background frame
            //imshow("video", frame);
			dst = MoveDetect(background, frame);
			imshow("result", dst);
		</textarea>
<textarea readonly="readonly" name="code" class="c++"> //Process three templates
	Mat cloth= imread("cloth.bmp");
	cvtColor(cloth, cloth, CV_BGR2GRAY);
	threshold(cloth, cloth, 40, 255, CV_THRESH_BINARY);
	vector<vector<Point>> contours1;
	vector<Vec4i> hierarcy1;	
	findContours(cloth, contours1, hierarcy1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);</textarea>
<textarea readonly="readonly" name="code" class="c++">
threshold(diff, diff, 70, 255, CV_THRESH_BINARY);
	//imshow("threshold", diff);
 
	Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
	Mat element2 = getStructuringElement(MORPH_RECT, Size(10,10));

	medianBlur(diff, diff, 5);//Filter parameters are adjusted according to your own environment
	//imshow("medianBlur", diff);

	/*blur(diff, diff, Size(10, 10));
	imshow("blur", diff);*/

	erode(diff, diff, element);
	//imshow("erode", diff);
 
	dilate(diff, diff, element2);//The expansion parameters are adjusted according to your own environment
	imshow("dilate", diff);
	
	vector<vector<Point>> contours;  
	vector<Vec4i> hierarcy;
	findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //find contours
	
	//drawContours(result, contours, -1, Scalar(0,0,255), 2, 8); //draw contours

  
	for(int i=0;i<contours.size();i++)//Compare the matching degree with the three templates respectively
	{
		double matchrate1=matchShapes(contours1[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
		cout<<"index1="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate1<<endl;
		double matchrate2=matchShapes(contours2[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
		cout<<"index2="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate2<<endl;
		double matchrate3=matchShapes(contours3[0],contours[i],CV_CONTOURS_MATCH_I1, 0.0);
		cout<<"index3="<<i<<"---"<<setiosflags(ios::fixed)<<matchrate3<<endl;
		if(matchrate1<matchrate2&&matchrate1<matchrate3)
		{
		  Mat result= imread("cloth1.jpg");
		  imshow("jieguo", result);  
		  putText(dst, "cloth", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
		}
		if(matchrate2<matchrate1&&matchrate2<matchrate3)
		{
		  Mat result= imread("scissors1.jpg");
		  imshow("jieguo", result);    
		  putText(dst, "scissors", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
		}
		if(matchrate3<matchrate1&&matchrate3<matchrate1)
		{
		  Mat result= imread("stone1.jpg");
		  imshow("jieguo", result);  
		  putText(dst, "stone", Point(30, 70), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(0, 0, 255), 2, 8);
		}
</textarea>




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667958&siteId=291194637