FingerDetector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. Console.WriteLine("---");
  25. this.depthImage = depthImage;
  26. this.edgeImageOriginal = edgeImage;
  27. this.edgeImageAdapted = edgeImage.copy();
  28. this.outputImage = outputImage;
  29. detectFingers();
  30. drawFingers();
  31. }
  32. private void detectFingers()
  33. {
  34. int maxX = depthImage.Width - 1;
  35. int maxY = depthImage.Height - 1;
  36. Fingers = new List<Finger>();
  37. for (int y = 1; y < maxY; y += 5)
  38. {
  39. for (int x = 1; x < maxX; x++)
  40. {
  41. if (edgeImageAdapted.isEdgeAt(x, y))
  42. {
  43. Vector2D edgePoint = new Vector2D(x, y);
  44. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  45. if (edgeDirection != null)
  46. {
  47. Vector2D dir = edgeDirection.getOrthogonal(true);
  48. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  49. dir = dir.getInverse();
  50. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  51. if (slice != null)
  52. {
  53. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  54. if (trail != null && trail.NumSlices > Constants.FingerMinNumSlices)
  55. {
  56. createFingerFromTrail(trail);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. private Vector2D getEdgeDirection(Vector2D edgePoint)
  65. {
  66. int x = edgePoint.IntX;
  67. int y = edgePoint.IntY;
  68. if (edgeImageAdapted.isEdgeAt(x, y - 1) && edgeImageAdapted.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  69. else if (edgeImageAdapted.isEdgeAt(x - 1, y) && edgeImageAdapted.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  70. else if (edgeImageAdapted.isEdgeAt(x - 1, y - 1) && edgeImageAdapted.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  71. else if (edgeImageAdapted.isEdgeAt(x + 1, y - 1) && edgeImageAdapted.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  72. else return null;
  73. }
  74. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  75. {
  76. int maxX = depthImage.Width - 1;
  77. int maxY = depthImage.Height - 1;
  78. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  79. Vector2D direction = startDirection;
  80. Vector2D position = startSlice.Mid + Constants.FingerStepSize * direction;
  81. if (position.isWithin(0, 0, maxX, maxY))
  82. {
  83. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  84. if (nextSlice != null)
  85. {
  86. trail.addSlice(nextSlice);
  87. trail = expandTrail(trail);
  88. if (trail.NumSlices > Constants.FingerMinNumSlices / 2)
  89. {
  90. trail.removeFirstSlices(Constants.FingerRemoveNumSlicesForCorrection);
  91. trail.reverse();
  92. trail = expandTrail(trail, true);
  93. trail.reverse();
  94. return trail;
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
  101. {
  102. int maxX = depthImage.Width - 1;
  103. int maxY = depthImage.Height - 1;
  104. Vector2D currentDirection = trail.getEndDirection();
  105. Vector2D currentPosition = trail.EndSlice.Mid + Constants.FingerStepSize * currentDirection;
  106. int gapCounter = 0;
  107. int numSlices = trail.NumSlices;
  108. FingerSlice lastSlice = trail.EndSlice;
  109. FingerSlice nextSlice;
  110. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
  111. {
  112. if (reversed)
  113. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection.getInverse());
  114. else
  115. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  116. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  117. {
  118. gapCounter = 0;
  119. numSlices++;
  120. trail.addSlice(nextSlice);
  121. currentDirection = trail.getEndDirection();
  122. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  123. lastSlice = nextSlice;
  124. }
  125. else
  126. {
  127. gapCounter++;
  128. currentPosition += currentDirection;
  129. }
  130. }
  131. return trail;
  132. }
  133. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  134. {
  135. if (edgeImageAdapted.isRoughEdgeAt(position)) return null;
  136. Vector2D dirStart = direction.getOrthogonal(true);
  137. Vector2D dirEnd = direction.getOrthogonal(false);
  138. Vector2D start = findNextEdge(position, dirStart);
  139. if (start == null) return null;
  140. Vector2D end = findNextEdge(position, dirEnd);
  141. if (end == null) return null;
  142. return getFingerSlice(start, end);
  143. }
  144. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  145. {
  146. Vector2D end = findNextEdge(start + Constants.FingerSliceOverlapFactor * direction, direction);
  147. if (end == null) return null;
  148. return getFingerSlice(start, end);
  149. }
  150. private Vector2D findNextEdge(Vector2D start, Vector2D direction, bool adaptedEdgeImage = true, bool stopAtMaxFingerSize = true, bool returnBoundIfNoEdge = false)
  151. {
  152. int maxX = depthImage.Width - 1;
  153. int maxY = depthImage.Height - 1;
  154. int maxStepsX;
  155. if (direction.X > 0)
  156. maxStepsX = (int)((maxX - start.X) / direction.X);
  157. else if (direction.X < 0)
  158. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  159. else
  160. maxStepsX = int.MaxValue;
  161. int maxStepsY;
  162. if (direction.Y > 0)
  163. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  164. else if (direction.Y < 0)
  165. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  166. else
  167. maxStepsY = int.MaxValue;
  168. int maxSteps = Math.Min(maxStepsX, maxStepsY);
  169. if (stopAtMaxFingerSize)
  170. maxSteps = Math.Min(maxSteps, (int)(Constants.FingerMaxSize / direction.Length));
  171. Vector2D end = new Vector2D(start);
  172. for (int i = 0; i < maxSteps; i++)
  173. {
  174. end += direction;
  175. if ((adaptedEdgeImage && edgeImageAdapted.isRoughEdgeAt(end)) || (!adaptedEdgeImage && edgeImageOriginal.isRoughEdgeAt(end)))
  176. {
  177. return end;
  178. }
  179. }
  180. if (returnBoundIfNoEdge)
  181. return end;
  182. else
  183. return null;
  184. }
  185. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  186. {
  187. int maxX = depthImage.Width - 1;
  188. int maxY = depthImage.Height - 1;
  189. Vector2D direction = (end - start).normalize();
  190. Vector2D beforeStart = (start - Constants.FingerSliceOverlapFactor * direction).moveInBound(0, 0, maxX, maxY);
  191. Vector2D behindEnd = (end + Constants.FingerSliceOverlapFactor * direction).moveInBound(0, 0, maxY, maxY);
  192. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  193. if (slice.Length >= Constants.FingerMinSize && slice.Length <= Constants.FingerMaxSize && fingerSliceDepthTest(slice))
  194. return slice;
  195. return null;
  196. }
  197. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  198. {
  199. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  200. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  201. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  202. return (depthStart > depthMid && depthMid < depthEnd);
  203. }
  204. private void createFingerFromTrail(FingerSliceTrail trail)
  205. {
  206. //bring finger in correct direction Tip->Hand
  207. trail = orderTrailTipToHand(trail);
  208. //create finger
  209. Finger finger = new Finger(trail);
  210. //add finger
  211. if (!isCrippleFinger(finger))
  212. Fingers.Add(finger);
  213. else
  214. Console.WriteLine("cripple");
  215. //remove edges around detected finger to improve performance
  216. edgeImageAdapted.removeEdgesInsidePolygon(finger.Contour.ToArray());
  217. }
  218. private bool isCrippleFinger(Finger finger)
  219. {
  220. int maxX = depthImage.Width - 1;
  221. int maxY = depthImage.Height - 1;
  222. FingerSlice midSlice = finger.SliceTrail.MidSlice;
  223. Vector2D direction = midSlice.Direction;
  224. Vector2D out1 = (midSlice.Start - Constants.FingerCrippleOutFactor * direction).moveInBound(0, 0, maxX, maxY);
  225. Vector2D out2 = (midSlice.End + Constants.FingerCrippleOutFactor * direction).moveInBound(0, 0, maxX, maxY);
  226. Int16 depthAtFinger = depthImage.getDepthAt(midSlice.Mid);
  227. Int16 depthAtOut1 = depthImage.getDepthAt(out1);
  228. Int16 depthAtOut2 = depthImage.getDepthAt(out2);
  229. int minDepthDifference = Math.Min(Math.Abs(depthAtFinger - depthAtOut1), Math.Abs(depthAtFinger - depthAtOut2));
  230. return (minDepthDifference < Constants.FingerCrippleOutMinDifference);
  231. }
  232. private FingerSlice findOutSlice(Vector2D start, Vector2D direction)
  233. {
  234. int maxX = depthImage.Width - 1;
  235. int maxY = depthImage.Height - 1;
  236. Vector2D dirOrth1 = direction.getOrthogonal(true);
  237. Vector2D dirOrth2 = direction.getOrthogonal(false);
  238. Vector2D outPoint = (start + Constants.FingerOutSliceFactor * direction).moveInBound(0, 0, maxX, maxY);
  239. Vector2D p1 = findNextEdge(outPoint, dirOrth1, false, false, true);
  240. Vector2D p2 = findNextEdge(outPoint, dirOrth2, false, false, true);
  241. FingerSlice slice = new FingerSlice(p1, p2);
  242. return slice;
  243. }
  244. private FingerSliceTrail orderTrailTipToHand(FingerSliceTrail trail)
  245. {
  246. int maxX = depthImage.Width - 2;
  247. int maxY = depthImage.Height - 2;
  248. FingerSlice start = trail.StartSlice;
  249. FingerSlice end = trail.EndSlice;
  250. Vector2D direction = (end.Mid - start.Mid).normalize();
  251. FingerSlice startOutSlice = findOutSlice(start.Mid, direction.getInverse());
  252. FingerSlice endOutSlice = findOutSlice(end.Mid, direction);
  253. float startOutLength = float.MaxValue;
  254. float endOutLength = float.MaxValue;
  255. if (startOutSlice.Start.isWithin(1, 1, maxX, maxY) && startOutSlice.End.isWithin(1, 1, maxX, maxY))
  256. startOutLength = startOutSlice.Length;
  257. if (endOutSlice.Start.isWithin(1, 1, maxX, maxY) && endOutSlice.End.isWithin(1, 1, maxX, maxY))
  258. endOutLength = endOutSlice.Length;
  259. if (startOutLength <= endOutLength)
  260. {
  261. trail.reverse();
  262. outputImage.drawLineSegment(startOutSlice.LineSegment, Constants.FingerHandOutSliceColor);
  263. outputImage.drawLineSegment(endOutSlice.LineSegment, Constants.FingerTipOutSliceColor);
  264. }
  265. else
  266. {
  267. outputImage.drawLineSegment(startOutSlice.LineSegment, Constants.FingerTipOutSliceColor);
  268. outputImage.drawLineSegment(endOutSlice.LineSegment, Constants.FingerHandOutSliceColor);
  269. }
  270. return trail;
  271. }
  272. private void drawFingers()
  273. {
  274. foreach (Finger finger in Fingers)
  275. {
  276. drawFinger(finger);
  277. }
  278. }
  279. private void drawFinger(Finger finger)
  280. {
  281. FingerSliceTrail trail = finger.SliceTrail;
  282. for (int i = 0; i < trail.NumSlices; i++)
  283. {
  284. outputImage.drawLineSegment(trail[i].LineSegment, Constants.FingerSliceColor);
  285. }
  286. outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
  287. outputImage.drawContour(finger.Contour, Constants.FingerContourColor, 1);
  288. }
  289. }
  290. }