PalmDetector.cs 13 KB

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