HandDetector.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 EdgeImage edgeImage;
  18. private List<Finger> fingers;
  19. public List<Hand> Hands { get; private set; }
  20. public Image<Gray, byte> HandMask { get; private set; }
  21. public HandDetector(EdgeImage edgeImage, List<Finger> fingers)
  22. {
  23. this.edgeImage = edgeImage;
  24. this.fingers = fingers;
  25. detectHands();
  26. }
  27. private void detectHands()
  28. {
  29. int width = edgeImage.Width;
  30. int height = edgeImage.Height;
  31. int maxArea = width * height;
  32. Image<Gray, byte> image = edgeImage.Image.Copy().Dilate(2).Erode(2).Mul(255);
  33. HandMask = image.CopyBlank();
  34. //draw top finger slice
  35. foreach (Finger finger in fingers)
  36. {
  37. Contour<Point> contour = finger.Contour;
  38. image.FillConvexPoly(contour.ToArray(), new Gray(0));
  39. image.DrawPolyline(finger.Contour.ToArray(), true, new Gray(255), 1);
  40. FingerSlice slice = finger.SliceTrail.EndSlice;
  41. Vector2D direction = slice.LineSegment.Line.Direction;
  42. Vector2D start = slice.Start+(Constants.FingerContourMargin+Constants.FingerSliceOverlapFactor)*direction;
  43. Vector2D end = slice.End-(Constants.FingerContourMargin+Constants.FingerSliceOverlapFactor)*direction;
  44. image.Draw(new Emgu.CV.Structure.LineSegment2D(start, end), new Gray(0), 2);
  45. //FingerSlice slice = finger.SliceTrail.Slices[1];
  46. //image.Draw(new Emgu.CV.Structure.LineSegment2D(slice.Start, slice.End), new Gray(255), 2);
  47. }
  48. Hands = new List<Hand>();
  49. foreach (Finger finger in fingers)
  50. {
  51. bool newHand = true;
  52. foreach (Hand hand in Hands)
  53. {
  54. if (hand.isInside(finger.HandPoint))
  55. {
  56. hand.addFinger(finger);
  57. finger.setHand(hand);
  58. newHand = false;
  59. break;
  60. }
  61. }
  62. if (newHand)
  63. {
  64. Image<Gray, byte> mask = new Image<Gray, byte>(width + 2, height + 2);
  65. MCvConnectedComp comp = new MCvConnectedComp();
  66. 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);
  67. if (comp.area < maxArea * Constants.HandMaxSize)
  68. {
  69. Image<Gray, byte> cropedMask = mask.Copy(new Rectangle(1, 1, width, height)).Dilate(1);
  70. Hand hand = new Hand(cropedMask);
  71. Hands.Add(hand);
  72. hand.addFinger(finger);
  73. finger.setHand(hand);
  74. HandMask = HandMask.Or(cropedMask);
  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. if (Hands.Count == 1 && Hands[0].Fingers.Count == 1)
  92. {
  93. if (Hands[0].Centroid.X < Hands[0].Fingers[0].HandPoint.X)
  94. Hands[0].Side = Hand.HandSide.Right;
  95. else
  96. Hands[0].Side = Hand.HandSide.Left;
  97. }
  98. }
  99. }
  100. }