FingerDetector.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Vector2D maxPixel = Parameters.ImageMaxPixel;
  31. int maxX = maxPixel.IntX;
  32. int maxY = maxPixel.IntY;
  33. Fingers = new List<Finger>();
  34. for (int y = 1; y < maxY; y += 5)
  35. {
  36. for (int x = 1; x < maxX; x++)
  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. }
  61. private Vector2D getEdgeDirection(Vector2D edgePoint)
  62. {
  63. int x = edgePoint.IntX;
  64. int y = edgePoint.IntY;
  65. if (edgeImageAdapted.isEdgeAt(x, y - 1) && edgeImageAdapted.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  66. else if (edgeImageAdapted.isEdgeAt(x - 1, y) && edgeImageAdapted.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  67. else if (edgeImageAdapted.isEdgeAt(x - 1, y - 1) && edgeImageAdapted.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  68. else if (edgeImageAdapted.isEdgeAt(x + 1, y - 1) && edgeImageAdapted.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  69. else return null;
  70. }
  71. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  72. {
  73. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  74. Vector2D direction = startDirection;
  75. Vector2D position = startSlice.Mid + Parameters.FingerStepSize * direction;
  76. if (position.isInBound())
  77. {
  78. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  79. if (nextSlice != null)
  80. {
  81. trail.addSlice(nextSlice);
  82. trail = expandTrail(trail);
  83. if (trail.NumSlices > Parameters.FingerMinNumSlices / 2)
  84. {
  85. trail.removeFirstSlices(Parameters.FingerRemoveNumSlicesForCorrection);
  86. trail.reverse();
  87. trail = expandTrail(trail, true);
  88. trail.reverse();
  89. return trail;
  90. }
  91. }
  92. }
  93. return null;
  94. }
  95. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  96. {
  97. Vector2D currentDirection = trail.getEndDirection();
  98. Vector2D currentPosition = trail.EndSlice.Mid + Parameters.FingerStepSize * currentDirection;
  99. int gapCounter = 0;
  100. int numSlices = trail.NumSlices;
  101. FingerSlice lastSlice = trail.EndSlice;
  102. FingerSlice nextSlice;
  103. while (currentPosition.isInBound() && gapCounter <= Math.Min(numSlices, Parameters.FingerMaxGapCounter))
  104. {
  105. Vector2D direction = (reversed) ? currentDirection.getInverse() : currentDirection;
  106. nextSlice = findFingerSliceFromMid(currentPosition, direction);
  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. return trail;
  123. }
  124. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  125. {
  126. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  127. Vector2D dirStart = direction.getOrthogonal(true);
  128. Vector2D dirEnd = direction.getOrthogonal(false);
  129. Vector2D start = edgeImageAdapted.findNextEdge(position, dirStart, Parameters.FingerMaxWidth);
  130. if (start == null) return null;
  131. Vector2D end = edgeImageAdapted.findNextEdge(position, dirEnd, Parameters.FingerMaxWidth);
  132. if (end == null) return null;
  133. return getFingerSlice(start, end);
  134. }
  135. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  136. {
  137. Vector2D searchStart = start + Parameters.FingerContourMargin * direction;
  138. Vector2D end = edgeImageAdapted.findNextEdge(searchStart, direction, Parameters.FingerMaxWidth);
  139. if (end == null)
  140. return null;
  141. return getFingerSlice(start, end);
  142. }
  143. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  144. {
  145. FingerSlice slice = new FingerSlice(start, end);
  146. if (slice.Length >= Parameters.FingerMinWidth && slice.Length <= Parameters.FingerMaxWidth && fingerSliceDepthTest(slice))
  147. return slice;
  148. return null;
  149. }
  150. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  151. {
  152. Int16 depthStart = depthImage.getDepthAt(fingerSlice.ContourStart);
  153. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  154. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.ContourEnd);
  155. return (depthStart > depthMid && depthMid < depthEnd);
  156. }
  157. private void createFingerFromTrail(FingerSliceTrail trail)
  158. {
  159. //bring finger in correct direction Tip->Hand
  160. trail = orderTrailTipToHand(trail);
  161. //create finger
  162. Finger finger = new Finger(trail);
  163. //add finger
  164. if (!isCrippleFinger(finger))
  165. Fingers.Add(finger);
  166. //remove edges around detected finger to improve performance
  167. edgeImageAdapted.removeEdgesInsidePolygon(finger.Contour.ToArray());
  168. }
  169. private bool isCrippleFinger(Finger finger)
  170. {
  171. Int16 depthAtFinger = depthImage.getDepthAt(finger.MidPoint);
  172. Int16 depthAtOut1 = depthImage.getDepthAt(finger.SliceTrail.MidSlice.OutStart);
  173. Int16 depthAtOut2 = depthImage.getDepthAt(finger.SliceTrail.MidSlice.OutEnd);
  174. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  175. return (minDepthDifference < Parameters.FingerCrippleMinDifference);
  176. }
  177. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  178. {
  179. int numSlicesForDirectionDetection = Parameters.FingerNumSlicesForDirectionDetection;
  180. int maxIndex = trail.NumSlices - 1;
  181. float sumStart = 0;
  182. float sumEnd = 0;
  183. for (int i = 0; i < numSlicesForDirectionDetection; i++)
  184. {
  185. sumStart += trail[i].Length;
  186. sumEnd += trail[maxIndex - i].Length;
  187. }
  188. float avergaeStart = sumStart / numSlicesForDirectionDetection;
  189. float averageEnd = sumEnd / numSlicesForDirectionDetection;
  190. //check direction
  191. if (avergaeStart > averageEnd)
  192. trail.reverse();
  193. return trail;
  194. }
  195. }
  196. }