Pārlūkot izejas kodu

Removed rect around fingers for contour detection.

Daniel Kauth 11 gadi atpakaļ
vecāks
revīzija
7ed272cd31

+ 6 - 0
bbiwarg/Detectors/Fingers/Finger.cs

@@ -53,6 +53,12 @@ namespace bbiwarg.Detectors.Fingers
             return tipPoint;
         }
 
+        public Vector<int> getHandPoint()
+        {
+            if (!lineUpToDate) updateLine();
+            return handPoint;
+        }
+
         public float getLength()
         {
             if (!lineUpToDate) updateLine();

+ 44 - 15
bbiwarg/Images/PalmImage.cs

@@ -8,38 +8,66 @@ using Emgu.CV;
 using Emgu.CV.Structure;
 using System.Drawing;
 
+using bbiwarg.Detectors.Fingers;
+using bbiwarg.Utility;
+
 namespace bbiwarg.Images
 {
     class PalmImage
     {
         private Image<Gray, Byte> image;
         private int width, height;
-        private Contour<Point> contour;
         bool[,] inContour;
-
-        public PalmImage(EdgeImage edgeImage)
+        
+        public PalmImage(EdgeImage edgeImage, FingerTracker fingerTracker)
         {
-            image = edgeImage.getImage().Clone();
+            image = edgeImage.getImage(); //.Clone();
             width = image.Width;
             height = image.Height;
+            
+            foreach (Finger f in fingerTracker.getFingers())
+            {
+                Vector<float> tip = new Vector<float>(new float[] { f.getTipPoint().x, f.getTipPoint().y });
+                Vector<float> hand = new Vector<float>(new float[] { f.getHandPoint().x, f.getHandPoint().y });
 
-            findContours();
 
+                for (int x = 0; x < width; ++x)
+                {
+                    for (int y = 0; y < height; ++y)
+                    {
+                        if (image.Data[y, x, 0] != 0)
+                        {
+                           Vector<float> p = new Vector<float>(new float[] { x, y });
 
-            
+                            float l2 = (tip - hand) * (tip - hand);
+                            float t = (p - tip) * (hand - tip) / l2;
+                            if (t >= 0.0 && t <= 1.0)
+                            {
+                                Vector<float> tmp = hand - tip;
+                                tmp.multiply(t);
+                                Vector<float> projection = tip + tmp;
+
+                                if (p.distance(projection) <= 15)
+                                    image.Data[y, x, 0] = 0;
+                            }
+                        }
+                    }
+                }
+            }
+
+            findContour();
         }
 
-        private void findContours()
+        private void findContour()
         {
-            image = image.Dilate(3);
-            contour = image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL);
+            //image = image.Dilate(1);
+            Contour<Point> contour = image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,
+                                                        Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);
 
             Contour<Point> maxContour = contour;
             double maxPerimeter = 0;
-            int count = 0;
             while (contour != null)
             {
-                ++count;
                 if (contour.Perimeter > maxPerimeter)
                 {
                     maxPerimeter = contour.Perimeter;
@@ -48,11 +76,12 @@ namespace bbiwarg.Images
                 contour = contour.HNext;
             }
 
-            Console.WriteLine("numContours: " + count + "\nmaxPerimeter = " + maxPerimeter);
-
             inContour = new bool[width, height];
-            foreach (Point p in maxContour)
-                inContour[p.X, p.Y] = true;
+            if (maxContour != null)
+            {
+                foreach (Point p in maxContour)
+                    inContour[p.X, p.Y] = true;
+            }
         }
 
         public bool belongsToPalm(int x, int y)

+ 1 - 1
bbiwarg/MainBBWIWARG.cs

@@ -12,7 +12,7 @@ namespace bbiwarg
     {
         static void Main(string[] args)
         {
-            IInputProvider inputProvider = new IisuInputProvider();//"..\\..\\videos\\touch\\4.skv");
+            IInputProvider inputProvider = new IisuInputProvider("..\\..\\videos\\touch\\4.skv");
             VideoHandle videoHandle = new VideoHandle(inputProvider);
             videoHandle.start();
 

+ 1 - 1
bbiwarg/VideoHandle.cs

@@ -129,7 +129,7 @@ namespace bbiwarg
             //touchDetector = new TouchDetector(fingerDetector.getFingers(), depthImage, touchImage);
             touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents(), touchImage);
 
-            palmImage = new PalmImage(edgeImage);
+            palmImage = new PalmImage(edgeImage, fingerTracker);
         }
     }
 }