PalmDetector.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /*
  39. Vector2D handLength = 1.5f * thumbDefect.VectorLong;
  40. Vector2D handWidth = 0.45f * handLength.getOrthogonal(palmHand.Side == HandSide.Right);
  41. Vector2D fingersUpperOld = thumbDefect.Inner + 0.8f * handLength;
  42. Vector2D wristUpperOld = thumbDefect.Inner - 0.2f * handLength;
  43. Vector2D wristLowerOld = wristUpperOld + handWidth;
  44. Vector2D fingersLowerOld = wristLowerOld + 0.75f * handLength - 0.3f * handWidth;
  45. */
  46. Vector2D directionWristFinger = thumbDefect.VectorLong.normalize();
  47. Vector2D directionFingerWrist = directionWristFinger.getInverse();
  48. Vector2D directionUpperLower = directionWristFinger.getOrthogonal(palmHand.Side == HandSide.Right);
  49. Vector2D directionLowerUpper = directionUpperLower.getInverse();
  50. //handLength
  51. Vector2D handLength = 1.5f * thumbDefect.VectorLong;
  52. //fingersUpper
  53. Vector2D fingersUpper = thumbDefect.OuterLong;
  54. bool handBelow = true;
  55. while (handBelow && fingersUpper.isInBound())
  56. {
  57. fingersUpper += directionWristFinger;
  58. Vector2D below = fingersUpper.copy();
  59. bool handBelowFound = false;
  60. while (!handBelowFound && below.getDistanceTo(fingersUpper) < 2*Parameters.FingerMaxWidth && below.isInBound())
  61. {
  62. below += directionUpperLower;
  63. handBelowFound = palmHand.isInside(below);
  64. }
  65. handBelow = handBelowFound;
  66. }
  67. //wristUpper
  68. Vector2D wristUpper = thumbDefect.Inner;
  69. while (wristUpper.isInBound() && palmHand.isInside(wristUpper))
  70. wristUpper += directionFingerWrist;
  71. //handWidth
  72. Vector2D lower = thumbDefect.Inner + 10 * directionUpperLower;
  73. while (lower.isInBound() && palmHand.isInside(lower))
  74. lower += directionUpperLower;
  75. Vector2D handWidth = (lower - thumbDefect.Inner);
  76. //wristLower
  77. Vector2D wristLower = wristUpper + handWidth;
  78. //fingersLower
  79. Vector2D fingersLower = wristLower + 0.75f * handLength - 0.25f * handWidth;
  80. Palm palm = new Palm(palmHand, wristUpper, wristLower, fingersLower, fingersUpper);
  81. Palms.Add(palm);
  82. palmHand.setPalm(palm);
  83. }
  84. }
  85. }
  86. }