FingerDetector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /// <summary>
  16. /// Detects fingers in the given depth and edge images. The finger detection searches for edges in the edge image and tries to find an initial finger slice. For each found finger slice, the finger detector tries to move along the finger direction to extend the finger slice trail. If the trail reaches its end, the finger detector removes the first few slices and starts the trail expansion in opposite direction. If the complete slice trail is long enough, the finger slices are sorted into correct order and the edges around the finger are removed to increase performance for the next finger detection.
  17. /// </summary>
  18. class FingerDetector
  19. {
  20. /// <summary>
  21. /// the coordinateConverter (used to calculate finger width (in milimeters))
  22. /// </summary>
  23. private CoordinateConverter coordinateConverter;
  24. /// <summary>
  25. /// the current depthImage
  26. /// </summary>
  27. private DepthImage depthImage;
  28. /// <summary>
  29. /// the current edge image in its original form
  30. /// </summary>
  31. private EdgeImage edgeImageOriginal;
  32. /// <summary>
  33. /// the current edge image in its adapted form (edges around detected fingers will be removed)
  34. /// </summary>
  35. private EdgeImage edgeImageAdapted;
  36. /// <summary>
  37. /// the detected fingers
  38. /// </summary>
  39. private List<Finger> fingers;
  40. /// <summary>
  41. /// Initializes a new instance of the FingerDetector class.
  42. /// </summary>
  43. /// <param name="coordinateConverter">The coordinate converter.</param>
  44. public FingerDetector(CoordinateConverter coordinateConverter) {
  45. this.coordinateConverter = coordinateConverter;
  46. }
  47. /// <summary>
  48. /// Detects fingers in the current frame using the depth and edge image. Afterwards the detected fingers are stored in the given frame data (detectedFingers).
  49. /// </summary>
  50. /// <param name="frameData">the current frame</param>
  51. public void detectFingers(FrameData frameData)
  52. {
  53. depthImage = frameData.DepthImage;
  54. edgeImageOriginal = frameData.EdgeImage;
  55. edgeImageAdapted = frameData.EdgeImage.copy();
  56. fingers = new List<Finger>();
  57. Vector2D maxPixel = depthImage.Size.MaxPixel;
  58. int maxX = maxPixel.IntX;
  59. int maxY = maxPixel.IntY;
  60. for (int y = 1; y < maxY; y += 4)
  61. {
  62. for (int x = 1; x < maxX; x += 2)
  63. {
  64. if (edgeImageAdapted.isEdgeAt(x, y))
  65. {
  66. Vector2D edgePoint = new Vector2D(x, y);
  67. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  68. if (edgeDirection != null)
  69. {
  70. Vector2D dir = edgeDirection.getOrthogonal();
  71. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  72. dir = dir.getInverse();
  73. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  74. if (slice != null)
  75. {
  76. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  77. if (trail != null && trail.NumSlices > Parameters.FingerMinNumSlices)
  78. {
  79. createFingerFromTrail(trail);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. frameData.DetectedFingers = fingers;
  87. }
  88. /// <summary>
  89. /// Gets the edge direction of the given edge point. The edge direction is either horizontal, vertical, diagonal (both) or null.
  90. /// </summary>
  91. /// <param name="edgePoint">the edge point</param>
  92. /// <returns>the edge directon at the given point</returns>
  93. private Vector2D getEdgeDirection(Vector2D edgePoint)
  94. {
  95. int x = edgePoint.IntX;
  96. int y = edgePoint.IntY;
  97. if (edgeImageAdapted.isEdgeAt(x, y - 1) && edgeImageAdapted.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  98. else if (edgeImageAdapted.isEdgeAt(x - 1, y) && edgeImageAdapted.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  99. else if (edgeImageAdapted.isEdgeAt(x - 1, y - 1) && edgeImageAdapted.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  100. else if (edgeImageAdapted.isEdgeAt(x + 1, y - 1) && edgeImageAdapted.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  101. else return null;
  102. }
  103. /// <summary>
  104. /// Creates a new FingerSliceTrail and expands it along its direction.
  105. /// </summary>
  106. /// <param name="startSlice">the initial finger slice</param>
  107. /// <param name="startDirection">the initial finger direction</param>
  108. /// <returns>the found FingerSliceTrail or null</returns>
  109. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  110. {
  111. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  112. Vector2D direction = startDirection;
  113. Vector2D position = startSlice.Mid + Parameters.FingerStepSize * direction;
  114. if (position.isInBound(depthImage.Size))
  115. {
  116. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  117. if (nextSlice != null)
  118. {
  119. trail.addSlice(nextSlice);
  120. trail = expandTrail(trail);
  121. if (trail.NumSlices > Parameters.FingerMinNumSlices / 2)
  122. {
  123. trail.removeFirstSlices(Parameters.FingerRemoveNumSlicesForCorrection);
  124. trail = expandTrail(trail, true);
  125. return trail;
  126. }
  127. }
  128. }
  129. return null;
  130. }
  131. /// <summary>
  132. /// Tries to expand a trail along its direction.
  133. /// </summary>
  134. /// <param name="trail">the given finger slice trail</param>
  135. /// <param name="reversed">indicates in which direction the trail should be expanded</param>
  136. /// <returns>the expanded finger slice trail</returns>
  137. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  138. {
  139. if (reversed)
  140. trail.reverse();
  141. Vector2D currentDirection = trail.getEndDirection();
  142. Vector2D currentPosition = trail.EndSlice.Mid + Parameters.FingerStepSize * currentDirection;
  143. int gapCounter = 0;
  144. int numSlices = trail.NumSlices;
  145. FingerSlice lastSlice = trail.EndSlice;
  146. FingerSlice nextSlice;
  147. while (currentPosition.isInBound(depthImage.Size) && gapCounter < Math.Min(numSlices, Parameters.FingerMaxGapCounter))
  148. {
  149. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection, reversed);
  150. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Parameters.FingerMaxSliceDifferencePerStep)
  151. {
  152. gapCounter = 0;
  153. numSlices++;
  154. trail.addSlice(nextSlice);
  155. currentDirection = trail.getEndDirection();
  156. currentPosition = nextSlice.Mid + Parameters.FingerStepSize * currentDirection;
  157. lastSlice = nextSlice;
  158. }
  159. else
  160. {
  161. gapCounter++;
  162. currentPosition += currentDirection;
  163. }
  164. }
  165. if (reversed)
  166. trail.reverse();
  167. return trail;
  168. }
  169. /// <summary>
  170. /// Tries to find a finger slice from a given position by searching in both orthogonal directions for an edge.
  171. /// </summary>
  172. /// <param name="position">the position somewhere in the middle of the possible finger</param>
  173. /// <param name="direction">the finger direction</param>
  174. /// <param name="reversed">indicates wether start and end should be swapped</param>
  175. /// <returns>the found finger slice or null</returns>
  176. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction, bool reversed = false)
  177. {
  178. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  179. Int16 depth = depthImage.getDepthAt(position);
  180. int maxWidth2D = (int)coordinateConverter.convertLength3Dto2D(Parameters.FingerMaxWidth3D, depth);
  181. Vector2D dirStart = direction.getOrthogonal(reversed);
  182. Vector2D dirEnd = direction.getOrthogonal(!reversed);
  183. Vector2D startPositionStart = position + 0.5f*Parameters.FingerMinWidth2D * dirStart;
  184. Vector2D start = edgeImageAdapted.findNextRoughEdge(startPositionStart, dirStart, maxWidth2D);
  185. if (start == null) return null;
  186. Vector2D endPositionStart = position + 0.5f*Parameters.FingerMinWidth2D * dirEnd;
  187. Vector2D end = edgeImageAdapted.findNextRoughEdge(position, dirEnd, maxWidth2D);
  188. if (end == null) return null;
  189. FingerSlice slice = new FingerSlice(start, end);
  190. if (!fingerSliceDepthTest(slice) || slice.Length > maxWidth2D)
  191. return null;
  192. return slice;
  193. }
  194. /// <summary>
  195. /// Tries to find a finger slice from one point towards the given direction (searches for an edge).
  196. /// </summary>
  197. /// <param name="start">the start position</param>
  198. /// <param name="direction">the slice direction</param>
  199. /// <returns>the found finger slice or null</returns>
  200. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  201. {
  202. Vector2D searchStart = start + Parameters.FingerMinWidth2D * direction;
  203. Int16 depth = depthImage.getDepthAt(start);
  204. int maxWidth2D = (int)coordinateConverter.convertLength3Dto2D(Parameters.FingerMaxWidth3D, depth);
  205. Vector2D end = edgeImageAdapted.findNextRoughEdge(searchStart, direction, maxWidth2D);
  206. if (end == null)
  207. return null;
  208. FingerSlice slice = new FingerSlice(start, end);
  209. if (!fingerSliceDepthTest(slice))
  210. return null;
  211. return slice;
  212. }
  213. /// <summary>
  214. /// Checks if a possible finger slice is located on a finger. To pass this test, the depth value at the mid has to be lower than on the outside (start and end).
  215. /// </summary>
  216. /// <param name="slice">the possible finger slcie</param>
  217. /// <returns>wether the slice is located on a finger</returns>
  218. private bool fingerSliceDepthTest(FingerSlice slice)
  219. {
  220. Int16 depthStart = depthImage.getDepthAt(slice.Start.moveWithinBound(depthImage.Size, slice.Direction.getInverse(), Parameters.FingerContourMargin));
  221. Int16 depthMid = depthImage.getDepthAt(slice.Mid);
  222. Int16 depthEnd = depthImage.getDepthAt(slice.End.moveWithinBound(depthImage.Size, slice.Direction, Parameters.FingerContourMargin));
  223. return (depthStart > depthMid && depthMid < depthEnd);
  224. }
  225. /// <summary>
  226. /// Sorts the finger slices in correct order an checks if the finger is a valid finger (<see cref="isCrippleFinger(Finger)"/>). If it is valid the finger is added to the list of detected fingers. Afterwards the edges around the finger are removed to surpress a new finger search for the same finger.
  227. /// </summary>
  228. /// <param name="trail">the slice trail of the possible finger</param>
  229. private void createFingerFromTrail(FingerSliceTrail trail)
  230. {
  231. //bring finger in correct direction Tip->Hand
  232. trail = orderTrailTipToHand(trail);
  233. //create finger
  234. Finger finger = new Finger(trail);
  235. //add finger
  236. if (!isCrippleFinger(finger))
  237. fingers.Add(finger);
  238. //remove edges around detected finger to improve performance
  239. edgeImageAdapted.removeEdgesInsidePolygon(finger.getContour(Parameters.FingerContourMargin).ToArray());
  240. }
  241. /// <summary>
  242. /// Checks wether the finger has enough space around itself (fingers from a closed hand shouldn't bee detected).
  243. /// </summary>
  244. /// <param name="finger">the finger</param>
  245. /// <returns>wether the finger has enough space around itself</returns>
  246. private bool isCrippleFinger(Finger finger)
  247. {
  248. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  249. Vector2D out1 = midSlice.Start.moveWithinBound(depthImage.Size, midSlice.Direction.getInverse(), Parameters.FingerOutMargin);
  250. Vector2D out2 = midSlice.End.moveWithinBound(depthImage.Size, midSlice.Direction, Parameters.FingerOutMargin);
  251. Int16 depthAtFinger = depthImage.getDepthAt(finger.MidPoint);
  252. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  253. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  254. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  255. return (minDepthDifference < Parameters.FingerMaxCrippleDifference);
  256. }
  257. /// <summary>
  258. /// Sorts a slice trail in the corrent order, so that the start is at the finger tip and the end is at the hand. To guess the correct order the width of the first and last slice are compared.
  259. /// </summary>
  260. /// <param name="trail">the slice trail of the finger</param>
  261. /// <returns>the slice trail of the finger in correct order</returns>
  262. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  263. {
  264. float startLength = trail.StartSlice.Length;
  265. float endLength = trail.EndSlice.Length;
  266. if (startLength > endLength)
  267. trail.reverse();
  268. return trail;
  269. }
  270. }
  271. }