PalmDetector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.Fingers;
  12. using bbiwarg.Graphics;
  13. namespace bbiwarg.Detectors.Palm
  14. {
  15. class PalmDetector
  16. {
  17. private int width, height;
  18. private DepthImage depthImage;
  19. private EdgeImage edgeImage;
  20. private OutputImage outputImage;
  21. private Image<Gray, Byte> handImage;
  22. private Image<Gray, Byte> pointingHandMask;
  23. private List<Finger> fingers;
  24. private Contour<Point> palmContour;
  25. private List<MCvConvexityDefect> convexityDefects;
  26. private static Kalman2DPositionFilter thumbDefactFilter;
  27. private bool valid = false;
  28. private Vector2D topLeft;
  29. private Vector2D topRight;
  30. private Vector2D bottomLeft;
  31. private Vector2D bottomRight;
  32. public Quadrangle PalmQuad;
  33. public PalmDetector(DepthImage depthImage, EdgeImage edgeImage, List<Finger> detectedFingers, OutputImage outputImage)
  34. {
  35. // TODO: determine which fingers are index or thumb-fingers and detect palm in thumb-hand
  36. width = depthImage.Width;
  37. height = depthImage.Height;
  38. this.depthImage = depthImage;
  39. this.edgeImage = edgeImage;
  40. this.outputImage = outputImage;
  41. // dst = (src > (MaxDepth - MinDepth)) ? 0 : 1
  42. handImage = depthImage.Image.ThresholdBinaryInv(new Gray(depthImage.MaxDepth - depthImage.MinDepth - 1), new Gray(1)).Convert<Gray, Byte>();
  43. fingers = getFingersWithoutThumb(detectedFingers);
  44. buildPointingHandMask();
  45. handImage = handImage.And(pointingHandMask);
  46. findLongestPalmContour();
  47. if (palmContour != null)
  48. {
  49. findConvexityDefactsSortedByDepth();
  50. removeConvexityDefectsNearFingerTips();
  51. findHandPoints();
  52. if (valid)
  53. {
  54. draw();
  55. }
  56. }
  57. }
  58. public static void resetFilter()
  59. {
  60. thumbDefactFilter = null;
  61. }
  62. private List<Finger> getFingersWithoutThumb(List<Finger> detectedFingers)
  63. {
  64. Finger leftMost = null;
  65. float minX = float.MaxValue;
  66. foreach (Finger f in detectedFingers)
  67. {
  68. float midX = ((f.Hand + f.Tip) / 2).X;
  69. if (midX < minX)
  70. {
  71. minX = midX;
  72. leftMost = f;
  73. }
  74. }
  75. List<Finger> result = new List<Finger>();
  76. foreach (Finger f in detectedFingers)
  77. {
  78. if (f != leftMost)
  79. result.Add(f);
  80. }
  81. return result;
  82. }
  83. private void fillFingerSlices(Image<Gray, Byte> image, byte val)
  84. {
  85. foreach (Finger f in fingers)
  86. {
  87. foreach (FingerSlice s in f.SliceTrail.Slices)
  88. {
  89. image.Draw(new LineSegment2DF(s.Start, s.End), new Gray(val), 1);
  90. }
  91. }
  92. }
  93. private Finger getLongestFinger()
  94. {
  95. float maxLength = 0;
  96. Finger longest = null;
  97. foreach (Finger f in fingers)
  98. {
  99. if (f.LineSegment.Length > maxLength)
  100. {
  101. maxLength = f.LineSegment.Length;
  102. longest = f;
  103. }
  104. }
  105. return longest;
  106. }
  107. private Point getPointInPointingHand()
  108. {
  109. Finger finger = getLongestFinger();
  110. if (finger == null)
  111. return new Point(0, 0);
  112. Vector2D direction = (finger.Hand - finger.Tip).normalize();
  113. Vector2D pos = finger.Hand + 20 * direction;
  114. return new Point(HelperFunctions.thresholdRange<int>(0, width - 1, pos.IntX), HelperFunctions.thresholdRange<int>(0, height - 1, pos.IntY));
  115. }
  116. private void buildPointingHandMask()
  117. {
  118. pointingHandMask = new Image<Gray, byte>(width, height, new Gray(0));
  119. fillFingerSlices(pointingHandMask, 1);
  120. pointingHandMask = pointingHandMask.Dilate(1);
  121. // dst = (src > 0) ? 1 : 0;
  122. pointingHandMask = pointingHandMask.Or(edgeImage.Image.ThresholdBinary(new Gray(0), new Gray(1)));
  123. pointingHandMask = pointingHandMask.Dilate(1);
  124. MCvConnectedComp tmp = new MCvConnectedComp();
  125. // fill with value 2
  126. CvInvoke.cvFloodFill(pointingHandMask.Ptr, getPointInPointingHand(), new MCvScalar(2), new MCvScalar(0), new MCvScalar(0), out tmp, 0, IntPtr.Zero);
  127. // dst = (src > 1) ? 0 : 1 (src > 1 <-> src == 2 <-> src filled by flood fill)
  128. pointingHandMask = pointingHandMask.ThresholdBinaryInv(new Gray(1), new Gray(1));
  129. pointingHandMask = pointingHandMask.Erode(1);
  130. fillFingerSlices(pointingHandMask, 0);
  131. pointingHandMask = pointingHandMask.Erode(2);
  132. }
  133. private void findLongestPalmContour()
  134. {
  135. Contour<Point> contour = handImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  136. palmContour = contour;
  137. double maxPerimeter = 0;
  138. while (contour != null)
  139. {
  140. if (contour.Perimeter > maxPerimeter)
  141. {
  142. maxPerimeter = contour.Perimeter;
  143. palmContour = contour;
  144. }
  145. contour = contour.HNext;
  146. }
  147. }
  148. private void findConvexityDefactsSortedByDepth()
  149. {
  150. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  151. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  152. {
  153. if (d1.Depth < d2.Depth)
  154. return 1;
  155. else if (d1.Depth > d2.Depth)
  156. return -1;
  157. return 0;
  158. });
  159. }
  160. private void removeConvexityDefectsNearFingerTips()
  161. {
  162. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  163. foreach (MCvConvexityDefect d in convexityDefects)
  164. {
  165. float minFingerTipDist = float.MaxValue;
  166. foreach (Finger f in fingers)
  167. {
  168. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  169. if (dist < minFingerTipDist)
  170. minFingerTipDist = dist;
  171. }
  172. if (minFingerTipDist > 20)
  173. newDefects.Add(d);
  174. }
  175. convexityDefects = newDefects;
  176. }
  177. private void findHandPoints()
  178. {
  179. if (convexityDefects.Count > 0)
  180. {
  181. MCvConvexityDefect thumbDefect = convexityDefects[0];
  182. Vector2D thumb;
  183. if (thumbDefactFilter == null)
  184. {
  185. thumb = new Vector2D(thumbDefect.DepthPoint);
  186. thumbDefactFilter = new Kalman2DPositionFilter(thumb, 1.0e-2f, 1.0e-2f);
  187. }
  188. else
  189. {
  190. thumb = thumbDefactFilter.getCorrectedPosition(new Vector2D(thumbDefect.DepthPoint));
  191. }
  192. Vector2D thumbDefectStart = new Vector2D(thumbDefect.StartPoint);
  193. Vector2D thumbDefectEnd = new Vector2D(thumbDefect.EndPoint);
  194. Vector2D handLength, handWidth;
  195. if (thumb.getDistanceTo(thumbDefectStart) > thumb.getDistanceTo(thumbDefectEnd))
  196. {
  197. //right hand
  198. handLength = thumbDefectStart - thumb;
  199. handWidth = 0.8f * new Vector2D(-handLength.Y, handLength.X);
  200. topLeft = thumbDefectStart;
  201. bottomLeft = thumb - 0.4f * handLength;
  202. bottomRight = bottomLeft + handWidth;
  203. topRight = bottomRight + 1.2f * handLength - 0.3f * handWidth;
  204. }
  205. else
  206. {
  207. //left hand
  208. handLength = thumbDefectEnd - thumb;
  209. handWidth = 0.8f * new Vector2D(handLength.Y, -handLength.X);
  210. topRight = thumbDefectEnd;
  211. bottomRight = thumb - 0.4f * handLength;
  212. bottomLeft = bottomRight + handWidth;
  213. topLeft = bottomLeft + 1.2f * handLength - 0.3f * handWidth;
  214. }
  215. PalmQuad = new Quadrangle(bottomLeft, topLeft, topRight, bottomRight);
  216. valid = true;
  217. }
  218. else
  219. {
  220. PalmQuad = null;
  221. valid = false;
  222. }
  223. }
  224. private void draw()
  225. {
  226. outputImage.drawContour(palmContour, 255, 0, 0);
  227. outputImage.drawPoints(palmContour.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE), 0, 255, 0);
  228. if (PalmQuad != null)
  229. {
  230. Vector2D[] vertices = PalmQuad.Vertices;
  231. for (int i = 0; i < 4; ++i)
  232. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(vertices[i], vertices[(i + 1) % 4]), 0, 0, 255);
  233. drawGrid(new Vector2D(vertices[0]), new Vector2D(vertices[1]), new Vector2D(vertices[2]), new Vector2D(vertices[3]));
  234. }
  235. }
  236. private void drawGrid(Vector2D a, Vector2D b, Vector2D c, Vector2D d)
  237. {
  238. int numRows = 4;
  239. int numColumns = 3;
  240. Vector2D relAB = (b - a) / numRows;
  241. Vector2D relDC = (c - d) / numRows;
  242. Vector2D relBC = (c - b) / numColumns;
  243. Vector2D relAD = (d - a) / numColumns;
  244. for (int i = 1; i < numRows; i++)
  245. {
  246. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAB, d + i * relDC), 80, 80, 255);
  247. }
  248. for (int i = 1; i < numColumns; i++)
  249. {
  250. outputImage.drawLineSegment(new bbiwarg.Utility.LineSegment2D(a + i * relAD, b + i * relBC), 80, 80, 255);
  251. }
  252. }
  253. }
  254. }