FingerDetector.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, true);
  87. trail.reverse();
  88. return trail;
  89. }
  90. }
  91. }
  92. return null;
  93. }
  94. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  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. Vector2D direction = (reversed) ? currentDirection.getInverse() : currentDirection;
  105. nextSlice = findFingerSliceFromMid(currentPosition, direction);
  106. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  107. {
  108. gapCounter = 0;
  109. numSlices++;
  110. trail.addSlice(nextSlice);
  111. currentDirection = trail.getEndDirection();
  112. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  113. lastSlice = nextSlice;
  114. }
  115. else
  116. {
  117. gapCounter++;
  118. currentPosition += currentDirection;
  119. }
  120. }
  121. return trail;
  122. }
  123. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  124. {
  125. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  126. Vector2D dirStart = direction.getOrthogonal(true);
  127. Vector2D dirEnd = direction.getOrthogonal(false);
  128. Vector2D start = edgeImageAdapted.findNextEdge(position, dirStart, Constants.FingerMaxWidth);
  129. if (start == null) return null;
  130. Vector2D end = edgeImageAdapted.findNextEdge(position, dirEnd, Constants.FingerMaxWidth);
  131. if (end == null) return null;
  132. return getFingerSlice(start, end);
  133. }
  134. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  135. {
  136. Vector2D searchStart = start + Constants.FingerSliceOverlapFactor * direction;
  137. Vector2D end = edgeImageAdapted.findNextEdge(searchStart, direction, Constants.FingerMaxWidth);
  138. if (end == null) return null;
  139. return getFingerSlice(start, end);
  140. }
  141. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  142. {
  143. Vector2D direction = (end - start).normalize();
  144. Vector2D directionInv = direction.getInverse();
  145. Vector2D beforeStart = (start + Constants.FingerSliceOverlapFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  146. Vector2D behindEnd = (end + Constants.FingerSliceOverlapFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  147. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  148. if (slice.Length >= Constants.FingerMinWidth && slice.Length <= Constants.FingerMaxWidth && fingerSliceDepthTest(slice))
  149. return slice;
  150. return null;
  151. }
  152. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  153. {
  154. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  155. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  156. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  157. return (depthStart > depthMid && depthMid < depthEnd);
  158. }
  159. private void createFingerFromTrail(FingerSliceTrail trail)
  160. {
  161. //bring finger in correct direction Tip->Hand
  162. trail = orderTrailTipToHand(trail);
  163. //create finger
  164. Finger finger = new Finger(trail);
  165. //add finger
  166. if (!isCrippleFinger(finger))
  167. Fingers.Add(finger);
  168. //remove edges around detected finger to improve performance
  169. edgeImageAdapted.removeEdgesInsidePolygon(finger.Contour.ToArray());
  170. }
  171. private bool isCrippleFinger(Finger finger)
  172. {
  173. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  174. Vector2D direction = midSlice.Direction;
  175. Vector2D directionInv = direction.getInverse();
  176. Vector2D out1 = (midSlice.Start + Constants.FingerCrippleOutFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  177. Vector2D out2 = (midSlice.End + Constants.FingerCrippleOutFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  178. Int16 depthAtFinger = depthImage.getDepthAt(midSlice.Mid);
  179. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  180. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  181. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  182. return (minDepthDifference < Constants.FingerCrippleOutMinDifference);
  183. }
  184. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  185. {
  186. int numSlicesForDirectionDetection = Constants.FingerNumSlicesForDirectionDetection;
  187. int maxIndex = trail.NumSlices - 1;
  188. float sumStart = 0;
  189. float sumEnd = 0;
  190. for (int i = 0; i < numSlicesForDirectionDetection; i++)
  191. {
  192. sumStart += trail[i].Length;
  193. sumEnd += trail[maxIndex - i].Length;
  194. }
  195. float avergaeStart = sumStart / numSlicesForDirectionDetection;
  196. float averageEnd = sumEnd / numSlicesForDirectionDetection;
  197. //check direction
  198. if (avergaeStart > averageEnd)
  199. trail.reverse();
  200. return trail;
  201. }
  202. }
  203. }