HandDetector.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. finger.setHand(hand);
  64. newHand = false;
  65. break;
  66. }
  67. }
  68. if (newHand)
  69. {
  70. Image<Gray, byte> mask = new Image<Gray, byte>(width + 2, height + 2);
  71. MCvConnectedComp comp = new MCvConnectedComp();
  72. 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);
  73. if (comp.area < maxArea * Constants.HandMaxSize)
  74. {
  75. Image<Gray, byte> cropedMask = mask.Copy(new Rectangle(1, 1, width, height)).Mul(255).Dilate(1);
  76. Hand hand = new Hand(cropedMask);
  77. Hands.Add(hand);
  78. hand.addFinger(finger);
  79. finger.setHand(hand);
  80. HandMask = HandMask.Or(cropedMask);
  81. }
  82. }
  83. }
  84. float minX = float.MaxValue;
  85. foreach (Hand hand in Hands)
  86. {
  87. if (hand.Centroid.X < minX)
  88. minX = hand.Centroid.X;
  89. }
  90. foreach (Hand hand in Hands)
  91. {
  92. if (hand.Centroid.X == minX)
  93. hand.Side = Hand.HandSide.Left;
  94. else
  95. hand.Side = Hand.HandSide.Right;
  96. }
  97. if (Hands.Count == 1 && Hands[0].Fingers.Count == 1)
  98. {
  99. if (Hands[0].Centroid.X < Hands[0].Fingers[0].HandPoint.X)
  100. Hands[0].Side = Hand.HandSide.Right;
  101. else
  102. Hands[0].Side = Hand.HandSide.Left;
  103. }
  104. }
  105. private void drawHands() {
  106. if (Hands.Count == 1)
  107. {
  108. outputImage.Image[0] = outputImage.Image[1] = outputImage.Image[2] = Hands[0].Mask;
  109. }
  110. else if (Hands.Count > 1)
  111. {
  112. for (int i = 0; i < 2; ++i)
  113. {
  114. if (Hands[i].Side == Hand.HandSide.Left)
  115. outputImage.Image[0] = outputImage.Image[0] | (Hands[i].Mask);
  116. else
  117. outputImage.Image[1] = outputImage.Image[1] | (Hands[i].Mask);
  118. }
  119. }
  120. }
  121. }
  122. }