PalmDetector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using bbiwarg.Utility;
  10. using bbiwarg.Images;
  11. using bbiwarg.Recognition.FingerRecognition;
  12. using bbiwarg.Recognition.HandRecognition;
  13. using bbiwarg.Graphics;
  14. namespace bbiwarg.Recognition.PalmRecognition
  15. {
  16. class PalmDetector
  17. {
  18. private OutputImage outputImage;
  19. private Hand palmHand, pointingHand;
  20. private Contour<Point> palmContour;
  21. private List<MCvConvexityDefect> convexityDefects;
  22. private Vector2D thumbDefectStart;
  23. private Vector2D thumbDefectEnd;
  24. public Vector2D thumbDefectDepth;
  25. private Kalman2DPositionFilter thumbDefectDepthFilter, thumbDefectStartFilter, thumbDefectEndFilter;
  26. private int numFramesNoHandFound;
  27. public Quadrangle PalmQuad { get; private set; }
  28. public PalmDetector()
  29. {
  30. thumbDefectDepthFilter = new Kalman2DPositionFilter(Constants.PalmThumbDefectmXX, Constants.PalmThumbDefectmXY,
  31. Constants.PalmThumbDefectmYY, Constants.PalmThumbDefectProcessNoise);
  32. thumbDefectStartFilter = new Kalman2DPositionFilter(Constants.PalmThumbDefectmXX, Constants.PalmThumbDefectmXY,
  33. Constants.PalmThumbDefectmYY, Constants.PalmThumbDefectProcessNoise);
  34. thumbDefectEndFilter = new Kalman2DPositionFilter(Constants.PalmThumbDefectmXX, Constants.PalmThumbDefectmXY,
  35. Constants.PalmThumbDefectmYY, Constants.PalmThumbDefectProcessNoise);
  36. }
  37. public void findPalmQuad(OutputImage outputImage, List<Hand> hands)
  38. {
  39. this.outputImage = outputImage;
  40. if (hands.Count == 1 && hands[0].Fingers.Count == 1)
  41. {
  42. palmHand = hands[0];
  43. pointingHand = null;
  44. }
  45. else if (hands.Count == 2)
  46. {
  47. if (hands[0].Fingers.Count > 1)
  48. {
  49. pointingHand = hands[0];
  50. palmHand = hands[1];
  51. }
  52. else if (hands[1].Fingers.Count > 1)
  53. {
  54. pointingHand = hands[1];
  55. palmHand = hands[0];
  56. }
  57. else if (hands[0].Fingers[0].LineSegment.Length > hands[1].Fingers[0].LineSegment.Length)
  58. {
  59. pointingHand = hands[0];
  60. palmHand = hands[1];
  61. }
  62. else
  63. {
  64. pointingHand = hands[1];
  65. palmHand = hands[0];
  66. }
  67. }
  68. if ((hands.Count == 1 && hands[0].Fingers.Count == 1) || hands.Count == 2)
  69. {
  70. findLongestPalmContour();
  71. findConvexityDefectsSortedByDepth();
  72. if (pointingHand != null)
  73. removeConvexityDefectsCausedByFingers();
  74. findHandPoints(hands.Count);
  75. }
  76. if (hands.Count == 0)
  77. {
  78. ++numFramesNoHandFound;
  79. if (numFramesNoHandFound == Constants.PalmNumFramesNoHandReset)
  80. reset();
  81. }
  82. draw();
  83. }
  84. public void reset()
  85. {
  86. thumbDefectDepthFilter.reset();
  87. thumbDefectStartFilter.reset();
  88. thumbDefectEndFilter.reset();
  89. palmContour = null;
  90. PalmQuad = null;
  91. numFramesNoHandFound = 0;
  92. }
  93. private void findLongestPalmContour()
  94. {
  95. Contour<Point> contour = palmHand.Mask.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
  96. Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  97. palmContour = contour;
  98. double maxPerimeter = 0;
  99. while (contour != null)
  100. {
  101. if (contour.Perimeter > maxPerimeter)
  102. {
  103. maxPerimeter = contour.Perimeter;
  104. palmContour = contour;
  105. }
  106. contour = contour.HNext;
  107. }
  108. }
  109. private void findConvexityDefectsSortedByDepth()
  110. {
  111. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  112. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  113. {
  114. if (d1.Depth < d2.Depth)
  115. return 1;
  116. else if (d1.Depth > d2.Depth)
  117. return -1;
  118. return 0;
  119. });
  120. }
  121. private void removeConvexityDefectsCausedByFingers()
  122. {
  123. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  124. foreach (MCvConvexityDefect d in convexityDefects)
  125. {
  126. bool fingerIntersectsStartEnd = false;
  127. float minFingerLineDist = float.MaxValue;
  128. foreach (Finger f in pointingHand.Fingers)
  129. {
  130. Utility.LineSegment2D defectLine = new Utility.LineSegment2D(new Vector2D(d.StartPoint), new Vector2D(d.EndPoint));
  131. Vector2D intersection = defectLine.Line.getIntersection(f.LineSegment.Line);
  132. if (intersection != null &&
  133. intersection.isInBox(defectLine.P1, defectLine.P2) && intersection.isInBox(f.LineSegment.P1, f.LineSegment.P2))
  134. {
  135. fingerIntersectsStartEnd = true;
  136. break;
  137. }
  138. Vector2D mid = (new Vector2D(d.StartPoint) + new Vector2D(d.EndPoint)) / 2.0f;
  139. float dist = f.LineSegment.getDistanceTo(mid);
  140. if (dist < minFingerLineDist)
  141. minFingerLineDist = dist;
  142. }
  143. if (minFingerLineDist >= Constants.PalmMinDefectMidFingerLineDistance && !fingerIntersectsStartEnd)
  144. newDefects.Add(d);
  145. }
  146. convexityDefects = newDefects;
  147. }
  148. private MCvConvexityDefect? findThumbDefect()
  149. {
  150. foreach (MCvConvexityDefect d in convexityDefects)
  151. {
  152. Vector2D depth = new Vector2D(d.DepthPoint);
  153. Vector2D start = new Vector2D(d.StartPoint);
  154. Vector2D end = new Vector2D(d.EndPoint);
  155. float angle = (float)((depth - start).getAngleBetween(depth - end) * 180 / Math.PI);
  156. float l1 = (depth - start).Length;
  157. float l2 = (depth - end).Length;
  158. float startEndLengthQuotient = Math.Max(l1, l2) / Math.Min(l1, l2);
  159. float depthThumbLengthQuotient = d.Depth / palmHand.Fingers[0].LineSegment.Length;
  160. if (angle <= Constants.PalmMaxThumbDefectAngle &&
  161. startEndLengthQuotient >= Constants.PalmMinThumbDefectStartEndLengthQuotient &&
  162. startEndLengthQuotient <= Constants.PalmMaxThumbDefectStartEndLengthQuotient &&
  163. depthThumbLengthQuotient >= Constants.PalmMinTumbDefectDepthThumbLengthQuotient &&
  164. depthThumbLengthQuotient <= Constants.PalmMaxTumbDefectDepthThumbLengthQuotient)
  165. {
  166. return d;
  167. }
  168. }
  169. return null;
  170. }
  171. private void findHandPoints(int numHands)
  172. {
  173. MCvConvexityDefect? thumbDefect = findThumbDefect();
  174. if (thumbDefect != null)
  175. {
  176. thumbDefectDepth = new Vector2D(thumbDefect.Value.DepthPoint);
  177. thumbDefectStart = new Vector2D(thumbDefect.Value.StartPoint);
  178. thumbDefectEnd = new Vector2D(thumbDefect.Value.EndPoint);
  179. if (!thumbDefectDepthFilter.Initialized)
  180. {
  181. thumbDefectDepthFilter.setInitialPosition(thumbDefectDepth);
  182. thumbDefectStartFilter.setInitialPosition(thumbDefectStart);
  183. thumbDefectEndFilter.setInitialPosition(thumbDefectEnd);
  184. }
  185. else
  186. {
  187. thumbDefectDepth = thumbDefectDepthFilter.getCorrectedPosition(thumbDefectDepth);
  188. thumbDefectStart = thumbDefectStartFilter.getCorrectedPosition(thumbDefectStart);
  189. thumbDefectEnd = thumbDefectEndFilter.getCorrectedPosition(thumbDefectEnd);
  190. }
  191. Vector2D handLength, handWidth, longestLineEndpoint, topLeft, bottomLeft, bottomRight, topRight;
  192. Vector2D startDepth = thumbDefectStart - thumbDefectDepth;
  193. Vector2D endDepth = thumbDefectEnd - thumbDefectDepth;
  194. if (startDepth.Length > endDepth.Length)
  195. {
  196. handLength = startDepth;
  197. longestLineEndpoint = thumbDefectStart;
  198. }
  199. else
  200. {
  201. handLength = endDepth;
  202. longestLineEndpoint = thumbDefectEnd;
  203. }
  204. if (palmHand.Side == Hand.HandSide.Left)
  205. handWidth = 0.85f * new Vector2D(-handLength.Y, handLength.X);
  206. else
  207. handWidth = 0.85f * new Vector2D(handLength.Y, -handLength.X);
  208. topLeft = longestLineEndpoint + 0.15f * handLength;
  209. bottomLeft = thumbDefectDepth - 0.4f * handLength;
  210. bottomRight = bottomLeft + handWidth;
  211. topRight = bottomRight + 1.2f * handLength - 0.3f * handWidth;
  212. PalmQuad = new Quadrangle(bottomLeft, topLeft, topRight, bottomRight);
  213. }
  214. }
  215. private void draw()
  216. {
  217. if (palmContour != null && palmContour.Count<Point>() > 0) {
  218. outputImage.drawContour(palmContour, Constants.PalmConturColor);
  219. outputImage.drawPoints(palmContour.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE), Constants.PalmConvexHullColor);
  220. }
  221. if (PalmQuad != null)
  222. {
  223. outputImage.fillCircle(thumbDefectStart.IntX, thumbDefectStart.IntY, 3, Color.Red);
  224. outputImage.fillCircle(thumbDefectEnd.IntX, thumbDefectEnd.IntY, 3, Color.Red);
  225. outputImage.fillCircle(thumbDefectDepth.IntX, thumbDefectDepth.IntY, 3, Color.Red);
  226. outputImage.drawLineSegment(new Utility.LineSegment2D(thumbDefectDepth, (thumbDefectStart + thumbDefectEnd) / 2.0f), Constants.PalmThumbDefectColor, 1);
  227. Vector2D[] vertices = PalmQuad.Vertices;
  228. for (int i = 0; i < 4; ++i)
  229. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(vertices[i], vertices[(i + 1) % 4]), Constants.PalmQuadColor);
  230. drawGrid(new Vector2D(vertices[0]), new Vector2D(vertices[1]), new Vector2D(vertices[2]), new Vector2D(vertices[3]));
  231. }
  232. }
  233. private void drawGrid(Vector2D a, Vector2D b, Vector2D c, Vector2D d)
  234. {
  235. Vector2D relAB = (b - a) / Constants.PalmGridRows;
  236. Vector2D relDC = (c - d) / Constants.PalmGridRows;
  237. Vector2D relBC = (c - b) / Constants.PalmGridColumns;
  238. Vector2D relAD = (d - a) / Constants.PalmGridColumns;
  239. for (int i = 1; i < Constants.PalmGridRows; i++)
  240. {
  241. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAB, d + i * relDC), Constants.PalmGridColor);
  242. }
  243. for (int i = 1; i < Constants.PalmGridColumns; i++)
  244. {
  245. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAD, b + i * relBC), Constants.PalmGridColor);
  246. }
  247. }
  248. }
  249. }