FingerDetector.cs 12 KB

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