123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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.Graphics;
- using bbiwarg.Utility;
- using Emgu.CV;
- using Emgu.CV.Structure;
- namespace bbiwarg.Recognition.HandRecognition
- {
- class HandDetector
- {
- private DepthImage depthImage;
- private EdgeImage edgeImage;
- private Image<Gray, byte> modifiedHandDepthImage;
- private List<Finger> fingers;
- public List<Hand> Hands { get; private set; }
- public Image<Gray, byte> HandMask { get; private set; }
- public HandDetector(DepthImage depthImage, EdgeImage edgeImage, List<Finger> fingers)
- {
- this.depthImage = depthImage;
- this.edgeImage = edgeImage;
- this.fingers = fingers;
- createModifiedHandEdgeImage();
- findHands();
- setZIndexes();
- createHandMask();
- findThumbDefects();
- }
- private void createModifiedHandEdgeImage()
- {
- modifiedHandDepthImage = depthImage.Image.Copy();
- foreach (Finger finger in fingers)
- {
- Point[] contour = finger.InnerContour.ToArray();
- modifiedHandDepthImage.DrawPolyline(contour, false, new Gray(0), 2);
- }
- }
- private void findHands()
- {
- int width = edgeImage.Width;
- int height = edgeImage.Height;
- int maxArea = width * height;
- Hands = new List<Hand>();
- foreach (Finger finger in fingers)
- {
- bool newHand = true;
- foreach (Hand hand in Hands)
- {
- if (hand.isInside(finger.HandPoint))
- {
- hand.addFinger(finger);
- finger.setHand(hand);
- newHand = false;
- break;
- }
- }
- if (newHand)
- {
- Image<Gray, byte> mask = new Image<Gray, byte>(width + 2, height + 2);
- MCvConnectedComp comp = new MCvConnectedComp();
- CvInvoke.cvFloodFill(modifiedHandDepthImage, finger.HandPoint, new MCvScalar(255), new MCvScalar(Constants.HandFloodFillDownDiff), new MCvScalar(Constants.HandFloodFillUpDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, mask);
- if (comp.area < maxArea * Constants.HandMaxSize)
- {
- Image<Gray, byte> cropedMask = mask.Copy(new Rectangle(1, 1, width, height));
- Hand hand = new Hand(cropedMask, finger);
- Hands.Add(hand);
- finger.setHand(hand);
- }
- }
- }
- }
- private void setZIndexes()
- {
- //sort depending on depth of centroid (far->near)
- Hands.Sort((h1, h2) => depthImage.getDepthAt(h2.CentroidInHand).CompareTo(depthImage.getDepthAt(h1.CentroidInHand)));
- for (int i = 0; i < Hands.Count; i++)
- Hands[i].setZIndex(i);
- }
- private void createHandMask()
- {
- HandMask = new Image<Gray, byte>(depthImage.Width, depthImage.Height);
- foreach (Hand hand in Hands)
- HandMask = HandMask.Or(hand.Mask);
- }
- private void findThumbDefects()
- {
- foreach (Hand hand in Hands)
- hand.findThumbDefect();
- }
- }
- }
|