FingerDetector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.Utility;
  10. using Emgu.CV.Structure;
  11. using Emgu.CV;
  12. using bbiwarg.Graphics;
  13. namespace bbiwarg.Recognition.FingerRecognition
  14. {
  15. class FingerDetector
  16. {
  17. private DepthImage depthImage;
  18. private EdgeImage edgeImageOriginal;
  19. private EdgeImage edgeImageAdapted;
  20. private OutputImage outputImage;
  21. public List<Finger> Fingers { get; private set; }
  22. public FingerDetector(DepthImage depthImage, EdgeImage edgeImage, OutputImage outputImage)
  23. {
  24. this.depthImage = depthImage;
  25. this.edgeImageOriginal = edgeImage;
  26. this.edgeImageAdapted = edgeImage.copy();
  27. this.outputImage = outputImage;
  28. detectFingers();
  29. drawFingers();
  30. }
  31. private void detectFingers()
  32. {
  33. int maxX = depthImage.BottomRight.IntX;
  34. int maxY = depthImage.BottomRight.IntY;
  35. Fingers = new List<Finger>();
  36. for (int y = 1; y < maxY; y += 5)
  37. {
  38. for (int x = 1; x < maxX; x++)
  39. {
  40. if (edgeImageAdapted.isEdgeAt(x, y))
  41. {
  42. Vector2D edgePoint = new Vector2D(x, y);
  43. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  44. if (edgeDirection != null)
  45. {
  46. Vector2D dir = edgeDirection.getOrthogonal();
  47. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  48. dir = dir.getInverse();
  49. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  50. if (slice != null)
  51. {
  52. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  53. if (trail != null && trail.NumSlices > Constants.FingerMinNumSlices)
  54. {
  55. createFingerFromTrail(trail);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. private Vector2D getEdgeDirection(Vector2D edgePoint)
  64. {
  65. int x = edgePoint.IntX;
  66. int y = edgePoint.IntY;
  67. if (edgeImageAdapted.isEdgeAt(x, y - 1) && edgeImageAdapted.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  68. else if (edgeImageAdapted.isEdgeAt(x - 1, y) && edgeImageAdapted.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  69. else if (edgeImageAdapted.isEdgeAt(x - 1, y - 1) && edgeImageAdapted.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  70. else if (edgeImageAdapted.isEdgeAt(x + 1, y - 1) && edgeImageAdapted.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  71. else return null;
  72. }
  73. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  74. {
  75. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  76. Vector2D direction = startDirection;
  77. Vector2D position = startSlice.Mid + Constants.FingerStepSize * direction;
  78. if (position.isInBound(Vector2D.Zero, depthImage.BottomRight))
  79. {
  80. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  81. if (nextSlice != null)
  82. {
  83. trail.addSlice(nextSlice);
  84. trail = expandTrail(trail);
  85. if (trail.NumSlices > Constants.FingerMinNumSlices / 2)
  86. {
  87. trail.removeFirstSlices(Constants.FingerRemoveNumSlicesForCorrection);
  88. trail.reverse();
  89. trail = expandTrail(trail);
  90. trail.reverse();
  91. return trail;
  92. }
  93. }
  94. }
  95. return null;
  96. }
  97. private FingerSliceTrail expandTrail(FingerSliceTrail trail)
  98. {
  99. Vector2D currentDirection = trail.getEndDirection();
  100. Vector2D currentPosition = trail.EndSlice.Mid + Constants.FingerStepSize * currentDirection;
  101. int gapCounter = 0;
  102. int numSlices = trail.NumSlices;
  103. FingerSlice lastSlice = trail.EndSlice;
  104. FingerSlice nextSlice;
  105. while (currentPosition.isInBound(Vector2D.Zero, depthImage.BottomRight) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
  106. {
  107. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  108. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  109. {
  110. gapCounter = 0;
  111. numSlices++;
  112. trail.addSlice(nextSlice);
  113. currentDirection = trail.getEndDirection();
  114. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  115. lastSlice = nextSlice;
  116. }
  117. else
  118. {
  119. gapCounter++;
  120. currentPosition += currentDirection;
  121. }
  122. }
  123. return trail;
  124. }
  125. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  126. {
  127. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  128. Vector2D dirStart = direction.getOrthogonal(true);
  129. Vector2D dirEnd = direction.getOrthogonal(false);
  130. Vector2D start = edgeImageAdapted.findNextEdge(position, dirStart, Constants.FingerMaxWidth);
  131. if (start == null) return null;
  132. Vector2D end = edgeImageAdapted.findNextEdge(position, dirEnd, Constants.FingerMaxWidth);
  133. if (end == null) return null;
  134. return getFingerSlice(start, end);
  135. }
  136. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  137. {
  138. Vector2D searchStart = start + Constants.FingerSliceOverlapFactor * direction;
  139. Vector2D end = edgeImageAdapted.findNextEdge(searchStart, direction, Constants.FingerMaxWidth);
  140. if (end == null) return null;
  141. return getFingerSlice(start, end);
  142. }
  143. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  144. {
  145. Vector2D direction = (end - start).normalize();
  146. Vector2D directionInv = direction.getInverse();
  147. Vector2D beforeStart = (start + Constants.FingerSliceOverlapFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  148. Vector2D behindEnd = (end + Constants.FingerSliceOverlapFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  149. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  150. if (slice.Length >= Constants.FingerMinWidth && slice.Length <= Constants.FingerMaxWidth && fingerSliceDepthTest(slice))
  151. return slice;
  152. return null;
  153. }
  154. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  155. {
  156. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  157. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  158. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  159. return (depthStart > depthMid && depthMid < depthEnd);
  160. }
  161. private void createFingerFromTrail(FingerSliceTrail trail)
  162. {
  163. //bring finger in correct direction Tip->Hand
  164. trail = orderTrailTipToHand(trail);
  165. //create finger
  166. Finger finger = new Finger(trail);
  167. //add finger
  168. if (!isCrippleFinger(finger))
  169. Fingers.Add(finger);
  170. //remove edges around detected finger to improve performance
  171. edgeImageAdapted.removeEdgesInsidePolygon(finger.Contour.ToArray());
  172. }
  173. private bool isCrippleFinger(Finger finger)
  174. {
  175. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  176. Vector2D direction = midSlice.Direction;
  177. Vector2D directionInv = direction.getInverse();
  178. Vector2D out1 = (midSlice.Start + Constants.FingerCrippleOutFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  179. Vector2D out2 = (midSlice.End + Constants.FingerCrippleOutFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  180. Int16 depthAtFinger = depthImage.getDepthAt(midSlice.Mid);
  181. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  182. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  183. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  184. return (minDepthDifference < Constants.FingerCrippleOutMinDifference);
  185. }
  186. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  187. {
  188. int numSlicesForDirectionDetection = Constants.FingerNumSlicesForDirectionDetection;
  189. int maxIndex = trail.NumSlices-1;
  190. float sumStart = 0;
  191. float sumEnd = 0;
  192. for (int i = 0; i < numSlicesForDirectionDetection; i++) {
  193. sumStart += trail[i].Length;
  194. sumEnd += trail[maxIndex - i].Length;
  195. }
  196. float avergaeStart = sumStart / numSlicesForDirectionDetection;
  197. float averageEnd = sumEnd / numSlicesForDirectionDetection;
  198. //check direction
  199. if (avergaeStart > averageEnd)
  200. trail.reverse();
  201. return trail;
  202. }
  203. private void drawFingers()
  204. {
  205. foreach (Finger finger in Fingers)
  206. {
  207. finger.draw(outputImage, false);
  208. }
  209. }
  210. }
  211. }