PalmDetector.cs 12 KB

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