123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Images;
- using bbiwarg.Recognition.FingerRecognition;
- using bbiwarg.Output;
- using bbiwarg.Utility;
- using bbiwarg.Input.InputHandling;
- using Emgu.CV;
- using Emgu.CV.Structure;
- namespace bbiwarg.Recognition.HandRecognition
- {
- /// <summary>
- /// Finds Hands by iterating over all fingers and flooding them. Each filled region is considered to be one hand and each finger belongs to one hand. To improve the hand contours, the hand mask are filled with the defects caused by overlapping fingers.
- /// </summary>
- class HandDetector
- {
- /// <summary>
- /// the depth image of the current frame
- /// </summary>
- private DepthImage depthImage;
- /// <summary>
- /// the modified depth image of the current frame (hand flooding changes depth image)
- /// </summary>
- private Image<Gray, byte> modifiedHandDepthImage;
- /// <summary>
- /// the fingers in the current frame
- /// </summary>
- private List<Finger> fingers;
- /// <summary>
- /// a mapping of hands and list of fingers, that don't belong to that specific hand
- /// </summary>
- private Dictionary<Hand, List<Finger>> otherHandsFingers;
- /// <summary>
- /// the hands in the current frame
- /// </summary>
- private List<Hand> hands;
- /// <summary>
- /// Detects Hands in the current frame and stores found hands in frameData.detectedHands
- /// </summary>
- /// <param name="frameData">the current frame</param>
- public void detectHands(FrameData frameData)
- {
- depthImage = frameData.DepthImage;
- fingers = frameData.TrackedFingers;
- createModifiedHandEdgeImage();
- findHands();
- fixOverlappingFingers();
- findCentroids();
- frameData.DetectedHands = hands;
- }
- /// <summary>
- /// Creates the modified hand image. The image is a copy of the original depth image with a contour around each finger (to prevent floodfill from filling through fingers).
- /// </summary>
- private void createModifiedHandEdgeImage()
- {
- modifiedHandDepthImage = depthImage.Image.Copy();
- foreach (Finger finger in fingers)
- {
- Int16 depthAtHand = depthImage.getDepthAt(finger.HandPoint);
- Point[] contour = finger.getContour(0f).ToArray();
- modifiedHandDepthImage.DrawPolyline(contour, false, new Gray(depthAtHand), 1);
- }
- }
- /// <summary>
- /// Finds hands by flood filling from each finger (mask). All unassigned fingers that lie within the hand are assigned to the hand, all other fingers are mapped as fingers that don't belong to that specific hand.
- /// </summary>
- private void findHands()
- {
- hands = new List<Hand>();
- otherHandsFingers = new Dictionary<Hand,List<Finger>>();
- List<Finger> assignedFingers = new List<Finger>();
- foreach (Finger finger in fingers)
- {
- if(!assignedFingers.Contains(finger)) {
- Image<Gray, byte> handMask = getHandMask(finger.HandPoint);
- int numPixels = handMask.CountNonzero()[0];
- if (numPixels > Parameters.HandMaxSize * depthImage.Size.NumPixels)
- {
- assignedFingers.Add(finger);
- break;
- }
- List<Finger> fingersOnHand = new List<Finger>();
- List<Finger> fingersOnOtherHand = new List<Finger>();
- foreach (Finger f in fingers) {
- if (!assignedFingers.Contains(f) && handMask.Data[f.HandPoint.IntY, f.HandPoint.IntX, 0] != 0)
- {
- fingersOnHand.Add(f);
- assignedFingers.Add(f);
- }
- else
- fingersOnOtherHand.Add(f);
- }
- Hand hand = new Hand(handMask, fingersOnHand);
- otherHandsFingers.Add(hand, fingersOnOtherHand);
- hands.Add(hand);
- }
- }
- }
- /// <summary>
- /// Flood fills from a given point and returns the filled area as mask.
- /// </summary>
- /// <param name="p">flood fill starting point</param>
- /// <returns>the filled area as mask</returns>
- private Image<Gray, byte> getHandMask(Vector2D p)
- {
- Image<Gray, byte> mask = new Image<Gray, byte>(depthImage.Size.Width + 2, depthImage.Size.Height + 2);
- MCvConnectedComp comp = new MCvConnectedComp();
- CvInvoke.cvFloodFill(modifiedHandDepthImage, p, new MCvScalar(255), new MCvScalar(Parameters.HandFloodFillDownDiff),
- new MCvScalar(Parameters.HandFloodFillUpDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, mask);
- return mask.Copy(new Rectangle(1, 1, depthImage.Size.Width, depthImage.Size.Height));
- }
- /// <summary>
- /// Fixes overlapping fingers by merging two hands if they are seperated by a finger and/or fills holes caused by overlapping fingers.
- /// </summary>
- private void fixOverlappingFingers()
- {
- extendOrMergeThroughOverlappingFingers();
- fillOverlappingFingers();
- }
- /// <summary>
- /// Merges two hands if they are seperatd by an overlapping finger or extends the hand mask through an overlapping finger.
- /// </summary>
- private void extendOrMergeThroughOverlappingFingers()
- {
- List<Hand> mergedHands = new List<Hand>();
- foreach (Hand hand in hands)
- {
- if (!mergedHands.Contains(hand))
- {
- List<Hand> mergeHands = new List<Hand>();
- foreach (Finger overlappingFinger in otherHandsFingers[hand])
- {
- FingerSlice midSlice = overlappingFinger.SliceTrail.MidSlice;
- Vector2D midOut1 = midSlice.Start.moveWithinBound(depthImage.Size, midSlice.Direction.getInverse(), Parameters.FingerOutMargin);
- Vector2D midOut2 = midSlice.End.moveWithinBound(depthImage.Size, midSlice.Direction, Parameters.FingerOutMargin);
- Int16 depthAtMidOut1 = depthImage.getDepthAt(midOut1);
- Int16 depthAtMidOut2 = depthImage.getDepthAt(midOut2);
- bool midOut1InHand = hand.isInside(midOut1);
- bool midOut2InHand = hand.isInside(midOut2);
- Int16 maxDepth = depthImage.MaxDepth;
- if (midOut1InHand != midOut2InHand && depthAtMidOut1 != maxDepth && depthAtMidOut2 != maxDepth && Math.Abs(depthAtMidOut1 - depthAtMidOut2) < Parameters.HandExtendMaxDifference)
- {
- Vector2D pHand, pHandExtension;
- if (midOut1InHand)
- {
- pHand = midOut1;
- pHandExtension = midOut2;
- }
- else
- {
- pHand = midOut2;
- pHandExtension = midOut1;
- }
- //check if pHandExtension is in other hand (if so -> merge with hand)
- bool merge = false;
- foreach (Hand mergeHand in hands)
- {
- if (mergeHand.isInside(pHandExtension) && !mergedHands.Contains(mergeHand))
- {
- mergeHands.Add(mergeHand);
- merge = true;
- break;
- }
- }
- //if no merge, extend hand
- if (!merge)
- extendToHand(hand, pHandExtension);
- }
- }
- foreach (Hand mergeHand in mergeHands)
- {
- mergeToHand(hand, mergeHand);
- mergedHands.Add(mergeHand);
- }
- }
- }
- foreach (Hand mergedHand in mergedHands)
- hands.Remove(mergedHand);
- }
- /// <summary>
- /// Merges two hands together and updates the list of other hands fingers.
- /// </summary>
- /// <param name="hand">the first hand (other hand will be merged to this one)</param>
- /// <param name="mergeHand">the second hand (this hand will be dropped afterwards)</param>
- private void mergeToHand(Hand hand, Hand mergeHand)
- {
- hand.mergeWith(mergeHand);
- foreach (Finger finger in mergeHand.Fingers)
- otherHandsFingers[hand].Remove(finger);
- otherHandsFingers.Remove(mergeHand);
- }
- /// <summary>
- /// Extends the hand mask of a given hand by flood filling starting from the given point.
- /// </summary>
- /// <param name="hand">the hand that should be extended</param>
- /// <param name="p">the flood fill starting point</param>
- private void extendToHand(Hand hand, Vector2D p)
- {
- Image<Gray, byte> extendMask = getHandMask(p);
- int numPixels = extendMask.CountNonzero()[0];
- if (numPixels <= Parameters.HandExtensionMaxRelativeSize * depthImage.Size.NumPixels)
- hand.extendMask(extendMask);
- }
- /// <summary>
- /// Fills holes caused by overlapping fingers.
- /// </summary>
- private void fillOverlappingFingers()
- {
- foreach (Hand hand in hands)
- {
- hand.fillOverlappingFingers(otherHandsFingers[hand]);
- }
- }
- /// <summary>
- /// Finds the hands centroids.
- /// </summary>
- private void findCentroids() {
- foreach (Hand hand in hands)
- hand.findCentroid();
- }
- }
- }
|