PalmImage.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. using System.Drawing;
  9. using bbiwarg.Detectors.Fingers;
  10. using bbiwarg.Utility;
  11. namespace bbiwarg.Images
  12. {
  13. class PalmImage
  14. {
  15. DepthImage depthImage;
  16. EdgeImage edgeImage;
  17. private Image<Gray, Byte> handImage;
  18. private Image<Gray, Byte> pointingHandMask;
  19. private Image<Gray, Byte> outputImage;
  20. private List<Finger> fingers;
  21. private Contour<Point> palmContour;
  22. private List<MCvConvexityDefect> convexityDefects;
  23. private Vector2D wristPoint, wristDirection;
  24. private LineSegment2DF wristLine, thumbLine;
  25. private MCvBox2D palmRect;
  26. private int width, height;
  27. public PalmImage(DepthImage depthImage, EdgeImage edgeImage, FingerDetector fingerDetector)
  28. {
  29. this.depthImage = depthImage;
  30. this.edgeImage = edgeImage;
  31. handImage = depthImage.getImage().Convert<Byte>(delegate(short s) { return (s == depthImage.getMaxDepth()) ? (byte) 0 : (byte) 1; });
  32. width = depthImage.getWidth();
  33. height = depthImage.getHeight();
  34. outputImage = new Image<Gray, byte>(width, height, new Gray(0));
  35. fingers = getFingersWithoutThumb(fingerDetector);
  36. buildPointingHandMask();
  37. handImage = handImage.And(pointingHandMask);
  38. findLongestPalmContour();
  39. if (palmContour != null)
  40. {
  41. findConvexityDefactsSortedByDepth();
  42. removeConvexityDefectsNearFingerTips();
  43. findWristLine();
  44. findThumbLine();
  45. removePointsFromContour(wristLine, 1);
  46. removePointsFromContour(thumbLine, 1);
  47. findPalmRect();
  48. draw();
  49. }
  50. }
  51. private List<Finger> getFingersWithoutThumb(FingerDetector fingerDetector)
  52. {
  53. List<Finger> detectedFingers = fingerDetector.Fingers;
  54. Finger leftMost = null;
  55. float minX = float.MaxValue;
  56. foreach (Finger f in detectedFingers)
  57. {
  58. float midX = ((f.Hand + f.Tip) / 2).X;
  59. if (midX < minX)
  60. {
  61. minX = midX;
  62. leftMost = f;
  63. }
  64. }
  65. List<Finger> result = new List<Finger>();
  66. foreach (Finger f in detectedFingers)
  67. {
  68. if (f != leftMost)
  69. result.Add(f);
  70. }
  71. return result;
  72. }
  73. private void fillFingerSlices(Image<Gray, Byte> image, byte val)
  74. {
  75. foreach (Finger f in fingers)
  76. {
  77. foreach (FingerSlice s in f.SliceTrail.getSlices())
  78. {
  79. image.Draw(new LineSegment2DF(s.Start, s.End), new Gray(val), 1);
  80. }
  81. }
  82. }
  83. private Finger getLongestFinger()
  84. {
  85. float maxLength = 0;
  86. Finger longest = null;
  87. foreach (Finger f in fingers)
  88. {
  89. if (f.Line.Length > maxLength)
  90. {
  91. maxLength = f.Line.Length;
  92. longest = f;
  93. }
  94. }
  95. return longest;
  96. }
  97. private Point getPointInPointingHand()
  98. {
  99. Finger finger = getLongestFinger();
  100. if (finger == null)
  101. return new Point(0, 0);
  102. Vector2D direction = (finger.Hand - finger.Tip).normalize();
  103. Vector2D pos = finger.Hand + 20 * direction;
  104. return new Point(Math.Min(width - 1, Math.Max(0, (int)pos.X)), Math.Min(height - 1, Math.Max(0, (int)pos.Y)));
  105. }
  106. private void buildPointingHandMask()
  107. {
  108. pointingHandMask = new Image<Gray, byte>(width, height, new Gray(0));
  109. fillFingerSlices(pointingHandMask, 1);
  110. pointingHandMask = pointingHandMask.Dilate(1);
  111. pointingHandMask = pointingHandMask.Or(edgeImage.getImage().Convert<Byte>(delegate(byte b) { return (b == 0) ? (byte)0 : (byte)1; }));
  112. pointingHandMask = pointingHandMask.Dilate(1);
  113. MCvConnectedComp tmp = new MCvConnectedComp();
  114. CvInvoke.cvFloodFill(pointingHandMask.Ptr, getPointInPointingHand(), new MCvScalar(2), new MCvScalar(0), new MCvScalar(0), out tmp, 0, IntPtr.Zero);
  115. pointingHandMask = pointingHandMask.Convert<Byte>(delegate(byte b) { return (b == 2) ? (byte)0 : (byte)1; });
  116. pointingHandMask = pointingHandMask.Erode(1);
  117. fillFingerSlices(pointingHandMask, 0);
  118. pointingHandMask = pointingHandMask.Erode(2);
  119. }
  120. private void findLongestPalmContour()
  121. {
  122. Contour<Point> contour = handImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  123. palmContour = contour;
  124. double maxPerimeter = 0;
  125. while (contour != null)
  126. {
  127. if (contour.Perimeter > maxPerimeter)
  128. {
  129. maxPerimeter = contour.Perimeter;
  130. palmContour = contour;
  131. }
  132. contour = contour.HNext;
  133. }
  134. }
  135. private void findWristDirection()
  136. {
  137. PointF[] points = new PointF[palmContour.Count<Point>()];
  138. int index = 0;
  139. foreach (Point p in palmContour)
  140. {
  141. points[index] = new PointF(p.X, p.Y);
  142. ++index;
  143. }
  144. PointF direction, tmp;
  145. PointCollection.Line2DFitting(points, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out tmp);
  146. wristDirection = new Vector2D(-direction.Y, direction.X);
  147. }
  148. private void findConvexityDefactsSortedByDepth()
  149. {
  150. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  151. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2) {
  152. if (d1.Depth < d2.Depth)
  153. return 1;
  154. else if (d1.Depth > d2.Depth)
  155. return -1;
  156. return 0;
  157. });
  158. }
  159. private void removeConvexityDefectsNearFingerTips()
  160. {
  161. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  162. foreach (MCvConvexityDefect d in convexityDefects)
  163. {
  164. float minFingerTipDist = float.MaxValue;
  165. foreach (Finger f in fingers)
  166. {
  167. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  168. if (dist < minFingerTipDist)
  169. minFingerTipDist = dist;
  170. }
  171. if (minFingerTipDist > 20)
  172. newDefects.Add(d);
  173. }
  174. convexityDefects = newDefects;
  175. }
  176. private void findWristPoint()
  177. {
  178. if (convexityDefects.Count > 1)
  179. wristPoint = new Vector2D(convexityDefects[1].DepthPoint);
  180. else
  181. wristPoint = new Vector2D(-1, -1);
  182. }
  183. private void findWristLine()
  184. {
  185. findWristPoint();
  186. findWristDirection();
  187. wristLine = new LineSegment2DF(wristPoint - 1000 * wristDirection, wristPoint + 1000 * wristDirection);
  188. }
  189. private void findThumbLine()
  190. {
  191. if (convexityDefects.Count > 0)
  192. {
  193. MCvConvexityDefect thumbDefect = convexityDefects[0];
  194. Vector2D p1 = new Vector2D(thumbDefect.DepthPoint);
  195. Vector2D p2 = new Vector2D(thumbDefect.StartPoint);
  196. Vector2D direction = (p1 - p2).normalize();
  197. thumbLine = new LineSegment2DF(p1 - 1000 * direction, p1 + 1000 * direction);
  198. }
  199. else
  200. {
  201. thumbLine = new LineSegment2DF(new PointF(-1, -1), new PointF(-1, -1));
  202. }
  203. }
  204. private void removePointsFromContour(LineSegment2DF line, int sideToRemove)
  205. {
  206. Contour<Point> newContour = new Contour<Point>(new MemStorage());
  207. int index = 0;
  208. foreach (Point p in palmContour)
  209. {
  210. if (line.Side(p) != sideToRemove)
  211. {
  212. newContour.Insert(index, p);
  213. ++index;
  214. }
  215. }
  216. palmContour = newContour;
  217. }
  218. private void findPalmRect()
  219. {
  220. palmRect = palmContour.GetMinAreaRect();
  221. }
  222. private void draw()
  223. {
  224. //outputImage.Draw(palmContour, new Gray(1), 1);
  225. PointF[] vertices = palmRect.GetVertices();
  226. for (int i = 0; i < 4; ++i)
  227. outputImage.Draw(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), new Gray(1), 1);
  228. outputImage.Draw(wristLine, new Gray(1), 1);
  229. outputImage.Draw(thumbLine, new Gray(1), 1);
  230. }
  231. public bool belongsToPalm(int x, int y)
  232. {
  233. return outputImage.Data[y, x, 0] == 1;
  234. }
  235. }
  236. }