FingerDetector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.Detectors.FingerDetection
  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(true);
  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, true);
  90. trail.reverse();
  91. return trail;
  92. }
  93. }
  94. }
  95. return null;
  96. }
  97. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  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. if (reversed)
  108. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection.getInverse());
  109. else
  110. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  111. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  112. {
  113. gapCounter = 0;
  114. numSlices++;
  115. trail.addSlice(nextSlice);
  116. currentDirection = trail.getEndDirection();
  117. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  118. lastSlice = nextSlice;
  119. }
  120. else
  121. {
  122. gapCounter++;
  123. currentPosition += currentDirection;
  124. }
  125. }
  126. return trail;
  127. }
  128. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  129. {
  130. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  131. Vector2D dirStart = direction.getOrthogonal(true);
  132. Vector2D dirEnd = direction.getOrthogonal(false);
  133. Vector2D start = findNextEdge(position, dirStart);
  134. if (start == null) return null;
  135. Vector2D end = findNextEdge(position, dirEnd);
  136. if (end == null) return null;
  137. return getFingerSlice(start, end);
  138. }
  139. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  140. {
  141. Vector2D end = findNextEdge(start + Constants.FingerSliceOverlapFactor * direction, direction);
  142. if (end == null) return null;
  143. return getFingerSlice(start, end);
  144. }
  145. private Vector2D findNextEdge(Vector2D start, Vector2D direction, bool adaptedEdgeImage = true, bool stopAtMaxFingerSize = true, bool returnBoundIfNoEdge = false)
  146. {
  147. Vector2D max = depthImage.BottomRight;
  148. Vector2D maxGrow = (max - start) / direction;
  149. Vector2D maxDecline = start / direction.getAbsolute();
  150. int maxStepsX;
  151. if (direction.X > 0)
  152. maxStepsX = maxGrow.IntX;
  153. else if (direction.X < 0)
  154. maxStepsX = maxDecline.IntX;
  155. else
  156. maxStepsX = int.MaxValue;
  157. int maxStepsY;
  158. if (direction.Y > 0)
  159. maxStepsY = maxGrow.IntY;
  160. else if (direction.Y < 0)
  161. maxStepsY = maxDecline.IntY;
  162. else
  163. maxStepsY = int.MaxValue;
  164. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  165. if (stopAtMaxFingerSize)
  166. maxSteps = Math.Min(maxSteps, (int)(Constants.FingerMaxSize / direction.Length));
  167. Vector2D end = new Vector2D(start);
  168. for (int i = 0; i < maxSteps; i++)
  169. {
  170. end += direction;
  171. if ((adaptedEdgeImage && edgeImageAdapted.isRoughEdgeAt(end)) || (!adaptedEdgeImage && edgeImageOriginal.isRoughEdgeAt(end)))
  172. {
  173. return end;
  174. }
  175. }
  176. if (returnBoundIfNoEdge)
  177. return end;
  178. else
  179. return null;
  180. }
  181. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  182. {
  183. int maxX = depthImage.Width - 1;
  184. int maxY = depthImage.Height - 1;
  185. Vector2D direction = (end - start).normalize();
  186. Vector2D directionInv = direction.getInverse();
  187. Vector2D beforeStart = (start + Constants.FingerSliceOverlapFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  188. Vector2D behindEnd = (end + Constants.FingerSliceOverlapFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  189. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  190. if (slice.Length >= Constants.FingerMinSize && slice.Length <= Constants.FingerMaxSize && fingerSliceDepthTest(slice))
  191. return slice;
  192. return null;
  193. }
  194. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  195. {
  196. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  197. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  198. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  199. return (depthStart > depthMid && depthMid < depthEnd);
  200. }
  201. private void createFingerFromTrail(FingerSliceTrail trail)
  202. {
  203. //bring finger in correct direction Tip->Hand
  204. trail = orderTrailTipToHand(trail);
  205. //create finger
  206. Finger finger = new Finger(trail);
  207. //add finger
  208. if (!isCrippleFinger(finger))
  209. Fingers.Add(finger);
  210. //remove edges around detected finger to improve performance
  211. edgeImageAdapted.removeEdgesInsidePolygon(finger.Contour.ToArray());
  212. }
  213. private bool isCrippleFinger(Finger finger)
  214. {
  215. int maxX = depthImage.Width - 1;
  216. int maxY = depthImage.Height - 1;
  217. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  218. Vector2D direction = midSlice.Direction;
  219. Vector2D directionInv = direction.getInverse();
  220. Vector2D out1 = (midSlice.Start + Constants.FingerCrippleOutFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  221. Vector2D out2 = (midSlice.End + Constants.FingerCrippleOutFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  222. Int16 depthAtFinger = depthImage.getDepthAt(midSlice.Mid);
  223. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  224. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  225. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  226. return (minDepthDifference < Constants.FingerCrippleOutMinDifference);
  227. }
  228. private FingerSlice findOutSlice(Vector2D start, Vector2D direction)
  229. {
  230. int maxX = depthImage.Width - 1;
  231. int maxY = depthImage.Height - 1;
  232. Vector2D dirOrth1 = direction.getOrthogonal(true);
  233. Vector2D dirOrth2 = direction.getOrthogonal(false);
  234. Vector2D outPoint = (start + Constants.FingerOutSliceFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction.getInverse());
  235. Vector2D p1 = findNextEdge(outPoint, dirOrth1, false, false, true);
  236. Vector2D p2 = findNextEdge(outPoint, dirOrth2, false, false, true);
  237. FingerSlice slice = new FingerSlice(p1, p2);
  238. return slice;
  239. }
  240. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  241. {
  242. int maxX = depthImage.Width - 2;
  243. int maxY = depthImage.Height - 2;
  244. FingerSlice start = trail.StartSlice;
  245. FingerSlice end = trail.EndSlice;
  246. Vector2D direction = (end.Mid - start.Mid).normalize();
  247. FingerSlice startOutSlice = findOutSlice(start.Mid, direction.getInverse());
  248. FingerSlice endOutSlice = findOutSlice(end.Mid, direction);
  249. float startOutLength = float.MaxValue;
  250. float endOutLength = float.MaxValue;
  251. if (startOutSlice.Start.isInBound(Vector2D.Zero, depthImage.BottomRight) && startOutSlice.End.isInBound(Vector2D.Zero, depthImage.BottomRight))
  252. startOutLength = startOutSlice.Length;
  253. if (endOutSlice.Start.isInBound(Vector2D.Zero, depthImage.BottomRight) && endOutSlice.End.isInBound(Vector2D.Zero, depthImage.BottomRight))
  254. endOutLength = endOutSlice.Length;
  255. if (startOutLength <= endOutLength)
  256. {
  257. trail.reverse();
  258. outputImage.drawLineSegment(startOutSlice.LineSegment, Constants.FingerHandOutSliceColor);
  259. outputImage.drawLineSegment(endOutSlice.LineSegment, Constants.FingerTipOutSliceColor);
  260. }
  261. else
  262. {
  263. outputImage.drawLineSegment(startOutSlice.LineSegment, Constants.FingerTipOutSliceColor);
  264. outputImage.drawLineSegment(endOutSlice.LineSegment, Constants.FingerHandOutSliceColor);
  265. }
  266. return trail;
  267. }
  268. private void drawFingers()
  269. {
  270. foreach (Finger finger in Fingers)
  271. {
  272. drawFinger(finger);
  273. }
  274. }
  275. private void drawFinger(Finger finger)
  276. {
  277. FingerSliceTrail trail = finger.SliceTrail;
  278. for (int i = 0; i < trail.NumSlices; i++)
  279. {
  280. outputImage.drawLineSegment(trail[i].LineSegment, Constants.FingerSliceColor);
  281. }
  282. outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
  283. outputImage.drawContour(finger.Contour, Constants.FingerContourColor, 1);
  284. }
  285. }
  286. }