HandDetector.cs 4.4 KB

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