PalmDetector.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 LineSegment2DF wristLine, thumbLine;
  26. private Quadrangle palmQuad;
  27. private bool valid = false;
  28. private Vector2D topLeft;
  29. private Vector2D topRight;
  30. private Vector2D bottomLeft;
  31. private Vector2D bottomRight;
  32. public PalmDetector(DepthImage depthImage, EdgeImage edgeImage, List<Finger> detectedFingers, PalmImage palmImage)
  33. {
  34. width = depthImage.getWidth();
  35. height = depthImage.getHeight();
  36. this.depthImage = depthImage;
  37. this.edgeImage = edgeImage;
  38. this.palmImage = palmImage;
  39. handImage = depthImage.getImage().Convert<Byte>(delegate(short s) { return (s == depthImage.getMaxDepth()) ? (byte)0 : (byte)1; });
  40. fingers = getFingersWithoutThumb(detectedFingers);
  41. buildPointingHandMask();
  42. handImage = handImage.And(pointingHandMask);
  43. findLongestPalmContour();
  44. if (palmContour != null)
  45. {
  46. findConvexityDefactsSortedByDepth();
  47. removeConvexityDefectsNearFingerTips();
  48. findHandPoints();
  49. if (valid)
  50. {
  51. removePointsFromContour(wristLine, 1);
  52. removePointsFromContour(thumbLine, 1);
  53. draw();
  54. }
  55. }
  56. }
  57. public Quadrangle getPalmQuad()
  58. {
  59. return palmQuad;
  60. }
  61. private List<Finger> getFingersWithoutThumb(List<Finger> detectedFingers)
  62. {
  63. Finger leftMost = null;
  64. float minX = float.MaxValue;
  65. foreach (Finger f in detectedFingers)
  66. {
  67. float midX = ((f.Hand + f.Tip) / 2).X;
  68. if (midX < minX)
  69. {
  70. minX = midX;
  71. leftMost = f;
  72. }
  73. }
  74. List<Finger> result = new List<Finger>();
  75. foreach (Finger f in detectedFingers)
  76. {
  77. if (f != leftMost)
  78. result.Add(f);
  79. }
  80. return result;
  81. }
  82. private void fillFingerSlices(Image<Gray, Byte> image, byte val)
  83. {
  84. foreach (Finger f in fingers)
  85. {
  86. foreach (FingerSlice s in f.SliceTrail.getSlices())
  87. {
  88. image.Draw(new LineSegment2DF(s.Start, s.End), new Gray(val), 1);
  89. }
  90. }
  91. }
  92. private Finger getLongestFinger()
  93. {
  94. float maxLength = 0;
  95. Finger longest = null;
  96. foreach (Finger f in fingers)
  97. {
  98. if (f.Line.Length > maxLength)
  99. {
  100. maxLength = f.Line.Length;
  101. longest = f;
  102. }
  103. }
  104. return longest;
  105. }
  106. private Point getPointInPointingHand()
  107. {
  108. Finger finger = getLongestFinger();
  109. if (finger == null)
  110. return new Point(0, 0);
  111. Vector2D direction = (finger.Hand - finger.Tip).normalize();
  112. Vector2D pos = finger.Hand + 20 * direction;
  113. return new Point(HelperFunctions.thresholdRange<int>(0, width - 1, (int) pos.X), HelperFunctions.thresholdRange<int>(0, height - 1, (int) pos.Y));
  114. }
  115. private void buildPointingHandMask()
  116. {
  117. pointingHandMask = new Image<Gray, byte>(width, height, new Gray(0));
  118. fillFingerSlices(pointingHandMask, 1);
  119. pointingHandMask = pointingHandMask.Dilate(1);
  120. pointingHandMask = pointingHandMask.Or(edgeImage.getImage().Convert<Byte>(delegate(byte b) { return (b == 0) ? (byte)0 : (byte)1; }));
  121. pointingHandMask = pointingHandMask.Dilate(1);
  122. MCvConnectedComp tmp = new MCvConnectedComp();
  123. CvInvoke.cvFloodFill(pointingHandMask.Ptr, getPointInPointingHand(), new MCvScalar(2), new MCvScalar(0), new MCvScalar(0), out tmp, 0, IntPtr.Zero);
  124. pointingHandMask = pointingHandMask.Convert<Byte>(delegate(byte b) { return (b == 2) ? (byte)0 : (byte)1; });
  125. pointingHandMask = pointingHandMask.Erode(1);
  126. fillFingerSlices(pointingHandMask, 0);
  127. pointingHandMask = pointingHandMask.Erode(2);
  128. }
  129. private void findLongestPalmContour()
  130. {
  131. Contour<Point> contour = handImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
  132. palmContour = contour;
  133. double maxPerimeter = 0;
  134. while (contour != null)
  135. {
  136. if (contour.Perimeter > maxPerimeter)
  137. {
  138. maxPerimeter = contour.Perimeter;
  139. palmContour = contour;
  140. }
  141. contour = contour.HNext;
  142. }
  143. }
  144. private void findConvexityDefactsSortedByDepth()
  145. {
  146. convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
  147. convexityDefects.Sort(delegate(MCvConvexityDefect d1, MCvConvexityDefect d2)
  148. {
  149. if (d1.Depth < d2.Depth)
  150. return 1;
  151. else if (d1.Depth > d2.Depth)
  152. return -1;
  153. return 0;
  154. });
  155. }
  156. private void removeConvexityDefectsNearFingerTips()
  157. {
  158. List<MCvConvexityDefect> newDefects = new List<MCvConvexityDefect>();
  159. foreach (MCvConvexityDefect d in convexityDefects)
  160. {
  161. float minFingerTipDist = float.MaxValue;
  162. foreach (Finger f in fingers)
  163. {
  164. float dist = f.Tip.getDistanceTo(new Vector2D(d.DepthPoint));
  165. if (dist < minFingerTipDist)
  166. minFingerTipDist = dist;
  167. }
  168. if (minFingerTipDist > 20)
  169. newDefects.Add(d);
  170. }
  171. convexityDefects = newDefects;
  172. }
  173. private void findHandPoints() {
  174. if (convexityDefects.Count > 0)
  175. {
  176. MCvConvexityDefect thumbDefect = convexityDefects[0];
  177. Vector2D thumb = new Vector2D(thumbDefect.DepthPoint);
  178. Vector2D thumbDefectStart = new Vector2D(thumbDefect.StartPoint);
  179. Vector2D handLength = thumbDefectStart - thumb;
  180. Vector2D handWidth = 0.8f * new Vector2D(handLength.Y, -handLength.X);
  181. topLeft = thumbDefectStart;
  182. bottomLeft = thumb - 0.3f * handLength;
  183. bottomRight = bottomLeft + handWidth;
  184. topRight = bottomRight + 1.5f * handLength;
  185. wristLine = new LineSegment2DF(bottomLeft - 1000 * handWidth, bottomRight + 1000 * handWidth);
  186. thumbLine = new LineSegment2DF(topLeft + 1000 * handLength, bottomLeft - 1000 * handLength);
  187. palmQuad = new Quadrangle(new Vector2D[] {bottomLeft, topLeft, topRight, bottomRight});
  188. valid = true;
  189. }
  190. else {
  191. palmQuad = null;
  192. valid = false;
  193. }
  194. }
  195. private void removePointsFromContour(LineSegment2DF line, int sideToRemove)
  196. {
  197. Contour<Point> newContour = new Contour<Point>(new MemStorage());
  198. int index = 0;
  199. foreach (Point p in palmContour)
  200. {
  201. if (line.Side(p) != sideToRemove)
  202. {
  203. newContour.Insert(index, p);
  204. ++index;
  205. }
  206. }
  207. palmContour = newContour;
  208. }
  209. private void draw()
  210. {
  211. palmImage.drawContour(palmContour);
  212. palmImage.drawLine(wristLine, PalmImageState.wristLine);
  213. palmImage.drawLine(thumbLine, PalmImageState.thumbLine);
  214. if (palmQuad != null)
  215. {
  216. Vector2D[] vertices = palmQuad.Vertices;
  217. for (int i = 0; i < 4; ++i)
  218. palmImage.drawLine(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), PalmImageState.palmRect);
  219. palmImage.drawGrid(new Vector2D(vertices[0]), new Vector2D(vertices[1]), new Vector2D(vertices[2]), new Vector2D(vertices[3]));
  220. }
  221. }
  222. }
  223. }