FingerDetector.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using bbiwarg.Images;
  9. using bbiwarg.Input.InputHandling;
  10. using bbiwarg.Utility;
  11. using Emgu.CV.Structure;
  12. using Emgu.CV;
  13. namespace bbiwarg.Recognition.FingerRecognition
  14. {
  15. class FingerDetector
  16. {
  17. private DepthImage depthImage;
  18. private EdgeImage edgeImageOriginal;
  19. private EdgeImage edgeImageAdapted;
  20. private List<Finger> fingers;
  21. public void detectFingers(FrameData frameData)
  22. {
  23. depthImage = frameData.DepthImage;
  24. edgeImageOriginal = frameData.EdgeImage;
  25. edgeImageAdapted = frameData.EdgeImage.copy();
  26. fingers = new List<Finger>();
  27. Vector2D maxPixel = depthImage.Size.MaxPixel;
  28. int maxX = maxPixel.IntX;
  29. int maxY = maxPixel.IntY;
  30. for (int y = 1; y < maxY; y += 4)
  31. {
  32. for (int x = 1; x < maxX; x += 2)
  33. {
  34. if (edgeImageAdapted.isEdgeAt(x, y))
  35. {
  36. Vector2D edgePoint = new Vector2D(x, y);
  37. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  38. if (edgeDirection != null)
  39. {
  40. Vector2D dir = edgeDirection.getOrthogonal();
  41. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  42. dir = dir.getInverse();
  43. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  44. if (slice != null)
  45. {
  46. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  47. if (trail != null && trail.NumSlices > Parameters.FingerMinNumSlices)
  48. {
  49. createFingerFromTrail(trail);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. frameData.DetectedFingers = fingers;
  57. }
  58. private Vector2D getEdgeDirection(Vector2D edgePoint)
  59. {
  60. int x = edgePoint.IntX;
  61. int y = edgePoint.IntY;
  62. if (edgeImageAdapted.isEdgeAt(x, y - 1) && edgeImageAdapted.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  63. else if (edgeImageAdapted.isEdgeAt(x - 1, y) && edgeImageAdapted.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  64. else if (edgeImageAdapted.isEdgeAt(x - 1, y - 1) && edgeImageAdapted.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  65. else if (edgeImageAdapted.isEdgeAt(x + 1, y - 1) && edgeImageAdapted.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  66. else return null;
  67. }
  68. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  69. {
  70. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  71. Vector2D direction = startDirection;
  72. Vector2D position = startSlice.Mid + Parameters.FingerStepSize * direction;
  73. if (position.isInBound(depthImage.Size))
  74. {
  75. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  76. if (nextSlice != null)
  77. {
  78. trail.addSlice(nextSlice);
  79. trail = expandTrail(trail);
  80. if (trail.NumSlices > Parameters.FingerMinNumSlices / 2)
  81. {
  82. trail.removeFirstSlices(Parameters.FingerRemoveNumSlicesForCorrection);
  83. trail = expandTrail(trail, true);
  84. return trail;
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  91. {
  92. if (reversed)
  93. trail.reverse();
  94. Vector2D currentDirection = trail.getEndDirection();
  95. Vector2D currentPosition = trail.EndSlice.Mid + Parameters.FingerStepSize * currentDirection;
  96. int gapCounter = 0;
  97. int numSlices = trail.NumSlices;
  98. FingerSlice lastSlice = trail.EndSlice;
  99. FingerSlice nextSlice;
  100. while (currentPosition.isInBound(depthImage.Size) && gapCounter < Math.Min(numSlices, Parameters.FingerMaxGapCounter))
  101. {
  102. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection, reversed);
  103. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Parameters.FingerMaxSliceDifferencePerStep)
  104. {
  105. gapCounter = 0;
  106. numSlices++;
  107. trail.addSlice(nextSlice);
  108. currentDirection = trail.getEndDirection();
  109. currentPosition = nextSlice.Mid + Parameters.FingerStepSize * currentDirection;
  110. lastSlice = nextSlice;
  111. }
  112. else
  113. {
  114. gapCounter++;
  115. currentPosition += currentDirection;
  116. }
  117. }
  118. if (reversed)
  119. trail.reverse();
  120. return trail;
  121. }
  122. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction, bool reversed = false)
  123. {
  124. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  125. Vector2D dirStart = direction.getOrthogonal(reversed);
  126. Vector2D dirEnd = direction.getOrthogonal(!reversed);
  127. Vector2D start = edgeImageAdapted.findNextRoughEdge(position, dirStart, Parameters.FingerMaxWidth);
  128. if (start == null) return null;
  129. Vector2D end = edgeImageAdapted.findNextRoughEdge(position, dirEnd, Parameters.FingerMaxWidth);
  130. if (end == null) return null;
  131. return getFingerSlice(start, end);
  132. }
  133. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  134. {
  135. Vector2D searchStart = start + Parameters.FingerContourMargin * direction;
  136. Vector2D end = edgeImageAdapted.findNextRoughEdge(searchStart, direction, Parameters.FingerMaxWidth);
  137. if (end == null)
  138. return null;
  139. return getFingerSlice(start, end);
  140. }
  141. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  142. {
  143. FingerSlice slice = new FingerSlice(start, end);
  144. if (slice.Length >= Parameters.FingerMinWidth && slice.Length <= Parameters.FingerMaxWidth && fingerSliceDepthTest(slice))
  145. return slice;
  146. return null;
  147. }
  148. private bool fingerSliceDepthTest(FingerSlice slice)
  149. {
  150. Int16 depthStart = depthImage.getDepthAt(slice.Start.moveWithinBound(depthImage.Size, slice.Direction.getInverse(), Parameters.FingerContourMargin));
  151. Int16 depthMid = depthImage.getDepthAt(slice.Mid);
  152. Int16 depthEnd = depthImage.getDepthAt(slice.End.moveWithinBound(depthImage.Size, slice.Direction, Parameters.FingerContourMargin));
  153. return (depthStart > depthMid && depthMid < depthEnd);
  154. }
  155. private void createFingerFromTrail(FingerSliceTrail trail)
  156. {
  157. //bring finger in correct direction Tip->Hand
  158. trail = orderTrailTipToHand(trail);
  159. //create finger
  160. Finger finger = new Finger(trail);
  161. //add finger
  162. if (!isCrippleFinger(finger))
  163. fingers.Add(finger);
  164. //remove edges around detected finger to improve performance
  165. edgeImageAdapted.removeEdgesInsidePolygon(finger.getContour(Parameters.FingerContourMargin).ToArray());
  166. }
  167. private bool isCrippleFinger(Finger finger)
  168. {
  169. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  170. Vector2D out1 = midSlice.Start.moveWithinBound(depthImage.Size, midSlice.Direction.getInverse(), Parameters.FingerOutMargin);
  171. Vector2D out2 = midSlice.End.moveWithinBound(depthImage.Size, midSlice.Direction, Parameters.FingerOutMargin);
  172. Int16 depthAtFinger = depthImage.getDepthAt(finger.MidPoint);
  173. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  174. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  175. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  176. return (minDepthDifference < Parameters.FingerMaxCrippleDifference);
  177. }
  178. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  179. {
  180. float startLength = trail.StartSlice.Length;
  181. float endLength = trail.EndSlice.Length;
  182. if (startLength > endLength)
  183. trail.reverse();
  184. return trail;
  185. }
  186. }
  187. }