12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Recognition.HandRecognition;
- using bbiwarg.Utility;
- using Emgu.CV.Structure;
- using Emgu.CV;
- namespace bbiwarg.Recognition.PalmRecognition
- {
- class PalmDetector
- {
- private List<Hand> hands;
- private List<Hand> palmHands;
- public List<Palm> Palms { get; private set; }
- public PalmDetector(List<Hand> hands)
- {
- this.hands = hands;
- findPalmHands();
- createPalms();
- }
- private void findPalmHands()
- {
- palmHands = new List<Hand>();
- foreach (Hand hand in hands)
- {
- if (hand.ThumbDefect != null)
- palmHands.Add(hand);
- }
- }
- private void createPalms()
- {
- Palms = new List<Palm>();
- foreach (Hand palmHand in palmHands)
- {
- ConvexityDefect thumbDefect = palmHand.ThumbDefect;
- Vector2D handLength = 1.5f * thumbDefect.VectorLong;
- Vector2D handWidth = 0.45f * handLength.getOrthogonal(palmHand.Side == HandSide.Right);
- Vector2D fingersUpperOld = thumbDefect.Inner + 0.8f * handLength;
- Vector2D wristUpperOld = thumbDefect.Inner - 0.2f * handLength;
- Vector2D wristLowerOld = wristUpperOld + handWidth;
- Vector2D fingersLowerOld = wristLowerOld + 0.75f * handLength - 0.3f * handWidth;
- Vector2D directionWristFinger = thumbDefect.VectorLong.normalize();
- Vector2D directionFingerWrist = directionWristFinger.getInverse();
- Vector2D directionUpperLower = directionWristFinger.getOrthogonal(palmHand.Side == HandSide.Right);
- Vector2D directionLowerUpper = directionUpperLower.getInverse();
- //fingersUpper
- Vector2D fingersUpper = thumbDefect.OuterLong;
- bool handBelow = true;
- while (handBelow && fingersUpper.isInBound(Vector2D.Zero, Constants.MaxPixel))
- {
- fingersUpper += directionWristFinger;
- Vector2D below = fingersUpper.copy();
- bool handBelowFound = false;
- while (!handBelowFound && below.getDistanceTo(fingersUpper) < 2*Constants.FingerMaxWidth && below.isInBound(Vector2D.Zero, Constants.MaxPixel))
- {
- below += directionUpperLower;
- handBelowFound = palmHand.isInside(below);
- }
- handBelow = handBelowFound;
- }
- //wristUpper
- Vector2D wristUpper = thumbDefect.Inner;
- while (wristUpper.isInBound(Vector2D.Zero, Constants.MaxPixel) && palmHand.isInside(wristUpper))
- wristUpper += directionFingerWrist;
- //wristLower
- Vector2D wristLower = wristLowerOld;
- //fingersLower
- Vector2D fingersLower = fingersLowerOld;
- Palm palm = new Palm(palmHand, wristUpper, wristLower, fingersLower, fingersUpper);
- Palms.Add(palm);
- palmHand.setPalm(palm);
- }
- }
- }
- }
|