PalmDetector.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. class PalmDetector
  17. {
  18. private DepthImage depthImage;
  19. private List<Hand> hands;
  20. private List<Palm> palms;
  21. public void detectPalms(FrameData frameData)
  22. {
  23. depthImage = frameData.DepthImage;
  24. hands = frameData.TrackedHands;
  25. findPalms();
  26. frameData.DetectedPalms = palms;
  27. }
  28. private void findPalms()
  29. {
  30. palms = new List<Palm>();
  31. foreach (Hand hand in hands)
  32. {
  33. if (hand.Fingers.Count == 1)
  34. {
  35. List<ConvexityDefect> convexityDefects = findConvexityDefects(hand);
  36. ConvexityDefect thumbDefect = findThumbDefect(hand.Fingers[0], convexityDefects);
  37. if (thumbDefect != null)
  38. {
  39. Palm palm = createPalm(hand, thumbDefect);
  40. palms.Add(palm);
  41. }
  42. }
  43. }
  44. }
  45. private List<ConvexityDefect> findConvexityDefects(Hand hand)
  46. {
  47. List<ConvexityDefect> convexityDefects;
  48. using (MemStorage ms = new MemStorage())
  49. {
  50. Contour<Point> contour = hand.Mask.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  51. List<MCvConvexityDefect> mcvConvexityDefects = new List<MCvConvexityDefect>(contour.GetConvexityDefacts(ms, Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  52. convexityDefects = new List<ConvexityDefect>();
  53. foreach (MCvConvexityDefect defect in mcvConvexityDefects)
  54. convexityDefects.Add(new ConvexityDefect(defect));
  55. }
  56. return convexityDefects;
  57. }
  58. private ConvexityDefect findThumbDefect(Finger thumb, List<ConvexityDefect> convexityDefects)
  59. {
  60. convexityDefects.Sort((cd1, cd2) => (cd2.Depth.CompareTo(cd1.Depth)));
  61. foreach (ConvexityDefect defect in convexityDefects)
  62. {
  63. if (defect.isPossibleThumbDefect(thumb))
  64. return defect;
  65. }
  66. return null;
  67. }
  68. private Palm createPalm(Hand hand, ConvexityDefect thumbDefect)
  69. {
  70. HandSide side = determineHandSide(thumbDefect);
  71. Vector2D wristUpper = findWristUpper(hand, thumbDefect);
  72. Vector2D fingersUpper = findFingersUpper(hand, thumbDefect, side);
  73. float palmWidth = findPalmWidth(hand, thumbDefect, side);
  74. float palmLength = wristUpper.getDistanceTo(fingersUpper);
  75. Vector2D directionWristFingers = thumbDefect.VectorLong.normalize();
  76. Vector2D directionUpperLower = thumbDefect.VectorLong.getOrthogonal(side == HandSide.Right).normalize();
  77. Vector2D wristLower = wristUpper + palmWidth*directionUpperLower;
  78. Vector2D fingersLower = wristUpper + 0.75f * palmLength * directionWristFingers + 0.75f * palmWidth * directionUpperLower;
  79. return new Palm(hand, thumbDefect, side, wristUpper, fingersUpper, fingersLower, wristLower);
  80. }
  81. private HandSide determineHandSide(ConvexityDefect thumbDefect)
  82. {
  83. if (thumbDefect.VectorShort.crossProduct(thumbDefect.VectorLong) < 0)
  84. return HandSide.Right;
  85. else
  86. return HandSide.Left;
  87. }
  88. private Vector2D findWristUpper(Hand hand, ConvexityDefect thumbDefect)
  89. {
  90. Vector2D wristDirection = thumbDefect.VectorLong.getInverse().normalize();
  91. Vector2D wristUpper = thumbDefect.Inner;
  92. Vector2D wristUpperNext = wristUpper + 5*wristDirection;
  93. while (wristUpperNext.isInBound(depthImage.Size) && hand.isInside(wristUpperNext))
  94. {
  95. wristUpper = wristUpperNext;
  96. wristUpperNext += wristDirection;
  97. }
  98. return wristUpper;
  99. }
  100. private Vector2D findFingersUpper(Hand hand, ConvexityDefect thumbDefect, HandSide side)
  101. {
  102. Vector2D fingersDirection = thumbDefect.VectorLong.normalize();
  103. Vector2D lowerDirection = fingersDirection.getOrthogonal(side == HandSide.Right).normalize();
  104. Vector2D fingersUpper = thumbDefect.OuterLong;
  105. Vector2D fingersUpperNext = fingersUpper + fingersDirection;
  106. bool handBelow = true;
  107. while (handBelow && fingersUpperNext.isInBound(depthImage.Size))
  108. {
  109. fingersUpper = fingersUpperNext;
  110. fingersUpperNext += fingersDirection;
  111. Vector2D below = fingersUpper.copy();
  112. handBelow = false;
  113. int distance = 0;
  114. while (!handBelow && distance < Parameters.FingerMaxWidth2D && below.isInBound(depthImage.Size))
  115. {
  116. handBelow = hand.isInside(below);
  117. below += lowerDirection;
  118. }
  119. }
  120. return fingersUpper;
  121. }
  122. private float findPalmWidth(Hand hand, ConvexityDefect thumbDefect, HandSide side)
  123. {
  124. Vector2D lowerDirection = thumbDefect.VectorLong.getOrthogonal(side == HandSide.Right).normalize();
  125. Vector2D current = thumbDefect.Inner;
  126. Vector2D step = thumbDefect.VectorLong / Parameters.PalmNumPositionsForPalmWidth;
  127. float maxWidth = float.MinValue;
  128. for (int i = 0; i < Parameters.PalmNumPositionsForPalmWidth; i++)
  129. {
  130. Vector2D lower = current;
  131. Vector2D lowerNext = lower + 10 * lowerDirection;
  132. while (lowerNext.isInBound(depthImage.Size) && hand.isInside(lowerNext))
  133. {
  134. lower = lowerNext;
  135. lowerNext += lowerDirection;
  136. }
  137. maxWidth = Math.Max(maxWidth, current.getDistanceTo(lower));
  138. current += step;
  139. }
  140. return maxWidth;
  141. }
  142. }
  143. }