FingerDetector.cs 9.8 KB

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