FingerDetector.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Fingers
  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 minNumSlices = 7;
  31. int width = depthImage.Width;
  32. int height = depthImage.Height;
  33. int maxX = width - 1;
  34. int maxY = height - 1;
  35. Fingers = new List<Finger>();
  36. for (int y = 1; y < maxY; y+=5) //y++ for 100% coverage, but y++ for 99% coverage and 3 times better perfomance
  37. {
  38. for (int x = 1; x < maxX; x++)
  39. {
  40. if (edgeImage.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 > minNumSlices)
  54. createFingerFromTrail(trail);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. private Vector2D getEdgeDirection(Vector2D edgePoint)
  62. {
  63. int x = edgePoint.IntX;
  64. int y = edgePoint.IntY;
  65. if (edgeImage.isEdgeAt(x, y - 1) && edgeImage.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  66. else if (edgeImage.isEdgeAt(x - 1, y) && edgeImage.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  67. else if (edgeImage.isEdgeAt(x - 1, y - 1) && edgeImage.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  68. else if (edgeImage.isEdgeAt(x + 1, y - 1) && edgeImage.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  69. else return null;
  70. }
  71. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  72. {
  73. int minNumSlicesForCorrection = 7;
  74. int numRemoveForCorrection = 3;
  75. int maxX = depthImage.Width - 1;
  76. int maxY = depthImage.Height - 1;
  77. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  78. Vector2D position = startSlice.Mid + startDirection;
  79. Vector2D direction = startDirection;
  80. if (position.isWithin(0, 0, maxX, maxY))
  81. {
  82. FingerSlice nextSlice = findFingerSliceFromMid(position, direction);
  83. if (nextSlice != null)
  84. {
  85. trail.addSlice(nextSlice);
  86. trail = expandTrail(trail);
  87. if (trail.NumSlices > minNumSlicesForCorrection)
  88. {
  89. trail.Slices.RemoveRange(0, numRemoveForCorrection);
  90. trail.Slices.Reverse();
  91. trail = expandTrail(trail);
  92. trail.Slices.Reverse();
  93. return trail;
  94. }
  95. }
  96. }
  97. return null;
  98. }
  99. private FingerSliceTrail expandTrail(FingerSliceTrail trail)
  100. {
  101. int maxX = depthImage.Width - 1;
  102. int maxY = depthImage.Height - 1;
  103. Vector2D currentDirection = trail.getEndDirection();
  104. Vector2D currentPosition = trail.End.Mid + 2*currentDirection;
  105. int gapCounter = 0;
  106. int numSlices = trail.NumSlices;
  107. FingerSlice lastSlice = trail.End;
  108. FingerSlice nextSlice;
  109. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices / 2, 10))
  110. {
  111. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  112. if (nextSlice != null && (nextSlice.Length < lastSlice.Length + 5 && nextSlice.Length > lastSlice.Length - 5))
  113. {
  114. gapCounter = 0;
  115. numSlices++;
  116. trail.addSlice(nextSlice);
  117. currentDirection = trail.getEndDirection();
  118. currentPosition = nextSlice.Mid + 2*currentDirection;
  119. lastSlice = nextSlice;
  120. }
  121. else
  122. {
  123. gapCounter++;
  124. currentPosition += currentDirection;
  125. }
  126. }
  127. return trail;
  128. }
  129. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  130. {
  131. if (edgeImage.isEdgeAt(position)) return null;
  132. Vector2D dirStart = direction.getOrthogonal(true);
  133. Vector2D dirEnd = direction.getOrthogonal(false);
  134. Vector2D start = findNextEdge(position, dirStart);
  135. if (start == null) return null;
  136. Vector2D end = findNextEdge(position, dirEnd);
  137. if (end == null) return null;
  138. return getFingerSlice(start, end);
  139. }
  140. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  141. {
  142. Vector2D end = findNextEdge(start, direction);
  143. if (end == null) return null;
  144. return getFingerSlice(start, end);
  145. }
  146. private Vector2D findNextEdge(Vector2D start, Vector2D direction)
  147. {
  148. int maxX = depthImage.Width - 1;
  149. int maxY = depthImage.Height - 1;
  150. int maxFingerSize = 30;
  151. int maxStepsX;
  152. if (direction.X > 0)
  153. maxStepsX = (int)((maxX - start.X) / direction.X);
  154. else if (direction.X < 0)
  155. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  156. else
  157. maxStepsX = int.MaxValue;
  158. int maxStepsY;
  159. if (direction.Y > 0)
  160. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  161. else if (direction.Y < 0)
  162. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  163. else
  164. maxStepsY = int.MaxValue;
  165. int maxStepsLength = (int)(maxFingerSize / direction.Length);
  166. int maxSteps = Math.Min(maxStepsLength, Math.Min(maxStepsX, maxStepsY));
  167. Vector2D end = new Vector2D(start);
  168. for (int i = 0; i < maxSteps; i++)
  169. {
  170. end += direction;
  171. if (edgeImage.isEdgeAt(end))
  172. {
  173. return end;
  174. }
  175. }
  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. int minFingerSize = 5;
  183. int maxFingerSize = 30;
  184. Vector2D direction = (end - start).normalize();
  185. Vector2D beforeStart = start - direction;
  186. Vector2D behindEnd = end + direction;
  187. start = beforeStart.isWithin(0, 0, maxX, maxY) ? beforeStart : start;
  188. end = behindEnd.isWithin(0, 0, maxX, maxY) ? behindEnd : end;
  189. FingerSlice slice = new FingerSlice(start, end);
  190. if (slice.Length >= minFingerSize && slice.Length <= maxFingerSize && 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. Finger finger = new Finger(trail);
  204. //add finger
  205. Fingers.Add(finger);
  206. //draw finger
  207. drawDetectedFinger(finger);
  208. //remove edges around detected finger to improve performance
  209. edgeImage.removeFingerEdges(finger);
  210. }
  211. private void drawDetectedFinger(Finger finger)
  212. {
  213. FingerSliceTrail trail = finger.SliceTrail;
  214. for (int i = 0; i < trail.NumSlices; i++)
  215. {
  216. outputImage.drawLineSegment(trail.Slices[i].LineSegment, Constants.FingerSliceColor);
  217. }
  218. outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
  219. }
  220. }
  221. }