FingerDetector.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. namespace bbiwarg.Detectors.Fingers
  13. {
  14. class FingerDetector
  15. {
  16. private DepthImage depthImage;
  17. private EdgeImage edgeImage;
  18. private FingerImage fingerImage;
  19. public List<Finger> Fingers { get; private set; }
  20. public FingerDetector(DepthImage depthImage, EdgeImage edgeImage, FingerImage fingerImage)
  21. {
  22. this.depthImage = depthImage;
  23. this.edgeImage = edgeImage.copy();
  24. this.fingerImage = fingerImage;
  25. findFingers();
  26. }
  27. private void findFingers()
  28. {
  29. int minNumSlices = 20;
  30. int width = depthImage.Width;
  31. int height = depthImage.Height;
  32. int maxX = width - 1;
  33. int maxY = height - 1;
  34. Fingers = new List<Finger>();
  35. for (int y = 1; y < maxY; y++)
  36. {
  37. for (int x = 1; x < maxX; x++)
  38. {
  39. if (edgeImage.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 > minNumSlices)
  53. createFingerFromTrail(trail);
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. private Vector2D getEdgeDirection(Vector2D edgePoint)
  61. {
  62. int x = edgePoint.IntX;
  63. int y = edgePoint.IntY;
  64. if (edgeImage.isEdgeAt(x, y - 1) && edgeImage.isEdgeAt(x, y + 1)) return new Vector2D(0, 1);
  65. else if (edgeImage.isEdgeAt(x - 1, y) && edgeImage.isEdgeAt(x + 1, y)) return new Vector2D(1, 0);
  66. else if (edgeImage.isEdgeAt(x - 1, y - 1) && edgeImage.isEdgeAt(x + 1, y + 1)) return new Vector2D(1, 1).normalize();
  67. else if (edgeImage.isEdgeAt(x + 1, y - 1) && edgeImage.isEdgeAt(x - 1, y + 1)) return new Vector2D(1, -1).normalize();
  68. else return null;
  69. }
  70. private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
  71. {
  72. int minNumSlicesForCorrection = 15;
  73. int numRemoveForCorrection = 3;
  74. int maxX = depthImage.Width - 1;
  75. int maxY = depthImage.Height - 1;
  76. FingerSliceTrail trail = new FingerSliceTrail(startSlice);
  77. Vector2D position = startSlice.Mid + startDirection;
  78. Vector2D direction = startDirection;
  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 > minNumSlicesForCorrection)
  87. {
  88. trail.Slices.Reverse();
  89. trail.Slices.RemoveRange(0, numRemoveForCorrection);
  90. trail = expandTrail(trail);
  91. trail.Slices.Reverse();
  92. return trail;
  93. }
  94. }
  95. }
  96. return null;
  97. }
  98. private FingerSliceTrail expandTrail(FingerSliceTrail trail)
  99. {
  100. int maxX = depthImage.Width - 1;
  101. int maxY = depthImage.Height - 1;
  102. int numDirectionsForAverage = 10;
  103. List<Vector2D> directions = new List<Vector2D>();
  104. int numDirections = Math.Min(trail.NumSlices - 1, numDirectionsForAverage);
  105. for (int i = 0; i < numDirections; i++)
  106. {
  107. directions.Add(trail.Slices[i + 1].Mid - trail.Slices[i].Mid);
  108. }
  109. Vector2D currentDirection = Vector2D.mean(directions).normalize();
  110. Vector2D currentPosition = trail.End.Mid + currentDirection;
  111. int gapCounter = 0;
  112. int numSlices = trail.NumSlices;
  113. FingerSlice lastSlice = trail.End;
  114. FingerSlice nextSlice;
  115. while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices / 2, 10))
  116. {
  117. nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
  118. if (nextSlice != null && (nextSlice.Length < lastSlice.Length + 5 && nextSlice.Length > lastSlice.Length - 5))
  119. {
  120. gapCounter = 0;
  121. numSlices++;
  122. trail.addSlice(nextSlice);
  123. directions.Add((nextSlice.Mid - lastSlice.Mid));
  124. if (directions.Count == numDirectionsForAverage)
  125. directions.RemoveAt(0);
  126. currentDirection = Vector2D.mean(directions).normalize();
  127. currentPosition = nextSlice.Mid + currentDirection;
  128. lastSlice = nextSlice;
  129. }
  130. else
  131. {
  132. gapCounter++;
  133. currentPosition += currentDirection;
  134. }
  135. }
  136. return trail;
  137. }
  138. private FingerSlice findFingerSliceFromMid(Vector2D position, Vector2D direction)
  139. {
  140. if (edgeImage.isEdgeAt(position)) return null;
  141. Vector2D dirStart = direction.getOrthogonal(true);
  142. Vector2D dirEnd = direction.getOrthogonal(false);
  143. Vector2D start = findNextEdge(position, dirStart);
  144. if (start == null) return null;
  145. Vector2D end = findNextEdge(position, dirEnd);
  146. if (end == null) return null;
  147. return getFingerSlice(start, end);
  148. }
  149. private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
  150. {
  151. Vector2D end = findNextEdge(start, direction);
  152. if (end == null) return null;
  153. return getFingerSlice(start, end);
  154. }
  155. private Vector2D findNextEdge(Vector2D start, Vector2D direction)
  156. {
  157. int maxX = depthImage.Width - 1;
  158. int maxY = depthImage.Height - 1;
  159. int maxFingerSize = 30;
  160. int maxStepsX;
  161. if (direction.X > 0)
  162. maxStepsX = (int)((maxX - start.X) / direction.X);
  163. else if (direction.X < 0)
  164. maxStepsX = (int)(start.X / Math.Abs(direction.X));
  165. else
  166. maxStepsX = int.MaxValue;
  167. int maxStepsY;
  168. if (direction.Y > 0)
  169. maxStepsY = (int)((maxY - start.Y) / direction.Y);
  170. else if (direction.Y < 0)
  171. maxStepsY = (int)(start.Y / Math.Abs(direction.Y));
  172. else
  173. maxStepsY = int.MaxValue;
  174. int maxStepsLength = (int)(maxFingerSize / direction.Length);
  175. int maxSteps = Math.Min(maxStepsLength, Math.Min(maxStepsX, maxStepsY));
  176. Vector2D end = new Vector2D(start);
  177. for (int i = 0; i < maxSteps; i++)
  178. {
  179. end += direction;
  180. if (edgeImage.isEdgeAt(end))
  181. {
  182. return end;
  183. }
  184. }
  185. return null;
  186. }
  187. private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
  188. {
  189. int maxX = depthImage.Width - 1;
  190. int maxY = depthImage.Height - 1;
  191. int minFingerSize = 5;
  192. int maxFingerSize = 30;
  193. Vector2D direction = (end - start).normalize();
  194. Vector2D beforeStart = start - direction;
  195. Vector2D behindEnd = end + direction;
  196. start = beforeStart.isWithin(0, 0, maxX, maxY) ? beforeStart : start;
  197. end = behindEnd.isWithin(0, 0, maxX, maxY) ? behindEnd : end;
  198. FingerSlice slice = new FingerSlice(start, end);
  199. if (slice.Length >= minFingerSize && slice.Length <= maxFingerSize && fingerSliceDepthTest(slice))
  200. return slice;
  201. return null;
  202. }
  203. private bool fingerSliceDepthTest(FingerSlice fingerSlice)
  204. {
  205. Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start);
  206. Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid);
  207. Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End);
  208. return (depthStart > depthMid && depthMid < depthEnd);
  209. }
  210. private void createFingerFromTrail(FingerSliceTrail trail)
  211. {
  212. Finger finger = new Finger(trail);
  213. //add finger
  214. Fingers.Add(finger);
  215. //draw finger
  216. fingerImage.drawFinger(finger, FingerImageState.fingerDetected);
  217. //remove edges around detected finger to improve performance
  218. edgeImage.removeFingerEdges(finger);
  219. }
  220. }
  221. }