HandDetector.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.Images;
  8. using bbiwarg.Recognition.FingerRecognition;
  9. using bbiwarg.Graphics;
  10. using bbiwarg.Utility;
  11. using Emgu.CV;
  12. using Emgu.CV.Structure;
  13. namespace bbiwarg.Recognition.HandRecognition
  14. {
  15. class HandDetector
  16. {
  17. private DepthImage depthImage;
  18. private EdgeImage edgeImage;
  19. private List<Finger> fingers;
  20. public List<Hand> Hands { get; private set; }
  21. public Image<Gray, byte> HandMask { get; private set; }
  22. public OutputImage outputImage;
  23. public HandDetector(DepthImage depthImage, EdgeImage edgeImage, List<Finger> fingers, OutputImage outputImage)
  24. {
  25. this.depthImage = depthImage;
  26. this.edgeImage = edgeImage;
  27. this.fingers = fingers;
  28. this.outputImage = outputImage;
  29. detectHands();
  30. drawHands();
  31. }
  32. private void detectHands()
  33. {
  34. int width = depthImage.Width;
  35. int height = depthImage.Height;
  36. int maxArea = width * height;
  37. Image<Gray, byte> image = edgeImage.Image.Copy().Dilate(2).Erode(2).Mul(255);
  38. HandMask = image.CopyBlank();
  39. //draw top finger slice
  40. foreach (Finger finger in fingers)
  41. {
  42. // TODO: connect contour with other edges
  43. Contour<Point> contour = finger.Contour;
  44. image.FillConvexPoly(contour.ToArray(), new Gray(0));
  45. image.DrawPolyline(finger.Contour.ToArray(), true, new Gray(255), 1);
  46. FingerSlice slice = finger.SliceTrail.EndSlice;
  47. Vector2D direction = slice.LineSegment.Line.Direction;
  48. Vector2D start = slice.Start+(Constants.FingerContourMargin+Constants.FingerSliceOverlapFactor)*direction;
  49. Vector2D end = slice.End-(Constants.FingerContourMargin+Constants.FingerSliceOverlapFactor)*direction;
  50. image.Draw(new Emgu.CV.Structure.LineSegment2D(start, end), new Gray(0), 2);
  51. //FingerSlice slice = finger.SliceTrail.Slices[1];
  52. //image.Draw(new Emgu.CV.Structure.LineSegment2D(slice.Start, slice.End), new Gray(255), 2);
  53. }
  54. Hands = new List<Hand>();
  55. foreach (Finger finger in fingers)
  56. {
  57. bool newHand = true;
  58. foreach (Hand hand in Hands)
  59. {
  60. if (hand.isInside(finger.HandPoint))
  61. {
  62. hand.addFinger(finger);
  63. newHand = false;
  64. break;
  65. }
  66. }
  67. if (newHand)
  68. {
  69. Image<Gray, byte> mask = new Image<Gray, byte>(width + 2, height + 2);
  70. MCvConnectedComp comp = new MCvConnectedComp();
  71. CvInvoke.cvFloodFill(image, finger.HandPoint, new MCvScalar(255), new MCvScalar(1), new MCvScalar(1), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, mask);
  72. if (comp.area < maxArea * Constants.HandMaxSize)
  73. {
  74. Image<Gray, byte> cropedMask = mask.Copy(new Rectangle(1, 1, width, height)).Mul(255).Dilate(1);
  75. Hand hand = new Hand(cropedMask, finger);
  76. Hands.Add(hand);
  77. HandMask = HandMask.Or(cropedMask);
  78. }
  79. }
  80. }
  81. float minX = float.MaxValue;
  82. foreach (Hand hand in Hands)
  83. {
  84. if (hand.Centroid.X < minX)
  85. minX = hand.Centroid.X;
  86. }
  87. foreach (Hand hand in Hands)
  88. {
  89. if (hand.Centroid.X == minX)
  90. hand.Side = Hand.HandSide.Left;
  91. else
  92. hand.Side = Hand.HandSide.Right;
  93. }
  94. }
  95. private void drawHands() {
  96. if (Hands.Count == 1)
  97. {
  98. outputImage.Image[0] = outputImage.Image[1] = outputImage.Image[2] = Hands[0].Mask;
  99. }
  100. else if (Hands.Count > 1)
  101. {
  102. for (int i = 0; i < 2; ++i)
  103. {
  104. if (Hands[i].Side == Hand.HandSide.Left)
  105. outputImage.Image[0] = outputImage.Image[0] | (Hands[i].Mask);
  106. else
  107. outputImage.Image[1] = outputImage.Image[1] | (Hands[i].Mask);
  108. }
  109. }
  110. }
  111. }
  112. }