PalmDetector.cs 11 KB

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