FingerDetector.cs 9.6 KB

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