Sfoglia il codice sorgente

palmDetection with fixed proportions

Alexander Hendrich 10 anni fa
parent
commit
3a85e6db53

+ 31 - 89
bbiwarg/Detectors/Palm/PalmDetector.cs

@@ -34,6 +34,12 @@ namespace bbiwarg.Detectors.Palm
         private LineSegment2DF wristLine, thumbLine;
         private Quad2D palmRect;
 
+        private bool valid = false;
+        private Vector2D topLeft;
+        private Vector2D topRight;
+        private Vector2D bottomLeft;
+        private Vector2D bottomRight;
+
         public PalmDetector(DepthImage depthImage, EdgeImage edgeImage, FingerDetector fingerDetector, PalmImage palmImage)
         {
             width = depthImage.getWidth();
@@ -54,15 +60,12 @@ namespace bbiwarg.Detectors.Palm
                 findConvexityDefactsSortedByDepth();
                 removeConvexityDefectsNearFingerTips();
 
-                findThumbLine();
-                findWristLine();
-                
-                removePointsFromContour(wristLine, 1);
-                removePointsFromContour(thumbLine, 1);
+                findHandPoints();
 
-                if (palmContour.Count<Point>() != 0)
+                if (valid)
                 {
-                    findPalmRect();
+                    removePointsFromContour(wristLine, 1);
+                    removePointsFromContour(thumbLine, 1);
 
                     draw();
                 }
@@ -168,21 +171,6 @@ namespace bbiwarg.Detectors.Palm
             }
         }
 
-        private void findWristDirection()
-        {
-            PointF[] points = new PointF[palmContour.Count<Point>()];
-            int index = 0;
-            foreach (Point p in palmContour)
-            {
-                points[index] = new PointF(p.X, p.Y);
-                ++index;
-            }
-            PointF direction, tmp;
-            PointCollection.Line2DFitting(points, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out tmp);
-            //Vector2D direction = new Vector2D(thumbLine.Direction);
-            wristDirection = new Vector2D(-direction.Y, direction.X);
-        }
-
         private void findConvexityDefactsSortedByDepth()
         {
             convexityDefects = new List<MCvConvexityDefect>(palmContour.GetConvexityDefacts(new MemStorage(), Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE));
@@ -214,34 +202,31 @@ namespace bbiwarg.Detectors.Palm
             convexityDefects = newDefects;
         }
 
-        private void findWristPoint()
-        {
-            if (convexityDefects.Count > 1)
-                wristPoint = new Vector2D(convexityDefects[1].DepthPoint);
-            else
-                wristPoint = new Vector2D(-1, -1);
-        }
-
-        private void findWristLine()
-        {
-            findWristPoint();
-            findWristDirection();
-            wristLine = new LineSegment2DF(wristPoint - 1000 * wristDirection, wristPoint + 1000 * wristDirection);
-        }
-
-        private void findThumbLine()
-        {
+        private void findHandPoints() {
             if (convexityDefects.Count > 0)
             {
                 MCvConvexityDefect thumbDefect = convexityDefects[0];
-                Vector2D p1 = new Vector2D(thumbDefect.DepthPoint);
-                Vector2D p2 = new Vector2D(thumbDefect.StartPoint);
-                Vector2D direction = (p1 - p2).normalize();
-                thumbLine = new LineSegment2DF(p1 - 1000 * direction, p1 + 1000 * direction);
+                Vector2D thumb = new Vector2D(thumbDefect.DepthPoint);
+                Vector2D thumbDefectStart = new Vector2D(thumbDefect.StartPoint);
+
+                Vector2D handLength = thumbDefectStart - thumb;
+                Vector2D handWidth = 0.8f * new Vector2D(handLength.Y, -handLength.X);
+
+                topLeft = thumbDefectStart;
+                bottomLeft = thumb - 0.3f * handLength;
+                bottomRight = bottomLeft + handWidth;
+                topRight = bottomRight + 1.5f * handLength;
+
+                wristLine = new LineSegment2DF(bottomLeft - 1000 * handWidth, bottomRight + 1000 * handWidth);
+                thumbLine = new LineSegment2DF(topLeft + 1000 * handLength, bottomLeft - 1000 * handLength);
+
+                palmRect = new Quad2D(bottomLeft, topLeft, topRight, bottomRight);
+
+                valid = true;
             }
-            else
-            {
-                thumbLine = new LineSegment2DF(new PointF(-1, -1), new PointF(-1, -1));
+            else {
+                palmRect = null;
+                valid = false;
             }
         }
 
@@ -260,49 +245,6 @@ namespace bbiwarg.Detectors.Palm
             palmContour = newContour;
         }
 
-        private void findPalmRect()
-        {
-            //palmRect = palmContour.GetMinAreaRect();
-
-            Line2D thumbLine2 = new Line2D(new Vector2D(thumbLine.P1), new Vector2D(thumbLine.P2));
-            Line2D wristLine2 = new Line2D(new Vector2D(wristLine.P1), new Vector2D(wristLine.P2));
-
-            Vector2D intersection = thumbLine2.intersection(wristLine2);
-            if (intersection != null)
-            {
-                Vector2D maxThumb = new Vector2D(0, 0), maxWrist = new Vector2D(0, 0);
-                float maxDistanceThumb = 0, maxDistanceWrist = 0;
-                foreach (Point p in palmContour)
-                {
-                    Vector2D v = new Vector2D(p);
-                    Vector2D projected = thumbLine2.projectToLine(v);
-
-                    float dist = projected.getDistanceTo(intersection);
-                    if (dist > maxDistanceThumb)
-                    {
-                        maxDistanceThumb = dist;
-                        maxThumb = projected;
-                    }
-
-                    projected = wristLine2.projectToLine(v);
-
-                    dist = projected.getDistanceTo(intersection);
-                    if (dist > maxDistanceWrist)
-                    {
-                        maxDistanceWrist = dist;
-                        maxWrist = projected;
-                    }
-                }
-
-                Vector2D origin = intersection;
-                palmRect = new Quad2D(origin, (maxThumb - origin), (maxWrist - origin));
-            }
-            else
-            {
-                palmRect = null;
-            }
-        }
-
         private void draw()
         {
             palmImage.drawContour(palmContour);

+ 1 - 0
bbiwarg/Detectors/Palm/PalmRect.cs

@@ -26,6 +26,7 @@ namespace bbiwarg.Detectors.Palm
                 return;
             }
 
+            //TODO
             origin = palmRect.Origin;
             PointF v0 = palmRect.Origin + palmRect.DirWidth;
             PointF v2 = palmRect.Origin + palmRect.DirLength;

+ 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();
 

+ 10 - 8
bbiwarg/Utility/Quad2D.cs

@@ -10,20 +10,22 @@ namespace bbiwarg.Utility
 {
     class Quad2D
     {
-        public Vector2D Origin {private set; get;}
-        public Vector2D DirLength {private set; get;}
-        public Vector2D DirWidth {private set; get;}
+        public Vector2D TopLeft {private set; get;}
+        public Vector2D TopRight { private set; get; }
+        public Vector2D BottomLeft { private set; get; }
+        public Vector2D BottomRight { private set; get; }
 
-        public Quad2D(Vector2D origin, Vector2D dirLength, Vector2D dirWidth)
+        public Quad2D(Vector2D bottomLeft, Vector2D topLeft, Vector2D topRight, Vector2D bottomRight)
         {
-            Origin = origin;
-            DirLength = dirLength;
-            DirWidth = dirWidth;
+            TopLeft = topLeft;
+            TopRight = topRight;
+            BottomLeft = bottomLeft;
+            BottomRight = BottomRight;
         }
 
         public PointF[] getVertices()
         {
-            return new PointF[] {Origin, Origin + DirLength, Origin + 0.8f * DirLength + 0.8f * DirWidth, Origin + DirWidth};
+            return new PointF[] {TopLeft,TopRight,BottomRight,BottomLeft};
         }
     }
 }