Learn Opencv--hand tracking gesture Tracking

Source: Internet
Author: User

These days, the island choppy, I this unfortunate child after all the wood has escaped the clutches of a cold, the gun bird ~ ~ ~

Only a simple gesture tracking code was written these days.

The principle is: background difference + skin tone detection.

Background difference: Take the average of the first 30 frames, calculate the sum of the difference between the first 30 frames, and then seek the mean value. is detected outside the threshold that the background mean is floating above and below the value.

Skin Tone Detection: use YCRCB space.

Two results and operation.

Advantages of this approach: 1. Effectively solve the skin color test results are always detected in the face of the situation;

2. Solve the background difference detection results of the chaotic situation;

Disadvantage: The background requirements are relatively stable, the greater the contrast, the better, poor robustness.

Note: The difference method due to the accumulation of images, the code should pay attention to ensure normalization!!! NORMALIZE

[CPP]View PlainCopy print?
  1. #include "stdafx.h"
  2. #include <cv.h>
  3. #include
  4. #include <iostream>
  5. Using namespace CV;
  6. Using namespace std;
  7. void intial (Mat src);
  8. void Accbackgound (Mat src,mat Pre);
  9. void Backgound (int count);
  10. void Foregound (Mat src,mat Pre);
  11. void Skin (Mat src);
  12. Mat Bg,th,mask0;
  13. Mat Bglow0,bglow1,bglow2;
  14. Mat BGHIGH0,BGHIGH1,BGHIGH2;
  15. Mat Mask;
  16. int high=10,low=10;
  17. int main ()
  18. {
  19. int count=0;
  20. Videocapture capture;
  21. Capture.open (0);
  22. Mat FRAM,PREFRAM,RESULT,FG;
  23. int framnum=0;
  24. While (capture.isopened ())
  25. {
  26. capture>>fram;
  27. Fram.convertto (FRAM,CV_32FC3);
  28. Normalize (Fram,fram,1,0,cv_minmax);
  29. Imshow ("src", fram);
  30. if (framnum==0)
  31. {
  32. Intial (FRAM);
  33. }
  34. Else if (framnum<30)
  35. {
  36. ++count;
  37. Accbackgound (Fram,prefram);
  38. }
  39. Else if (framnum==30)
  40. Backgound (count);
  41. Else
  42. {
  43. Foregound (Fram,prefram);
  44. Skin (FRAM);
  45. }
  46. Fram.copyto (Prefram);
  47. framnum++;
  48. Char key= (char) Waitkey (2);
  49. switch (key)
  50. {
  51. Case :
  52. return 0;
  53. Break ;
  54. }
  55. }
  56. }
  57. void intial (Mat src)
  58. {
  59. Src.copyto (BG);
  60. }
  61. void Accbackgound (Mat src,mat Pre)
  62. {
  63. Mat temp;
  64. Accumulate (SRC,BG);
  65. Absdiff (src,pre,temp);
  66. if (th.data==null)
  67. {
  68. Temp.copyto (TH);
  69. }
  70. Else
  71. Accumulate (temp,th);
  72. }
  73. void Backgound (int count)
  74. {
  75. Bg=bg/count;
  76. Th=th/count;
  77. Normalize (Bg,bg,1,0,cv_minmax);
  78. Imshow ("Backgound", BG);
  79. Mat T[3];
  80. Mat B[3];
  81. Split (TH,T);
  82. Split (BG,B);
  83. Bglow0=b[0]-t[0]*low;
  84. Bglow1=b[1]-t[1]*low;
  85. Bglow2=b[2]-t[2]*low;
  86. Bghigh0=b[0]+t[0]*high;
  87. Bghigh1=b[1]+t[1]*high;
  88. Bghigh2=b[2]+t[2]*high;
  89. cout<<"Start traclking" <<endl;
  90. }
  91. void Foregound (Mat src,mat Pre)
  92. {
  93. Mat TEMP0,TEMP1,TEMP2;
  94. Mat Framnow[3];
  95. Mat Frampre[3];
  96. Framnow[0].setto (Scalar (0,0,0));
  97. Framnow[1].setto (Scalar (0,0,0));
  98. Framnow[2].setto (Scalar (0,0,0));
  99. Temp0.setto (Scalar (0,0,0));
  100. Temp1.setto (Scalar (0,0,0));
  101. Temp2.setto (Scalar (0,0,0));
  102. /* 
  103. Split (Pre,frampre);
  104. Accumulateweighted (frampre[0],bglow0,0.1);
  105. Accumulateweighted (frampre[0],bghigh0,0.1);
  106. Accumulateweighted (frampre[1],bglow1,0.1);
  107. Accumulateweighted (frampre[1],bghigh1,0.1);
  108. Accumulateweighted (frampre[2],bglow2,0.1);
  109. Accumulateweighted (frampre[2],bglow2,0.1);
  110. */
  111. Split (Src,framnow);
  112. InRange (FRAMNOW[0],BGLOW0,BGHIGH0,TEMP0);
  113. InRange (FRAMNOW[1],BGLOW1,BGHIGH1,TEMP1);
  114. InRange (FRAMNOW[2],BGLOW2,BGHIGH2,TEMP2);
  115. Bitwise_or (TEMP0,TEMP1,TEMP0);
  116. Bitwise_or (TEMP0,TEMP2,TEMP0);
  117. Bitwise_not (TEMP0,TEMP0);
  118. Imshow ("Show", temp0);
  119. Temp0.copyto (Mask0);
  120. }
  121. void Skin (Mat src)
  122. {
  123. Src.convertto (src,cv_8uc3,255);
  124. Mat YUV,DST;
  125. Cvtcolor (SRC,YUV,CV_BGR2YCRCB);
  126. Mat dstTemp1 (Src.rows, Src.cols, CV_8UC1);
  127. Mat dstTemp2 (Src.rows, Src.cols, CV_8UC1);
  128. //quantify YUV space, get 2 value image, bright part is hand shape
  129. InRange (YUV, scalar (0,133,0), scalar (256,173,256), DSTTEMP1);
  130. InRange (YUV, scalar (0,0,77), scalar (256,256,127), DSTTEMP2);
  131. Bitwise_and (DSTTEMP1, DSTTEMP2, mask);
  132. Dst.setto (Scalar::all (0));
  133. Bitwise_and (Mask,mask0,mask);
  134. Src.copyto (Dst,mask);
  135. vector< vector<point> > contours; //Contour
  136. vector< vector<point> > filtercontours; //Profile after screening
  137. vector< vec4i > hierarchy; //Profile structure information
  138. vector< Point > Hull; //Convex-enveloping point set
  139. Contours.clear ();
  140. Hierarchy.clear ();
  141. Filtercontours.clear ();
  142. //The contour of the hand
  143. Findcontours (Mask, contours, hierarchy, cv_retr_external, cv_chain_approx_simple);
  144. //Remove pseudo-contour
  145. For (size_t i = 0; i < contours.size (); i++)
  146. {
  147. //APPROXPOLYDP (Mat (Contours[i]), Mat (Approxcontours[i]), Arclength (Mat (Contours[i]), true) *0.02, true);
  148. if (Fabs (Contourarea (Mat (contours[i))) > 1000&&fabs (Arclength (Mat (contours[i)),true) <2000 ) //To determine the threshold value of the hand entry area
  149. {
  150. Filtercontours.push_back (Contours[i]);
  151. }
  152. }
  153. //Draw Contour
  154. Drawcontours (SRC, filtercontours,-1, Scalar (0,0,255), 2);  //8, hierarchy);
  155. Imshow ("traclking", SRC);
  156. }


from:http://blog.csdn.net/yangtrees/article/details/7566284

Learn Opencv--hand tracking gesture Tracking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.