FingerDetector.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 = 20;
  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++)
  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 = 15;
  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.Reverse();
  90. trail.Slices.RemoveRange(0, numRemoveForCorrection);
  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. int numDirectionsForAverage = 10;
  104. List<Vector2D> directions = new List<Vector2D>();
  105. int numDirections = Math.Min(trail.NumSlices - 1, numDirectionsForAverage);
  106. for (int i = 0; i < numDirections; i++)
  107. {
  108. directions.Add(trail.Slices[i + 1].Mid - trail.Slices[i].Mid);
  109. }
  110. Vector2D currentDirection = Vector2D.mean(directions).normalize();
  111. Vector2D currentPosition = trail.End.Mid + currentDirection;
  112. int gapCounter = 0;
  113. int numSlices = trail.NumSlices;
  114. FingerSlice lastSlice = trail.End;
  115. FingerSlice nextSlice;
  116. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices / 2, 10))
  117. {
  118. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  119. if (nextSlice != null && (nextSlice.Length < lastSlice.Length + 5 && nextSlice.Length > lastSlice.Length - 5))
  120. {
  121. gapCounter = 0;
  122. numSlices++;
  123. trail.addSlice(nextSlice);
  124. directions.Add((nextSlice.Mid - lastSlice.Mid));
  125. if (directions.Count == numDirectionsForAverage)
  126. directions.RemoveAt(0);
  127. currentDirection = Vector2D.mean(directions).normalize();
  128. currentPosition = nextSlice.Mid + currentDirection;
  129. lastSlice = nextSlice;
  130. }
  131. else
  132. {
  133. gapCounter++;
  134. currentPosition += currentDirection;
  135. }
  136. }
  137. return trail;
  138. }
  139. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  140. {
  141. if (edgeImage.isEdgeAt(position)) return null;
  142. Vector2D dirStart = direction.getOrthogonal(true);
  143. Vector2D dirEnd = direction.getOrthogonal(false);
  144. Vector2D start = findNextEdge(position, dirStart);
  145. if (start == null) return null;
  146. Vector2D end = findNextEdge(position, dirEnd);
  147. if (end == null) return null;
  148. return getFingerSlice(start, end);
  149. }
  150. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  151. {
  152. Vector2D end = findNextEdge(start, direction);
  153. if (end == null) return null;
  154. return getFingerSlice(start, end);
  155. }
  156. private Vector2D findNextEdge(Vector2D start, Vector2D direction)
  157. {
  158. int maxX = depthImage.Width - 1;
  159. int maxY = depthImage.Height - 1;
  160. int maxFingerSize = 30;
  161. int maxStepsX;
  162. if (direction.X > 0)
  163. maxStepsX = (int)((maxX - start.X) / direction.X);
  164. else if (direction.X < 0)
  165. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  166. else
  167. maxStepsX = int.MaxValue;
  168. int maxStepsY;
  169. if (direction.Y > 0)
  170. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  171. else if (direction.Y < 0)
  172. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  173. else
  174. maxStepsY = int.MaxValue;
  175. int maxStepsLength = (int)(maxFingerSize / direction.Length);
  176. int maxSteps = Math.Min(maxStepsLength, Math.Min(maxStepsX, maxStepsY));
  177. Vector2D end = new Vector2D(start);
  178. for (int i = 0; i < maxSteps; i++)
  179. {
  180. end += direction;
  181. if (edgeImage.isEdgeAt(end))
  182. {
  183. return end;
  184. }
  185. }
  186. return null;
  187. }
  188. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  189. {
  190. int maxX = depthImage.Width - 1;
  191. int maxY = depthImage.Height - 1;
  192. int minFingerSize = 5;
  193. int maxFingerSize = 30;
  194. Vector2D direction = (end - start).normalize();
  195. Vector2D beforeStart = start - direction;
  196. Vector2D behindEnd = end + direction;
  197. start = beforeStart.isWithin(0, 0, maxX, maxY) ? beforeStart : start;
  198. end = behindEnd.isWithin(0, 0, maxX, maxY) ? behindEnd : end;
  199. FingerSlice slice = new FingerSlice(start, end);
  200. if (slice.Length >= minFingerSize && slice.Length <= maxFingerSize && fingerSliceDepthTest(slice))
  201. return slice;
  202. return null;
  203. }
  204. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  205. {
  206. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  207. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  208. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  209. return (depthStart > depthMid && depthMid < depthEnd);
  210. }
  211. private void createFingerFromTrail(FingerSliceTrail trail)
  212. {
  213. Finger finger = new Finger(trail);
  214. //add finger
  215. Fingers.Add(finger);
  216. //draw finger
  217. drawDetectedFinger(finger);
  218. //remove edges around detected finger to improve performance
  219. edgeImage.removeFingerEdges(finger);
  220. }
  221. private void drawDetectedFinger(Finger finger)
  222. {
  223. FingerSliceTrail trail = finger.SliceTrail;
  224. for (int i = 0; i < trail.NumSlices; i++)
  225. {
  226. outputImage.drawLineSegment(trail.Slices[i].LineSegment, 255, 0, 255);
  227. }
  228. outputImage.drawLineSegment(finger.LineSegment, 255, 0, 0);
  229. }
  230. }
  231. }