PalmDetector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. namespace bbiwarg.Detectors.Palm
  13. {
  14. class PalmDetector
  15. {
  16. private int width, height;
  17. private DepthImage depthImage;
  18. private EdgeImage edgeImage;
  19. private PalmImage palmImage;
  20. private Image<Gray, Byte> handImage;
  21. private Image<Gray, Byte> pointingHandMask;
  22. private List<Finger> fingers;
  23. private Contour<Point> palmContour;
  24. private List<MCvConvexityDefect> convexityDefects;
  25. private Vector2D wristPoint, wristDirection;
  26. private LineSegment2DF wristLine, thumbLine;
  27. private Quad2D palmRect;
  28. public PalmDetector(DepthImage depthImage, EdgeImage edgeImage, FingerDetector fingerDetector, PalmImage palmImage)
  29. {
  30. width = depthImage.getWidth();
  31. height = depthImage.getHeight();
  32. this.depthImage = depthImage;
  33. this.edgeImage = edgeImage;
  34. this.palmImage = palmImage;
  35. handImage = depthImage.getImage().Convert<Byte>(delegate(short s) { return (s == depthImage.getMaxDepth()) ? (byte)0 : (byte)1; });
  36. fingers = getFingersWithoutThumb(fingerDetector);
  37. buildPointingHandMask();
  38. handImage = handImage.And(pointingHandMask);
  39. findLongestPalmContour();
  40. if (palmContour != null)
  41. {
  42. findConvexityDefactsSortedByDepth();
  43. removeConvexityDefectsNearFingerTips();
  44. findThumbLine();
  45. findWristLine();
  46. removePointsFromContour(wristLine, 1);
  47. removePointsFromContour(thumbLine, 1);
  48. if (palmContour.Count<Point>() != 0)
  49. {
  50. findPalmRect();
  51. draw();
  52. }
  53. }
  54. }
  55. public PalmRect getPalm()
  56. {
  57. return new PalmRect(palmRect);
  58. }
  59. private List<Finger> getFingersWithoutThumb(FingerDetector fingerDetector)
  60. {
  61. List<Finger> detectedFingers = fingerDetector.Fingers;
  62. Finger leftMost = null;
  63. float minX = float.MaxValue;
  64. foreach (Finger f in detectedFingers)
  65. {
  66. float midX = ((f.Hand + f.Tip) / 2).X;
  67. if (midX < minX)
  68. {
  69. minX = midX;
  70. leftMost = f;
  71. }
  72. }
  73. List<Finger> result = new List<Finger>();
  74. foreach (Finger f in detectedFingers)
  75. {
  76. if (f != leftMost)
  77. result.Add(f);
  78. }
  79. return result;
  80. }
  81. private void fillFingerSlices(Image<Gray, Byte> image, byte val)
  82. {
  83. foreach (Finger f in fingers)
  84. {
  85. foreach (FingerSlice s in f.SliceTrail.getSlices())
  86. {
  87. image.Draw(new LineSegment2DF(s.Start, s.End), new Gray(val), 1);
  88. }
  89. }
  90. }
  91. private Finger getLongestFinger()
  92. {
  93. float maxLength = 0;
  94. Finger longest = null;
  95. foreach (Finger f in fingers)
  96. {
  97. if (f.Line.Length > maxLength)
  98. {
  99. maxLength = f.Line.Length;
  100. longest = f;
  101. }
  102. }
  103. return longest;
  104. }
  105. private Point getPointInPointingHand()
  106. {
  107. Finger finger = getLongestFinger();
  108. if (finger == null)
  109. return new Point(0, 0);
  110. Vector2D direction = (finger.Hand - finger.Tip).normalize();
  111. Vector2D pos = finger.Hand + 20 * direction;
  112. return new Point(HelperFunctions.thresholdRange<int>(0, width - 1, (int) pos.X), HelperFunctions.thresholdRange<int>(0, height - 1, (int) pos.Y));
  113. }
  114. private void buildPointingHandMask()
  115. {
  116. pointingHandMask = new Image<Gray, byte>(width, height, new Gray(0));
  117. fillFingerSlices(pointingHandMask, 1);
  118. pointingHandMask = pointingHandMask.Dilate(1);
  119. pointingHandMask = pointingHandMask.Or(edgeImage.getImage().Convert<Byte>(delegate(byte b) { return (b == 0) ? (byte)0 : (byte)1; }));
  120. pointingHandMask = pointingHandMask.Dilate(1);
  121. MCvConnectedComp tmp = new MCvConnectedComp();
  122. CvInvoke.cvFloodFill(pointingHandMask.Ptr, getPointInPointingHand(), new MCvScalar(2), new MCvScalar(0), new MCvScalar(0), out tmp, 0, IntPtr.Zero);
  123. pointingHandMask = pointingHandMask.Convert<Byte>(delegate(byte b) { return (b == 2) ? (byte)0 : (byte)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 findWristDirection()
  144. {
  145. PointF[] points = new PointF[palmContour.Count<Point>()];
  146. int index = 0;
  147. foreach (Point p in palmContour)
  148. {
  149. points[index] = new PointF(p.X, p.Y);
  150. ++index;
  151. }
  152. PointF direction, tmp;
  153. PointCollection.Line2DFitting(points, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out tmp);
  154. //Vector2D direction = new Vector2D(thumbLine.Direction);
  155. wristDirection = new Vector2D(-direction.Y, direction.X);
  156. }
  157. private void findConvexityDefactsSortedByDepth()
  158. {
  159. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  160. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  161. {
  162. if (d1.Depth < d2.Depth)
  163. return 1;
  164. else if (d1.Depth > d2.Depth)
  165. return -1;
  166. return 0;
  167. });
  168. }
  169. private void removeConvexityDefectsNearFingerTips()
  170. {
  171. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  172. foreach (MCvConvexityDefect d in convexityDefects)
  173. {
  174. float minFingerTipDist = float.MaxValue;
  175. foreach (Finger f in fingers)
  176. {
  177. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  178. if (dist < minFingerTipDist)
  179. minFingerTipDist = dist;
  180. }
  181. if (minFingerTipDist > 20)
  182. newDefects.Add(d);
  183. }
  184. convexityDefects = newDefects;
  185. }
  186. private void findWristPoint()
  187. {
  188. if (convexityDefects.Count > 1)
  189. wristPoint = new Vector2D(convexityDefects[1].DepthPoint);
  190. else
  191. wristPoint = new Vector2D(-1, -1);
  192. }
  193. private void findWristLine()
  194. {
  195. findWristPoint();
  196. findWristDirection();
  197. wristLine = new LineSegment2DF(wristPoint - 1000 * wristDirection, wristPoint + 1000 * wristDirection);
  198. }
  199. private void findThumbLine()
  200. {
  201. if (convexityDefects.Count > 0)
  202. {
  203. MCvConvexityDefect thumbDefect = convexityDefects[0];
  204. Vector2D p1 = new Vector2D(thumbDefect.DepthPoint);
  205. Vector2D p2 = new Vector2D(thumbDefect.StartPoint);
  206. Vector2D direction = (p1 - p2).normalize();
  207. thumbLine = new LineSegment2DF(p1 - 1000 * direction, p1 + 1000 * direction);
  208. }
  209. else
  210. {
  211. thumbLine = new LineSegment2DF(new PointF(-1, -1), new PointF(-1, -1));
  212. }
  213. }
  214. private void removePointsFromContour(LineSegment2DF line, int sideToRemove)
  215. {
  216. Contour<Point> newContour = new Contour<Point>(new MemStorage());
  217. int index = 0;
  218. foreach (Point p in palmContour)
  219. {
  220. if (line.Side(p) != sideToRemove)
  221. {
  222. newContour.Insert(index, p);
  223. ++index;
  224. }
  225. }
  226. palmContour = newContour;
  227. }
  228. private void findPalmRect()
  229. {
  230. //palmRect = palmContour.GetMinAreaRect();
  231. Line2D thumbLine2 = new Line2D(new Vector2D(thumbLine.P1), new Vector2D(thumbLine.P2));
  232. Line2D wristLine2 = new Line2D(new Vector2D(wristLine.P1), new Vector2D(wristLine.P2));
  233. Vector2D intersection = thumbLine2.intersection(wristLine2);
  234. if (intersection != null)
  235. {
  236. Vector2D maxThumb = new Vector2D(0, 0), maxWrist = new Vector2D(0, 0);
  237. float maxDistanceThumb = 0, maxDistanceWrist = 0;
  238. foreach (Point p in palmContour)
  239. {
  240. Vector2D v = new Vector2D(p);
  241. Vector2D projected = thumbLine2.projectToLine(v);
  242. float dist = projected.getDistanceTo(intersection);
  243. if (dist > maxDistanceThumb)
  244. {
  245. maxDistanceThumb = dist;
  246. maxThumb = projected;
  247. }
  248. projected = wristLine2.projectToLine(v);
  249. dist = projected.getDistanceTo(intersection);
  250. if (dist > maxDistanceWrist)
  251. {
  252. maxDistanceWrist = dist;
  253. maxWrist = projected;
  254. }
  255. }
  256. Vector2D origin = intersection;
  257. palmRect = new Quad2D(origin, (maxThumb - origin), (maxWrist - origin));
  258. }
  259. else
  260. {
  261. palmRect = null;
  262. }
  263. }
  264. private void draw()
  265. {
  266. palmImage.drawContour(palmContour);
  267. palmImage.drawLine(wristLine, PalmImageState.wristLine);
  268. palmImage.drawLine(thumbLine, PalmImageState.thumbLine);
  269. if (palmRect != null)
  270. {
  271. PointF[] vertices = palmRect.getVertices();
  272. for (int i = 0; i < 4; ++i)
  273. palmImage.drawLine(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), PalmImageState.palmRect);
  274. palmImage.drawGrid(new Vector2D(vertices[0]), new Vector2D(vertices[1]), new Vector2D(vertices[2]), new Vector2D(vertices[3]));
  275. }
  276. }
  277. }
  278. }