HandDetector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.Recognition.FingerRecognition;
  9. using bbiwarg.Output;
  10. using bbiwarg.Utility;
  11. using bbiwarg.Input.InputHandling;
  12. using Emgu.CV;
  13. using Emgu.CV.Structure;
  14. namespace bbiwarg.Recognition.HandRecognition
  15. {
  16. /// <summary>
  17. /// Finds Hands by iterating over all fingers and flooding them. Each filled region is considered to be one hand and each finger belongs to one hand. To improve the hand contours, the hand mask are filled with the defects caused by overlapping fingers.
  18. /// </summary>
  19. class HandDetector
  20. {
  21. /// <summary>
  22. /// the depth image of the current frame
  23. /// </summary>
  24. private DepthImage depthImage;
  25. /// <summary>
  26. /// the modified depth image of the current frame (hand flooding changes depth image)
  27. /// </summary>
  28. private Image<Gray, byte> modifiedHandDepthImage;
  29. /// <summary>
  30. /// the fingers in the current frame
  31. /// </summary>
  32. private List<Finger> fingers;
  33. /// <summary>
  34. /// a mapping of hands and list of fingers, that don't belong to that specific hand
  35. /// </summary>
  36. private Dictionary<Hand, List<Finger>> otherHandsFingers;
  37. /// <summary>
  38. /// the hands in the current frame
  39. /// </summary>
  40. private List<Hand> hands;
  41. /// <summary>
  42. /// Detects Hands in the current frame and stores found hands in frameData.detectedHands
  43. /// </summary>
  44. /// <param name="frameData">the current frame</param>
  45. public void detectHands(FrameData frameData)
  46. {
  47. depthImage = frameData.DepthImage;
  48. fingers = frameData.TrackedFingers;
  49. createModifiedHandEdgeImage();
  50. findHands();
  51. fixOverlappingFingers();
  52. findCentroids();
  53. frameData.DetectedHands = hands;
  54. }
  55. /// <summary>
  56. /// Creates the modified hand image. The image is a copy of the original depth image with a contour around each finger (to prevent floodfill from filling through fingers).
  57. /// </summary>
  58. private void createModifiedHandEdgeImage()
  59. {
  60. modifiedHandDepthImage = depthImage.Image.Copy();
  61. foreach (Finger finger in fingers)
  62. {
  63. Int16 depthAtHand = depthImage.getDepthAt(finger.HandPoint);
  64. Point[] contour = finger.getContour(0f).ToArray();
  65. modifiedHandDepthImage.DrawPolyline(contour, false, new Gray(depthAtHand), 1);
  66. }
  67. }
  68. /// <summary>
  69. /// Finds hands by flood filling from each finger (mask). All unassigned fingers that lie within the hand are assigned to the hand, all other fingers are mapped as fingers that don't belong to that specific hand.
  70. /// </summary>
  71. private void findHands()
  72. {
  73. hands = new List<Hand>();
  74. otherHandsFingers = new Dictionary<Hand,List<Finger>>();
  75. List<Finger> assignedFingers = new List<Finger>();
  76. foreach (Finger finger in fingers)
  77. {
  78. if(!assignedFingers.Contains(finger)) {
  79. Image<Gray, byte> handMask = getHandMask(finger.HandPoint);
  80. List<Finger> fingersOnHand = new List<Finger>();
  81. List<Finger> fingersOnOtherHand = new List<Finger>();
  82. foreach (Finger f in fingers) {
  83. if (!assignedFingers.Contains(f) && handMask.Data[f.HandPoint.IntY, f.HandPoint.IntX, 0] != 0)
  84. {
  85. fingersOnHand.Add(f);
  86. assignedFingers.Add(f);
  87. }
  88. else
  89. fingersOnOtherHand.Add(f);
  90. }
  91. Hand hand = new Hand(handMask, fingersOnHand);
  92. otherHandsFingers.Add(hand, fingersOnOtherHand);
  93. hands.Add(hand);
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Flood fills from a given point and returns the filled area as mask.
  99. /// </summary>
  100. /// <param name="p">flood fill starting point</param>
  101. /// <returns>the filled area as mask</returns>
  102. private Image<Gray, byte> getHandMask(Vector2D p)
  103. {
  104. Image<Gray, byte> mask = new Image<Gray, byte>(depthImage.Size.Width + 2, depthImage.Size.Height + 2);
  105. MCvConnectedComp comp = new MCvConnectedComp();
  106. CvInvoke.cvFloodFill(modifiedHandDepthImage, p, new MCvScalar(255), new MCvScalar(Parameters.HandFloodFillDownDiff), new MCvScalar(Parameters.HandFloodFillUpDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, mask);
  107. return mask.Copy(new Rectangle(1, 1, depthImage.Size.Width, depthImage.Size.Height));
  108. }
  109. /// <summary>
  110. /// Fixes overlapping fingers by merging two hands if they are seperated by a finger and/or fills holes caused by overlapping fingers.
  111. /// </summary>
  112. private void fixOverlappingFingers()
  113. {
  114. extendOrMergeThroughOverlappingFingers();
  115. fillOverlappingFingers();
  116. }
  117. /// <summary>
  118. /// Merges two hands if they are seperatd by an overlapping finger or extends the hand mask through an overlapping finger.
  119. /// </summary>
  120. private void extendOrMergeThroughOverlappingFingers()
  121. {
  122. List<Hand> mergedHands = new List<Hand>();
  123. foreach (Hand hand in hands)
  124. {
  125. if (!mergedHands.Contains(hand))
  126. {
  127. List<Hand> mergeHands = new List<Hand>();
  128. foreach (Finger overlappingFinger in otherHandsFingers[hand])
  129. {
  130. FingerSlice midSlice = overlappingFinger.SliceTrail.MidSlice;
  131. Vector2D midOut1 = midSlice.Start.moveWithinBound(depthImage.Size, midSlice.Direction.getInverse(), Parameters.FingerOutMargin);
  132. Vector2D midOut2 = midSlice.End.moveWithinBound(depthImage.Size, midSlice.Direction, Parameters.FingerOutMargin);
  133. Int16 depthAtMidOut1 = depthImage.getDepthAt(midOut1);
  134. Int16 depthAtMidOut2 = depthImage.getDepthAt(midOut2);
  135. bool midOut1InHand = hand.isInside(midOut1);
  136. bool midOut2InHand = hand.isInside(midOut2);
  137. Int16 maxDepth = depthImage.MaxDepth;
  138. if (midOut1InHand != midOut2InHand && depthAtMidOut1 != maxDepth && depthAtMidOut2 != maxDepth && Math.Abs(depthAtMidOut1 - depthAtMidOut2) < Parameters.HandExtendMaxDifference)
  139. {
  140. Vector2D pHand, pHandExtension;
  141. if (midOut1InHand)
  142. {
  143. pHand = midOut1;
  144. pHandExtension = midOut2;
  145. }
  146. else
  147. {
  148. pHand = midOut2;
  149. pHandExtension = midOut1;
  150. }
  151. //check if pHandExtension is in other hand (if so -> merge with hand)
  152. bool merge = false;
  153. foreach (Hand mergeHand in hands)
  154. {
  155. if (mergeHand.isInside(pHandExtension) && !mergedHands.Contains(mergeHand))
  156. {
  157. mergeHands.Add(mergeHand);
  158. merge = true;
  159. break;
  160. }
  161. }
  162. //if no merge, extend hand
  163. if (!merge)
  164. extendToHand(hand, pHandExtension);
  165. }
  166. }
  167. foreach (Hand mergeHand in mergeHands)
  168. {
  169. mergeToHand(hand, mergeHand);
  170. mergedHands.Add(mergeHand);
  171. }
  172. }
  173. }
  174. foreach (Hand mergedHand in mergedHands)
  175. hands.Remove(mergedHand);
  176. }
  177. /// <summary>
  178. /// Merges two hands together and updates the list of other hands fingers.
  179. /// </summary>
  180. /// <param name="hand">the first hand (other hand will be merged to this one)</param>
  181. /// <param name="mergeHand">the second hand (this hand will be dropped afterwards)</param>
  182. private void mergeToHand(Hand hand, Hand mergeHand)
  183. {
  184. hand.mergeWith(mergeHand);
  185. foreach (Finger finger in mergeHand.Fingers)
  186. otherHandsFingers[hand].Remove(finger);
  187. otherHandsFingers.Remove(mergeHand);
  188. }
  189. /// <summary>
  190. /// Extends the hand mask of a given hand by flood filling starting from the given point.
  191. /// </summary>
  192. /// <param name="hand">the hand that should be extended</param>
  193. /// <param name="p">the flood fill starting point</param>
  194. private void extendToHand(Hand hand, Vector2D p)
  195. {
  196. Image<Gray, byte> extendMask = getHandMask(p);
  197. int numPixels = extendMask.CountNonzero()[0];
  198. if (numPixels <= Parameters.HandExtensionMaxRelativeSize * depthImage.Size.NumPixels)
  199. hand.extendMask(extendMask);
  200. }
  201. /// <summary>
  202. /// Fills holes caused by overlapping fingers.
  203. /// </summary>
  204. private void fillOverlappingFingers()
  205. {
  206. foreach (Hand hand in hands)
  207. {
  208. hand.fillOverlappingFingers(otherHandsFingers[hand]);
  209. }
  210. }
  211. /// <summary>
  212. /// Finds the hands centroids.
  213. /// </summary>
  214. private void findCentroids() {
  215. foreach (Hand hand in hands)
  216. hand.findCentroid();
  217. }
  218. }
  219. }