PalmDetector.cs 12 KB

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