FingerDetector.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 edgeImage;
  19. private OutputImage outputImage;
  20. public List<Finger> Fingers { get; private set; }
  21. public FingerDetector(DepthImage depthImage, EdgeImage edgeImage, OutputImage outputImage)
  22. {
  23. this.depthImage = depthImage;
  24. this.edgeImage = edgeImage.copy();
  25. this.outputImage = outputImage;
  26. findFingers();
  27. }
  28. private void findFingers()
  29. {
  30. int maxX = depthImage.Width - 1;
  31. int maxY = depthImage.Height - 1;
  32. Fingers = new List<Finger>();
  33. for (int y = 1; y < maxY; y+=5) //y++ for 100% coverage, but y+=5 for 99% coverage and 3 times better perfomance
  34. {
  35. for (int x = 1; x < maxX; x++)
  36. {
  37. if (edgeImage.isEdgeAt(x, y))
  38. {
  39. Vector2D edgePoint = new Vector2D(x, y);
  40. Vector2D edgeDirection = getEdgeDirection(edgePoint);
  41. if (edgeDirection != null)
  42. {
  43. Vector2D dir = edgeDirection.getOrthogonal(true);
  44. if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
  45. dir = dir.getInverse();
  46. FingerSlice slice = findFingerSliceFromStartEdge(edgePoint, dir);
  47. if (slice != null)
  48. {
  49. FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
  50. if (trail != null && trail.NumSlices > Constants.FingerMinNumSlices)
  51. createFingerFromTrail(trail);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. private Vector2D getEdgeDirection(Vector2D edgePoint)
  59. {
  60. int x = edgePoint.IntX;
  61. int y = edgePoint.IntY;
  62. if (edgeImage.isEdgeAt(x, y - 1) && edgeImage.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  63. else if (edgeImage.isEdgeAt(x - 1, y) && edgeImage.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  64. else if (edgeImage.isEdgeAt(x - 1, y - 1) && edgeImage.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  65. else if (edgeImage.isEdgeAt(x + 1, y - 1) && edgeImage.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  66. else return null;
  67. }
  68. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  69. {
  70. int maxX = depthImage.Width - 1;
  71. int maxY = depthImage.Height - 1;
  72. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  73. Vector2D direction = startDirection;
  74. Vector2D position = startSlice.Mid + Constants.FingerStepSize * direction;
  75. if (position.isWithin(0, 0, maxX, maxY))
  76. {
  77. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  78. if (nextSlice != null)
  79. {
  80. trail.addSlice(nextSlice);
  81. trail = expandTrail(trail);
  82. if (trail.NumSlices > Constants.FingerMinNumSlices)
  83. {
  84. trail.Slices.RemoveRange(0, Constants.FingerRemoveNumSlicesForCorrection);
  85. trail.Slices.Reverse();
  86. trail = expandTrail(trail);
  87. trail.Slices.Reverse();
  88. return trail;
  89. }
  90. }
  91. }
  92. return null;
  93. }
  94. private FingerSliceTrail expandTrail(FingerSliceTrail trail)
  95. {
  96. int maxX = depthImage.Width - 1;
  97. int maxY = depthImage.Height - 1;
  98. Vector2D currentDirection = trail.getEndDirection();
  99. Vector2D currentPosition = trail.End.Mid + Constants.FingerStepSize * currentDirection;
  100. int gapCounter = 0;
  101. int numSlices = trail.NumSlices;
  102. FingerSlice lastSlice = trail.End;
  103. FingerSlice nextSlice;
  104. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
  105. {
  106. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  107. if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
  108. {
  109. gapCounter = 0;
  110. numSlices++;
  111. trail.addSlice(nextSlice);
  112. currentDirection = trail.getEndDirection();
  113. currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
  114. lastSlice = nextSlice;
  115. }
  116. else
  117. {
  118. gapCounter++;
  119. currentPosition += currentDirection;
  120. }
  121. }
  122. return trail;
  123. }
  124. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  125. {
  126. if (edgeImage.isEdgeAt(position)) return null;
  127. Vector2D dirStart = direction.getOrthogonal(true);
  128. Vector2D dirEnd = direction.getOrthogonal(false);
  129. Vector2D start = findNextEdge(position, dirStart);
  130. if (start == null) return null;
  131. Vector2D end = findNextEdge(position, dirEnd);
  132. if (end == null) return null;
  133. return getFingerSlice(start, end);
  134. }
  135. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  136. {
  137. Vector2D end = findNextEdge(start, direction);
  138. if (end == null) return null;
  139. return getFingerSlice(start, end);
  140. }
  141. private Vector2D findNextEdge(Vector2D start, Vector2D direction)
  142. {
  143. int maxX = depthImage.Width - 1;
  144. int maxY = depthImage.Height - 1;
  145. int maxStepsX;
  146. if (direction.X > 0)
  147. maxStepsX = (int)((maxX - start.X) / direction.X);
  148. else if (direction.X < 0)
  149. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  150. else
  151. maxStepsX = int.MaxValue;
  152. int maxStepsY;
  153. if (direction.Y > 0)
  154. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  155. else if (direction.Y < 0)
  156. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  157. else
  158. maxStepsY = int.MaxValue;
  159. int maxStepsLength = (int)(Constants.FingerMaxSize / direction.Length);
  160. int maxSteps = Math.Min(maxStepsLength, Math.Min(maxStepsX, maxStepsY));
  161. Vector2D end = new Vector2D(start);
  162. for (int i = 0; i < maxSteps; i++)
  163. {
  164. end += direction;
  165. if (edgeImage.isEdgeAt(end))
  166. {
  167. return end;
  168. }
  169. }
  170. return null;
  171. }
  172. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  173. {
  174. int maxX = depthImage.Width - 1;
  175. int maxY = depthImage.Height - 1;
  176. Vector2D direction = (end - start).normalize();
  177. Vector2D beforeStart = (start - direction).moveInBound(0, 0, maxX, maxY);
  178. Vector2D behindEnd = (end + direction).moveInBound(0, 0, maxY, maxY);
  179. FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
  180. if (slice.Length >= Constants.FingerMinSize && slice.Length <= Constants.FingerMaxSize && fingerSliceDepthTest(slice))
  181. return slice;
  182. return null;
  183. }
  184. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  185. {
  186. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  187. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  188. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  189. return (depthStart > depthMid && depthMid < depthEnd);
  190. }
  191. private void createFingerFromTrail(FingerSliceTrail trail)
  192. {
  193. Finger finger = new Finger(trail);
  194. //add finger
  195. Fingers.Add(finger);
  196. //draw finger
  197. drawDetectedFinger(finger);
  198. //remove edges around detected finger to improve performance
  199. edgeImage.removeFingerEdges(finger);
  200. }
  201. private void drawDetectedFinger(Finger finger)
  202. {
  203. FingerSliceTrail trail = finger.SliceTrail;
  204. for (int i = 0; i < trail.NumSlices; i++)
  205. {
  206. outputImage.drawLineSegment(trail.Slices[i].LineSegment, Constants.FingerSliceColor);
  207. }
  208. outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
  209. }
  210. }
  211. }