PalmDetector.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Recognition.HandRecognition;
  7. using bbiwarg.Utility;
  8. using Emgu.CV.Structure;
  9. using Emgu.CV;
  10. namespace bbiwarg.Recognition.PalmRecognition
  11. {
  12. class PalmDetector
  13. {
  14. private List<Hand> hands;
  15. private List<Hand> palmHands;
  16. public List<Palm> Palms { get; private set; }
  17. public PalmDetector(List<Hand> hands)
  18. {
  19. this.hands = hands;
  20. findPalmHands();
  21. createPalms();
  22. }
  23. private void findPalmHands()
  24. {
  25. palmHands = new List<Hand>();
  26. foreach (Hand hand in hands)
  27. {
  28. if (hand.ThumbDefect != null)
  29. palmHands.Add(hand);
  30. }
  31. }
  32. private void createPalms()
  33. {
  34. Palms = new List<Palm>();
  35. foreach (Hand palmHand in palmHands)
  36. {
  37. ConvexityDefect thumbDefect = palmHand.ThumbDefect;
  38. Vector2D handLength = 1.5f * thumbDefect.VectorLong;
  39. Vector2D handWidth = 0.45f * handLength.getOrthogonal(palmHand.Side == HandSide.Right);
  40. Vector2D fingersUpperOld = thumbDefect.Inner + 0.8f * handLength;
  41. Vector2D wristUpperOld = thumbDefect.Inner - 0.2f * handLength;
  42. Vector2D wristLowerOld = wristUpperOld + handWidth;
  43. Vector2D fingersLowerOld = wristLowerOld + 0.75f * handLength - 0.3f * handWidth;
  44. Vector2D directionWristFinger = thumbDefect.VectorLong.normalize();
  45. Vector2D directionFingerWrist = directionWristFinger.getInverse();
  46. Vector2D directionUpperLower = directionWristFinger.getOrthogonal(palmHand.Side == HandSide.Right);
  47. Vector2D directionLowerUpper = directionUpperLower.getInverse();
  48. //fingersUpper
  49. Vector2D fingersUpper = thumbDefect.OuterLong;
  50. bool handBelow = true;
  51. while (handBelow && fingersUpper.isInBound(Vector2D.Zero, Constants.MaxPixel))
  52. {
  53. fingersUpper += directionWristFinger;
  54. Vector2D below = fingersUpper.copy();
  55. bool handBelowFound = false;
  56. while (!handBelowFound && below.getDistanceTo(fingersUpper) < 2*Constants.FingerMaxWidth && below.isInBound(Vector2D.Zero, Constants.MaxPixel))
  57. {
  58. below += directionUpperLower;
  59. handBelowFound = palmHand.isInside(below);
  60. }
  61. handBelow = handBelowFound;
  62. }
  63. //wristUpper
  64. Vector2D wristUpper = thumbDefect.Inner;
  65. while (wristUpper.isInBound(Vector2D.Zero, Constants.MaxPixel) && palmHand.isInside(wristUpper))
  66. wristUpper += directionFingerWrist;
  67. //wristLower
  68. Vector2D wristLower = wristLowerOld;
  69. //fingersLower
  70. Vector2D fingersLower = fingersLowerOld;
  71. Palm palm = new Palm(palmHand, wristUpper, wristLower, fingersLower, fingersUpper);
  72. Palms.Add(palm);
  73. palmHand.setPalm(palm);
  74. }
  75. }
  76. }
  77. }