PalmDetector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. public Contour<Point> PalmContour { get; private set; }
  19. public Vector2D ThumbDefectStart { get; private set; }
  20. public Vector2D ThumbDefectEnd { get; private set; }
  21. public Vector2D ThumbDefectDepth { get; private set; }
  22. private Hand palmHand, pointingHand;
  23. private List<MCvConvexityDefect> convexityDefects;
  24. private Kalman2DPositionFilter thumbDefectDepthFilter, thumbDefectStartFilter, thumbDefectEndFilter;
  25. private int numFramesNoHandFound;
  26. private Quadrangle lastPalmQuad;
  27. private List<Hand> hands;
  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(List<Hand> hands, Image<Gray, Byte> foregroundMask)
  40. {
  41. this.hands = hands;
  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(foregroundMask);
  77. }
  78. if (hands.Count == 0)
  79. {
  80. ++numFramesNoHandFound;
  81. if (numFramesNoHandFound == Constants.PalmNumFramesNoHandReset)
  82. reset();
  83. }
  84. else
  85. {
  86. numFramesNoHandFound = 0;
  87. }
  88. }
  89. public void reset()
  90. {
  91. thumbDefectDepthFilter.reset();
  92. thumbDefectStartFilter.reset();
  93. thumbDefectEndFilter.reset();
  94. PalmContour = null;
  95. PalmQuad = null;
  96. PalmHandSide = Hand.HandSide.Left;
  97. numFramesNoHandFound = 0;
  98. lastPalmQuad = null;
  99. }
  100. private void findLongestPalmContour()
  101. {
  102. Contour<Point> contour = palmHand.Mask.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
  103. Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  104. PalmContour = contour;
  105. double maxPerimeter = 0;
  106. while (contour != null)
  107. {
  108. if (contour.Perimeter > maxPerimeter)
  109. {
  110. maxPerimeter = contour.Perimeter;
  111. PalmContour = contour;
  112. }
  113. contour = contour.HNext;
  114. }
  115. }
  116. private void findConvexityDefectsSortedByDepth()
  117. {
  118. convexityDefects = new List<MCvConvexityDefect>(PalmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  119. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  120. {
  121. if (d1.Depth < d2.Depth)
  122. return 1;
  123. else if (d1.Depth > d2.Depth)
  124. return -1;
  125. return 0;
  126. });
  127. }
  128. private void removeConvexityDefectsCausedByFingers()
  129. {
  130. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  131. foreach (MCvConvexityDefect d in convexityDefects)
  132. {
  133. bool fingerIntersectsStartEnd = false;
  134. float minFingerLineDist = float.MaxValue;
  135. foreach (Finger f in pointingHand.Fingers)
  136. {
  137. Utility.LineSegment2D defectLine = new Utility.LineSegment2D(new Vector2D(d.StartPoint), new Vector2D(d.EndPoint));
  138. Vector2D intersection = defectLine.Line.getIntersection(f.LineSegment.Line);
  139. if (intersection != null &&
  140. intersection.isInBox(defectLine.P1, defectLine.P2) && intersection.isInBox(f.LineSegment.P1, f.LineSegment.P2))
  141. {
  142. fingerIntersectsStartEnd = true;
  143. break;
  144. }
  145. Vector2D mid = (new Vector2D(d.StartPoint) + new Vector2D(d.EndPoint)) / 2.0f;
  146. float dist = f.LineSegment.getDistanceTo(mid);
  147. if (dist < minFingerLineDist)
  148. minFingerLineDist = dist;
  149. }
  150. if (minFingerLineDist >= Constants.PalmMinDefectMidFingerLineDistance && !fingerIntersectsStartEnd)
  151. newDefects.Add(d);
  152. }
  153. convexityDefects = newDefects;
  154. }
  155. private MCvConvexityDefect? findThumbDefect()
  156. {
  157. foreach (MCvConvexityDefect d in convexityDefects)
  158. {
  159. Vector2D depth = new Vector2D(d.DepthPoint);
  160. Vector2D start = new Vector2D(d.StartPoint);
  161. Vector2D end = new Vector2D(d.EndPoint);
  162. float angle = (float)((depth - start).getAngleBetween(depth - end) * 180 / Math.PI);
  163. float l1 = (depth - start).Length;
  164. float l2 = (depth - end).Length;
  165. float startEndLengthQuotient = Math.Max(l1, l2) / Math.Min(l1, l2);
  166. float depthThumbLengthQuotient = d.Depth / palmHand.Fingers[0].LineSegment.Length;
  167. if (angle <= Constants.PalmMaxThumbDefectAngle &&
  168. startEndLengthQuotient >= Constants.PalmMinThumbDefectStartEndLengthQuotient &&
  169. startEndLengthQuotient <= Constants.PalmMaxThumbDefectStartEndLengthQuotient &&
  170. depthThumbLengthQuotient >= Constants.PalmMinTumbDefectDepthThumbLengthQuotient &&
  171. depthThumbLengthQuotient <= Constants.PalmMaxTumbDefectDepthThumbLengthQuotient)
  172. {
  173. return d;
  174. }
  175. }
  176. return null;
  177. }
  178. float getForegroundPixelPercentage(Quadrangle quad, Image<Gray, Byte> foregroundMask)
  179. {
  180. float numPixelsInMask = quad.Mask.CountNonzero()[0];
  181. float numPixelsInMaskInForeground = (quad.Mask & foregroundMask).CountNonzero()[0];
  182. return numPixelsInMaskInForeground / numPixelsInMask;
  183. }
  184. private void findHandPoints(Image<Gray, Byte> foregroundMask)
  185. {
  186. MCvConvexityDefect? thumbDefect = findThumbDefect();
  187. if (thumbDefect != null)
  188. {
  189. ThumbDefectDepth = new Vector2D(thumbDefect.Value.DepthPoint);
  190. ThumbDefectStart = new Vector2D(thumbDefect.Value.StartPoint);
  191. ThumbDefectEnd = new Vector2D(thumbDefect.Value.EndPoint);
  192. if (!thumbDefectDepthFilter.Initialized)
  193. {
  194. thumbDefectDepthFilter.setInitialPosition(ThumbDefectDepth);
  195. thumbDefectStartFilter.setInitialPosition(ThumbDefectStart);
  196. thumbDefectEndFilter.setInitialPosition(ThumbDefectEnd);
  197. }
  198. else
  199. {
  200. ThumbDefectDepth = thumbDefectDepthFilter.getCorrectedPosition(ThumbDefectDepth);
  201. ThumbDefectStart = thumbDefectStartFilter.getCorrectedPosition(ThumbDefectStart);
  202. ThumbDefectEnd = thumbDefectEndFilter.getCorrectedPosition(ThumbDefectEnd);
  203. }
  204. Vector2D handLength, handWidth, longestLineEndpoint, topLeft, bottomLeft, bottomRight, topRight;
  205. Vector2D startDepth = ThumbDefectStart - ThumbDefectDepth;
  206. Vector2D endDepth = ThumbDefectEnd - ThumbDefectDepth;
  207. if (startDepth.Length > endDepth.Length)
  208. {
  209. handLength = startDepth;
  210. longestLineEndpoint = ThumbDefectStart;
  211. if (hands.Count == 1)
  212. {
  213. if (ThumbDefectStart.X > ThumbDefectDepth.X)
  214. hands[0].Side = Hand.HandSide.Left;
  215. else
  216. hands[0].Side = Hand.HandSide.Right;
  217. }
  218. }
  219. else
  220. {
  221. handLength = endDepth;
  222. longestLineEndpoint = ThumbDefectEnd;
  223. if (hands.Count == 1)
  224. {
  225. if (ThumbDefectEnd.X > ThumbDefectDepth.X)
  226. hands[0].Side = Hand.HandSide.Left;
  227. else
  228. hands[0].Side = Hand.HandSide.Right;
  229. }
  230. }
  231. if (palmHand.Side == Hand.HandSide.Left)
  232. {
  233. handWidth = 0.85f * new Vector2D(-handLength.Y, handLength.X);
  234. topLeft = longestLineEndpoint + 0.15f * handLength;
  235. bottomLeft = ThumbDefectDepth - 0.4f * handLength;
  236. bottomRight = bottomLeft + handWidth;
  237. topRight = bottomRight + 1.2f * handLength - 0.3f * handWidth;
  238. }
  239. else
  240. {
  241. handWidth = 0.85f * new Vector2D(handLength.Y, -handLength.X);
  242. topRight = longestLineEndpoint + 0.15f * handLength;
  243. bottomRight = ThumbDefectDepth - 0.4f * handLength;
  244. bottomLeft = bottomRight + handWidth;
  245. topLeft = bottomLeft + 1.2f * handLength - 0.3f * handWidth;
  246. }
  247. Quadrangle quad = new Quadrangle(bottomLeft, topLeft, topRight, bottomRight, foregroundMask.Width, foregroundMask.Height);
  248. if ((lastPalmQuad == null ||
  249. (quad.Area / lastPalmQuad.Area >= Constants.PalmMinAreaQuotient && quad.Area / lastPalmQuad.Area <= Constants.PalmMaxAreaQuotient)) &&
  250. getForegroundPixelPercentage(quad, foregroundMask) >= Constants.PalmMinPrecentageQuadForeground)
  251. {
  252. PalmQuad = quad;
  253. PalmHandSide = palmHand.Side;
  254. lastPalmQuad = PalmQuad;
  255. }
  256. }
  257. if (lastPalmQuad != null && getForegroundPixelPercentage(lastPalmQuad, foregroundMask) <= Constants.PalmMaxPrecentageQuadForegroundReset)
  258. reset();
  259. }
  260. }
  261. }