FingerDetector.cs 9.5 KB

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