PalmDetector.cs 9.9 KB

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