PalmDetector.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. findPalmRect();
  49. draw();
  50. }
  51. }
  52. public Palm getPalm()
  53. {
  54. return new Palm(palmRect);
  55. }
  56. private List<Finger> getFingersWithoutThumb(FingerDetector fingerDetector)
  57. {
  58. List<Finger> detectedFingers = fingerDetector.Fingers;
  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.getSlices())
  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.Line.Length > maxLength)
  95. {
  96. maxLength = f.Line.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, (int) pos.X), HelperFunctions.thresholdRange<int>(0, height - 1, (int) pos.Y));
  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. pointingHandMask = pointingHandMask.Or(edgeImage.getImage().Convert<Byte>(delegate(byte b) { return (b == 0) ? (byte)0 : (byte)1; }));
  117. pointingHandMask = pointingHandMask.Dilate(1);
  118. MCvConnectedComp tmp = new MCvConnectedComp();
  119. CvInvoke.cvFloodFill(pointingHandMask.Ptr, getPointInPointingHand(), new MCvScalar(2), new MCvScalar(0), new MCvScalar(0), out tmp, 0, IntPtr.Zero);
  120. pointingHandMask = pointingHandMask.Convert<Byte>(delegate(byte b) { return (b == 2) ? (byte)0 : (byte)1; });
  121. pointingHandMask = pointingHandMask.Erode(1);
  122. fillFingerSlices(pointingHandMask, 0);
  123. pointingHandMask = pointingHandMask.Erode(2);
  124. }
  125. private void findLongestPalmContour()
  126. {
  127. Contour<Point> contour = handImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  128. palmContour = contour;
  129. double maxPerimeter = 0;
  130. while (contour != null)
  131. {
  132. if (contour.Perimeter > maxPerimeter)
  133. {
  134. maxPerimeter = contour.Perimeter;
  135. palmContour = contour;
  136. }
  137. contour = contour.HNext;
  138. }
  139. }
  140. private void findWristDirection()
  141. {
  142. PointF[] points = new PointF[palmContour.Count<Point>()];
  143. int index = 0;
  144. foreach (Point p in palmContour)
  145. {
  146. points[index] = new PointF(p.X, p.Y);
  147. ++index;
  148. }
  149. PointF direction, tmp;
  150. PointCollection.Line2DFitting(points, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out tmp);
  151. wristDirection = new Vector2D(-direction.Y, direction.X);
  152. }
  153. private void findConvexityDefactsSortedByDepth()
  154. {
  155. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  156. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  157. {
  158. if (d1.Depth < d2.Depth)
  159. return 1;
  160. else if (d1.Depth > d2.Depth)
  161. return -1;
  162. return 0;
  163. });
  164. }
  165. private void removeConvexityDefectsNearFingerTips()
  166. {
  167. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  168. foreach (MCvConvexityDefect d in convexityDefects)
  169. {
  170. float minFingerTipDist = float.MaxValue;
  171. foreach (Finger f in fingers)
  172. {
  173. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  174. if (dist < minFingerTipDist)
  175. minFingerTipDist = dist;
  176. }
  177. if (minFingerTipDist > 20)
  178. newDefects.Add(d);
  179. }
  180. convexityDefects = newDefects;
  181. }
  182. private void findWristPoint()
  183. {
  184. if (convexityDefects.Count > 1)
  185. wristPoint = new Vector2D(convexityDefects[1].DepthPoint);
  186. else
  187. wristPoint = new Vector2D(-1, -1);
  188. }
  189. private void findWristLine()
  190. {
  191. findWristPoint();
  192. findWristDirection();
  193. wristLine = new LineSegment2DF(wristPoint - 1000 * wristDirection, wristPoint + 1000 * wristDirection);
  194. }
  195. private void findThumbLine()
  196. {
  197. if (convexityDefects.Count > 0)
  198. {
  199. MCvConvexityDefect thumbDefect = convexityDefects[0];
  200. Vector2D p1 = new Vector2D(thumbDefect.DepthPoint);
  201. Vector2D p2 = new Vector2D(thumbDefect.StartPoint);
  202. Vector2D direction = (p1 - p2).normalize();
  203. thumbLine = new LineSegment2DF(p1 - 1000 * direction, p1 + 1000 * direction);
  204. }
  205. else
  206. {
  207. thumbLine = new LineSegment2DF(new PointF(-1, -1), new PointF(-1, -1));
  208. }
  209. }
  210. private void removePointsFromContour(LineSegment2DF line, int sideToRemove)
  211. {
  212. Contour<Point> newContour = new Contour<Point>(new MemStorage());
  213. int index = 0;
  214. foreach (Point p in palmContour)
  215. {
  216. if (line.Side(p) != sideToRemove)
  217. {
  218. newContour.Insert(index, p);
  219. ++index;
  220. }
  221. }
  222. palmContour = newContour;
  223. }
  224. private void findPalmRect()
  225. {
  226. palmRect = palmContour.GetMinAreaRect();
  227. }
  228. private void draw()
  229. {
  230. palmImage.drawContour(palmContour);
  231. palmImage.drawLine(wristLine, PalmImageState.wristLine);
  232. palmImage.drawLine(thumbLine, PalmImageState.thumbLine);
  233. PointF[] vertices = palmRect.GetVertices();
  234. for (int i = 0; i < 4; ++i)
  235. palmImage.drawLine(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), PalmImageState.palmRect);
  236. }
  237. }
  238. }