PalmDetector.cs 9.5 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. 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 MCvBox2D 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. findWristLine();
  45. findThumbLine();
  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. wristDirection = new Vector2D(-direction.Y, direction.X);
  155. }
  156. private void findConvexityDefactsSortedByDepth()
  157. {
  158. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  159. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  160. {
  161. if (d1.Depth < d2.Depth)
  162. return 1;
  163. else if (d1.Depth > d2.Depth)
  164. return -1;
  165. return 0;
  166. });
  167. }
  168. private void removeConvexityDefectsNearFingerTips()
  169. {
  170. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  171. foreach (MCvConvexityDefect d in convexityDefects)
  172. {
  173. float minFingerTipDist = float.MaxValue;
  174. foreach (Finger f in fingers)
  175. {
  176. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  177. if (dist < minFingerTipDist)
  178. minFingerTipDist = dist;
  179. }
  180. if (minFingerTipDist > 20)
  181. newDefects.Add(d);
  182. }
  183. convexityDefects = newDefects;
  184. }
  185. private void findWristPoint()
  186. {
  187. if (convexityDefects.Count > 1)
  188. wristPoint = new Vector2D(convexityDefects[1].DepthPoint);
  189. else
  190. wristPoint = new Vector2D(-1, -1);
  191. }
  192. private void findWristLine()
  193. {
  194. findWristPoint();
  195. findWristDirection();
  196. wristLine = new LineSegment2DF(wristPoint - 1000 * wristDirection, wristPoint + 1000 * wristDirection);
  197. }
  198. private void findThumbLine()
  199. {
  200. if (convexityDefects.Count > 0)
  201. {
  202. MCvConvexityDefect thumbDefect = convexityDefects[0];
  203. Vector2D p1 = new Vector2D(thumbDefect.DepthPoint);
  204. Vector2D p2 = new Vector2D(thumbDefect.StartPoint);
  205. Vector2D direction = (p1 - p2).normalize();
  206. thumbLine = new LineSegment2DF(p1 - 1000 * direction, p1 + 1000 * direction);
  207. }
  208. else
  209. {
  210. thumbLine = new LineSegment2DF(new PointF(-1, -1), new PointF(-1, -1));
  211. }
  212. }
  213. private void removePointsFromContour(LineSegment2DF line, int sideToRemove)
  214. {
  215. Contour<Point> newContour = new Contour<Point>(new MemStorage());
  216. int index = 0;
  217. foreach (Point p in palmContour)
  218. {
  219. if (line.Side(p) != sideToRemove)
  220. {
  221. newContour.Insert(index, p);
  222. ++index;
  223. }
  224. }
  225. palmContour = newContour;
  226. }
  227. private void findPalmRect()
  228. {
  229. palmRect = palmContour.GetMinAreaRect();
  230. }
  231. private void draw()
  232. {
  233. palmImage.drawContour(palmContour);
  234. palmImage.drawLine(wristLine, PalmImageState.wristLine);
  235. palmImage.drawLine(thumbLine, PalmImageState.thumbLine);
  236. PointF[] vertices = palmRect.GetVertices();
  237. for (int i = 0; i < 4; ++i)
  238. palmImage.drawLine(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), PalmImageState.palmRect);
  239. palmImage.drawGrid(new Vector2D(vertices[0]),new Vector2D(vertices[1]),new Vector2D(vertices[2]),new Vector2D(vertices[3]));
  240. }
  241. }
  242. }