PalmDetector.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.Images;
  8. using bbiwarg.Utility;
  9. using bbiwarg.Recognition.FingerRecognition;
  10. using bbiwarg.Recognition.HandRecognition;
  11. using bbiwarg.Input.InputHandling;
  12. using Emgu.CV;
  13. using Emgu.CV.Structure;
  14. namespace bbiwarg.Recognition.PalmRecognition
  15. {
  16. /// <summary>
  17. /// Detects palms by iterating over each hand, if the hand has exactly one finger (possible thumb) it calculates its convexity defects and checks if there is a convexity defect that matches the requirements for a thumb defect. If a thumb defect is found, the four palm points are generated and a new palm is created.
  18. /// </summary>
  19. class PalmDetector
  20. {
  21. /// <summary>
  22. /// the depth image of the current frame
  23. /// </summary>
  24. private DepthImage depthImage;
  25. /// <summary>
  26. /// the hands in the current frame
  27. /// </summary>
  28. private List<Hand> hands;
  29. /// <summary>
  30. /// the palms in the current frames
  31. /// </summary>
  32. private List<Palm> palms;
  33. /// <summary>
  34. /// Detects palms in the current frame and stores them in frameData.detectedPalms
  35. /// </summary>
  36. /// <param name="frameData">the current frame</param>
  37. public void detectPalms(FrameData frameData)
  38. {
  39. depthImage = frameData.DepthImage;
  40. hands = frameData.TrackedHands;
  41. findPalms();
  42. frameData.DetectedPalms = palms;
  43. }
  44. /// <summary>
  45. /// Find Palms by checking each hand with only one finger, if that finger is a thumb.
  46. /// </summary>
  47. private void findPalms()
  48. {
  49. palms = new List<Palm>();
  50. foreach (Hand hand in hands)
  51. {
  52. if (hand.Fingers.Count == 1)
  53. {
  54. List<ConvexityDefect> convexityDefects = findConvexityDefects(hand);
  55. ConvexityDefect thumbDefect = findThumbDefect(hand.Fingers[0], convexityDefects);
  56. if (thumbDefect != null)
  57. {
  58. Palm palm = createPalm(hand, thumbDefect);
  59. palms.Add(palm);
  60. }
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Gets the convexity Defects from a hand mask.
  66. /// </summary>
  67. /// <param name="hand">the hand</param>
  68. /// <returns>the conveixty defects</returns>
  69. private List<ConvexityDefect> findConvexityDefects(Hand hand)
  70. {
  71. List<ConvexityDefect> convexityDefects;
  72. using (MemStorage ms = new MemStorage())
  73. {
  74. Contour<Point> contour = hand.Mask.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  75. List<MCvConvexityDefect> mcvConvexityDefects = new List<MCvConvexityDefect>(contour.GetConvexityDefacts(ms, Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  76. convexityDefects = new List<ConvexityDefect>();
  77. foreach (MCvConvexityDefect defect in mcvConvexityDefects)
  78. convexityDefects.Add(new ConvexityDefect(defect));
  79. }
  80. return convexityDefects;
  81. }
  82. /// <summary>
  83. /// Checks all convexity defects sorted by length if they match the thumb defect requirements and returns the first match or null if none match.
  84. /// </summary>
  85. /// <param name="thumb">the possible thumb</param>
  86. /// <param name="convexityDefects">the convexity defects</param>
  87. /// <returns></returns>
  88. private ConvexityDefect findThumbDefect(Finger thumb, List<ConvexityDefect> convexityDefects)
  89. {
  90. convexityDefects.Sort((cd1, cd2) => (cd2.Depth.CompareTo(cd1.Depth)));
  91. foreach (ConvexityDefect defect in convexityDefects)
  92. {
  93. if (defect.isPossibleThumbDefect(thumb))
  94. return defect;
  95. }
  96. return null;
  97. }
  98. /// <summary>
  99. /// Finds the four palm points and creates a new palm
  100. /// </summary>
  101. /// <param name="hand">the hand of the palm</param>
  102. /// <param name="thumbDefect">the convexity defect of the thumb</param>
  103. /// <returns>a new palm</returns>
  104. private Palm createPalm(Hand hand, ConvexityDefect thumbDefect)
  105. {
  106. HandSide side = determineHandSide(thumbDefect);
  107. Vector2D wristUpper = findWristUpper(hand, thumbDefect);
  108. Vector2D fingersUpper = findFingersUpper(hand, thumbDefect, side);
  109. float palmWidth = findPalmWidth(hand, thumbDefect, side);
  110. float palmLength = wristUpper.getDistanceTo(fingersUpper);
  111. Vector2D directionWristFingers = thumbDefect.VectorLong.normalize();
  112. Vector2D directionUpperLower = thumbDefect.VectorLong.getOrthogonal(side == HandSide.Right).normalize();
  113. Vector2D wristLower = wristUpper + palmWidth * directionUpperLower;
  114. Vector2D fingersLower = wristUpper + 0.75f * palmLength * directionWristFingers + 0.75f * palmWidth * directionUpperLower;
  115. return new Palm(hand, thumbDefect, side, wristUpper, fingersUpper, fingersLower, wristLower);
  116. }
  117. /// <summary>
  118. /// Determines the handedness of the palm's hand
  119. /// </summary>
  120. /// <param name="thumbDefect">the convexity defect of the thumb</param>
  121. /// <returns>the handedness of the palm's hand</returns>
  122. private HandSide determineHandSide(ConvexityDefect thumbDefect)
  123. {
  124. if (thumbDefect.VectorShort.crossProduct(thumbDefect.VectorLong) < 0)
  125. return HandSide.Right;
  126. else
  127. return HandSide.Left;
  128. }
  129. /// <summary>
  130. /// Finds the upper wrist end by walking from the defect.Inner towards the wrist direction, until it reaches the hand boundaries.
  131. /// </summary>
  132. /// <param name="hand">the hand</param>
  133. /// <param name="thumbDefect">the convexity defect of the thumb</param>
  134. /// <returns>the position of the upper wrist end</returns>
  135. private Vector2D findWristUpper(Hand hand, ConvexityDefect thumbDefect)
  136. {
  137. Vector2D wristDirection = thumbDefect.VectorLong.getInverse().normalize();
  138. Vector2D wristUpper = thumbDefect.Inner;
  139. Vector2D wristUpperNext = wristUpper + 5 * wristDirection;
  140. while (wristUpperNext.isInBound(depthImage.Size) && hand.isInside(wristUpperNext))
  141. {
  142. wristUpper = wristUpperNext;
  143. wristUpperNext += wristDirection;
  144. }
  145. return wristUpper;
  146. }
  147. /// <summary>
  148. /// Finds the upper finger end by walking from the defect.OuterLong in the finger direction, until it reaches the end of the middle finger below.
  149. /// </summary>
  150. /// <param name="hand">the hand</param>
  151. /// <param name="thumbDefect">the convexity defect of the thumb</param>
  152. /// <param name="side">the handedness</param>
  153. /// <returns>the position of the upper fingers end</returns>
  154. private Vector2D findFingersUpper(Hand hand, ConvexityDefect thumbDefect, HandSide side)
  155. {
  156. Vector2D fingersDirection = thumbDefect.VectorLong.normalize();
  157. Vector2D lowerDirection = fingersDirection.getOrthogonal(side == HandSide.Right).normalize();
  158. Vector2D fingersUpper = thumbDefect.OuterLong;
  159. Vector2D fingersUpperNext = fingersUpper + fingersDirection;
  160. bool handBelow = true;
  161. while (handBelow && fingersUpperNext.isInBound(depthImage.Size))
  162. {
  163. fingersUpper = fingersUpperNext;
  164. fingersUpperNext += fingersDirection;
  165. Vector2D below = fingersUpper.copy();
  166. handBelow = false;
  167. int distance = 0;
  168. while (!handBelow && distance < Parameters.FingerMaxWidth2D && below.isInBound(depthImage.Size))
  169. {
  170. handBelow = hand.isInside(below);
  171. below += lowerDirection;
  172. }
  173. }
  174. return fingersUpper;
  175. }
  176. /// <summary>
  177. /// Gets the palm width by checking for the maximum orthogonal length withing the hand along multiple positions on the index finger (defect.inner<->defect.outerLong)
  178. /// </summary>
  179. /// <param name="hand">the hand</param>
  180. /// <param name="thumbDefect">the convexity defect of the thumb</param>
  181. /// <param name="side">the handedness</param>
  182. /// <returns>the palm width</returns>
  183. private float findPalmWidth(Hand hand, ConvexityDefect thumbDefect, HandSide side)
  184. {
  185. Vector2D lowerDirection = thumbDefect.VectorLong.getOrthogonal(side == HandSide.Right).normalize();
  186. Vector2D current = thumbDefect.Inner;
  187. Vector2D step = thumbDefect.VectorLong / Parameters.PalmNumPositionsForPalmWidth;
  188. float maxWidth = float.MinValue;
  189. for (int i = 0; i < Parameters.PalmNumPositionsForPalmWidth; i++)
  190. {
  191. Vector2D lower = current;
  192. Vector2D lowerNext = lower + 10 * lowerDirection;
  193. while (lowerNext.isInBound(depthImage.Size) && hand.isInside(lowerNext))
  194. {
  195. lower = lowerNext;
  196. lowerNext += lowerDirection;
  197. }
  198. maxWidth = Math.Max(maxWidth, current.getDistanceTo(lower));
  199. current += step;
  200. }
  201. return maxWidth;
  202. }
  203. }
  204. }