PalmDetector.cs 9.6 KB

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