FingerDetector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. findFingers();
  29. }
  30. private void findFingers()
  31. {
  32. int maxX = depthImage.Width - 1;
  33. int maxY = depthImage.Height - 1;
  34. Fingers = new List<Finger>();
  35. for (int y = 1; y < maxY; y += 5) //y++ for 100% coverage, but y+=5 for 99% coverage and 3 times better perfomance
  36. {
  37. for (int x = 1; x < maxX; x++)
  38. {
  39. if (edgeImageAdapted.isEdgeAt(x, y))
  40. {
  41. Vector2D edgePoint = new Vector2D(x, y);
  42. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  43. if (edgeDirection != null)
  44. {
  45. Vector2D dir = edgeDirection.getOrthogonal(true);
  46. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  47. dir = dir.getInverse();
  48. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  49. if (slice != null)
  50. {
  51. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  52. if (trail != null && trail.NumSlices > Constants.FingerMinNumSlices)
  53. createFingerFromTrail(trail);
  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. int maxX = depthImage.Width - 1;
  73. int maxY = depthImage.Height - 1;
  74. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  75. Vector2D direction = startDirection;
  76. Vector2D position = startSlice.Mid + Constants.FingerStepSize * direction;
  77. if (position.isWithin(0, 0, maxX, maxY))
  78. {
  79. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  80. if (nextSlice != null)
  81. {
  82. trail.addSlice(nextSlice);
  83. trail = expandTrail(trail);
  84. if (trail.NumSlices > Constants.FingerMinNumSlices)
  85. {
  86. trail.Slices.RemoveRange(0, Constants.FingerRemoveNumSlicesForCorrection);
  87. trail.Slices.Reverse();
  88. trail = expandTrail(trail);
  89. trail.Slices.Reverse();
  90. return trail;
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. private FingerSliceTrail expandTrail(FingerSliceTrail trail)
  97. {
  98. int maxX = depthImage.Width - 1;
  99. int maxY = depthImage.Height - 1;
  100. Vector2D currentDirection = trail.getEndDirection();
  101. Vector2D currentPosition = trail.End.Mid + Constants.FingerStepSize * currentDirection;
  102. int gapCounter = 0;
  103. int numSlices = trail.NumSlices;
  104. FingerSlice lastSlice = trail.End;
  105. FingerSlice nextSlice;
  106. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
  107. {
  108. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  109. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  110. {
  111. gapCounter = 0;
  112. numSlices++;
  113. trail.addSlice(nextSlice);
  114. currentDirection = trail.getEndDirection();
  115. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  116. lastSlice = nextSlice;
  117. }
  118. else
  119. {
  120. gapCounter++;
  121. currentPosition += currentDirection;
  122. }
  123. }
  124. return trail;
  125. }
  126. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  127. {
  128. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  129. Vector2D dirStart = direction.getOrthogonal(true);
  130. Vector2D dirEnd = direction.getOrthogonal(false);
  131. Vector2D start = findNextEdge(position, dirStart);
  132. if (start == null) return null;
  133. Vector2D end = findNextEdge(position, dirEnd);
  134. if (end == null) return null;
  135. return getFingerSlice(start, end);
  136. }
  137. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  138. {
  139. Vector2D end = findNextEdge(start+3*direction, direction);
  140. if (end == null) return null;
  141. return getFingerSlice(start, end);
  142. }
  143. private Vector2D findNextEdge(Vector2D start, Vector2D direction, bool adaptedEdgeImage = true, bool stopAtMaxFingerSize = true, bool returnBoundIfNoEdge = false)
  144. {
  145. int maxX = depthImage.Width - 1;
  146. int maxY = depthImage.Height - 1;
  147. int maxStepsX;
  148. if (direction.X > 0)
  149. maxStepsX = (int)((maxX - start.X) / direction.X);
  150. else if (direction.X < 0)
  151. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  152. else
  153. maxStepsX = int.MaxValue;
  154. int maxStepsY;
  155. if (direction.Y > 0)
  156. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  157. else if (direction.Y < 0)
  158. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  159. else
  160. maxStepsY = int.MaxValue;
  161. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  162. if (stopAtMaxFingerSize)
  163. maxSteps = Math.Min(maxSteps, (int)(Constants.FingerMaxSize / direction.Length));
  164. Vector2D end = new Vector2D(start);
  165. for (int i = 0; i < maxSteps; i++)
  166. {
  167. end += direction;
  168. if ((adaptedEdgeImage && edgeImageAdapted.isRoughEdgeAt(end)) || (!adaptedEdgeImage && edgeImageOriginal.isRoughEdgeAt(end)))
  169. {
  170. return end;
  171. }
  172. }
  173. if (returnBoundIfNoEdge)
  174. return end;
  175. else
  176. return null;
  177. }
  178. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  179. {
  180. int maxX = depthImage.Width - 1;
  181. int maxY = depthImage.Height - 1;
  182. Vector2D direction = (end - start).normalize();
  183. Vector2D beforeStart = (start - direction).moveInBound(0, 0, maxX, maxY);
  184. Vector2D behindEnd = (end + direction).moveInBound(0, 0, maxY, maxY);
  185. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  186. if (slice.Length >= Constants.FingerMinSize && slice.Length <= Constants.FingerMaxSize && fingerSliceDepthTest(slice))
  187. return slice;
  188. return null;
  189. }
  190. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  191. {
  192. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  193. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  194. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  195. return (depthStart > depthMid && depthMid < depthEnd);
  196. }
  197. private void createFingerFromTrail(FingerSliceTrail trail)
  198. {
  199. //bring finger in correct direction Tip->Hand
  200. trail = orderTrailTipToHand(trail);
  201. //create finger
  202. Finger finger = new Finger(trail);
  203. //add finger
  204. Fingers.Add(finger);
  205. //draw finger
  206. drawDetectedFinger(finger);
  207. //remove edges around detected finger to improve performance
  208. Point[] polygon = finger.getBoundingPolygon();
  209. edgeImageAdapted.removeEdgesInsidePolygon(polygon);
  210. }
  211. private FingerSlice findOutSlice(Vector2D start, Vector2D direction)
  212. {
  213. int maxX = depthImage.Width - 1;
  214. int maxY = depthImage.Height - 1;
  215. Vector2D dirOrth1 = direction.getOrthogonal(true);
  216. Vector2D dirOrth2 = direction.getOrthogonal(false);
  217. Vector2D outPoint = (start + Constants.FingerOutSliceFactor * Constants.FingerStepSize * direction).moveInBound(0, 0, maxX, maxY);
  218. Vector2D p1 = findNextEdge(outPoint, dirOrth1, false, false, true);
  219. Vector2D p2 = findNextEdge(outPoint, dirOrth2, false, false, true);
  220. FingerSlice slice = new FingerSlice(p1, p2);
  221. outputImage.drawLineSegment(slice.LineSegment, Constants.FingerOutSliceColor);
  222. return slice;
  223. }
  224. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  225. {
  226. int maxX = depthImage.Width - 1;
  227. int maxY = depthImage.Height - 1;
  228. FingerSlice start = trail.Start;
  229. FingerSlice end = trail.End;
  230. Vector2D direction = (end.Mid - start.Mid).normalize();
  231. FingerSlice startOutSlice = findOutSlice(start.Mid, -1 * direction);
  232. FingerSlice endOutSlice = findOutSlice(end.Mid, direction);
  233. float startOutLength = float.MaxValue;
  234. float endOutLength = float.MaxValue;
  235. if (!startOutSlice.Start.isOnBound(0, 0, maxX, maxY) && startOutSlice.End.isOnBound(0, 0, maxX, maxY))
  236. startOutLength = startOutSlice.Length;
  237. if (!endOutSlice.Start.isOnBound(0, 0, maxX, maxY) && endOutSlice.End.isOnBound(0, 0, maxX, maxY))
  238. endOutLength = endOutSlice.Length;
  239. if (startOutLength < endOutLength)
  240. trail.Slices.Reverse();
  241. return trail;
  242. }
  243. private void drawDetectedFinger(Finger finger)
  244. {
  245. FingerSliceTrail trail = finger.SliceTrail;
  246. for (int i = 0; i < trail.NumSlices; i++)
  247. {
  248. outputImage.drawLineSegment(trail.Slices[i].LineSegment, Constants.FingerSliceColor);
  249. }
  250. outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
  251. }
  252. }
  253. }