PalmDetector.cs 14 KB

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