浏览代码

added finger contour

Alexander Hendrich 11 年之前
父节点
当前提交
95d2c894d7

+ 1 - 0
bbiwarg/Constants.cs

@@ -57,6 +57,7 @@ namespace bbiwarg
         public static readonly int FingerNumSlicesForRelativeDirection = 5;
         public static readonly int FingerNumFramesUntilTracked = 2;
         public static readonly int FingerOutSliceFactor = 5;
+        public static readonly int FingerContourMargin = 3;
         public static readonly float FingerMinSimilarityForTracking = 0.75f;
 
         // hand detection

+ 11 - 6
bbiwarg/Detectors/FingerDetection/Finger.cs

@@ -6,6 +6,7 @@ using System.Text;
 using System.Threading.Tasks;
 using bbiwarg.Images;
 using bbiwarg.Utility;
+using Emgu.CV;
 
 namespace bbiwarg.Detectors.FingerDetection
 {
@@ -25,22 +26,26 @@ namespace bbiwarg.Detectors.FingerDetection
             SliceTrail = sliceTrail;
         }
 
-        public Point[] getBoundingPolygon()
+        public Contour<Point> getContour()
         {
             List<Point> pointsA = new List<Point>();
             List<Point> pointsB = new List<Point>();
-            //int numSlices = SliceTrail.NumSlices;
-            //for (int i = 0; i < numSlices; i++)
             foreach (FingerSlice slice in SliceTrail.Slices)
             {
                 Vector2D direction = (slice.End - slice.Start).normalize();
-                pointsA.Add(slice.Start-2*direction);
-                pointsB.Add(slice.End+2*direction);
+                pointsA.Add(slice.Start - Constants.FingerContourMargin * direction);
+                pointsB.Add(slice.End + Constants.FingerContourMargin * direction);
             }
 
             pointsB.Reverse();
             pointsA.AddRange(pointsB);
-            return pointsA.ToArray();
+
+            Contour<Point> contour = new Contour<Point>(new MemStorage());
+            foreach (Point p in pointsA)
+            {
+                contour.Push(p);
+            }
+            return contour;
         }
 
         //TODO: redo (not use similarity but actual distances and angle instead)

+ 9 - 5
bbiwarg/Detectors/FingerDetection/FingerDetector.cs

@@ -99,7 +99,7 @@ namespace bbiwarg.Detectors.FingerDetection
                     {
                         trail.Slices.RemoveRange(0, Constants.FingerRemoveNumSlicesForCorrection);
                         trail.Slices.Reverse();
-                        trail = expandTrail(trail);
+                        trail = expandTrail(trail, true);
                         trail.Slices.Reverse();
                         return trail;
                     }
@@ -110,7 +110,7 @@ namespace bbiwarg.Detectors.FingerDetection
 
         }
 
-        private FingerSliceTrail expandTrail(FingerSliceTrail trail)
+        private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
         {
             int maxX = depthImage.Width - 1;
             int maxY = depthImage.Height - 1;
@@ -126,7 +126,10 @@ namespace bbiwarg.Detectors.FingerDetection
 
             while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
             {
-                nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
+                if(reversed)
+                    nextSlice = findFingerSliceFromMid(currentPosition, currentDirection.getInverse());
+                else
+                    nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
 
                 if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
                 {
@@ -253,8 +256,7 @@ namespace bbiwarg.Detectors.FingerDetection
             drawDetectedFinger(finger);
 
             //remove edges around detected finger to improve performance
-            Point[] polygon = finger.getBoundingPolygon();
-            edgeImageAdapted.removeEdgesInsidePolygon(polygon);
+            edgeImageAdapted.removeEdgesInsidePolygon(finger.getContour().ToArray());
         }
 
         private FingerSlice findOutSlice(Vector2D start, Vector2D direction)
@@ -311,7 +313,9 @@ namespace bbiwarg.Detectors.FingerDetection
             {
                 outputImage.drawLineSegment(trail.Slices[i].LineSegment, Constants.FingerSliceColor);
             }
+            //outputImage.drawPolygon(finger.getBoundingPolygon(), 0);
             outputImage.drawLineSegment(finger.LineSegment, Constants.FingerDetectedColor);
+            outputImage.drawContour(finger.getContour(), Color.Red, 1);
         }
     }
 }

+ 5 - 4
bbiwarg/Graphics/OutputImage.cs

@@ -20,14 +20,15 @@ namespace bbiwarg.Graphics
             Image = new Image<Rgb, byte>(width, height);
         }
 
-        public Color getColotAt(int x, int y) {
-            
+        public Color getColotAt(int x, int y)
+        {
+
             byte red = Image.Data[y, x, 0];
             byte green = Image.Data[y, x, 1];
             byte blue = Image.Data[y, x, 2];
-            return Color.FromArgb(red,green,blue);
+            return Color.FromArgb(red, green, blue);
         }
-        
+
         public void drawLineSegment(bbiwarg.Utility.LineSegment2D lineSegment, Color color, int thickness = 1)
         {
             Image.Draw(new LineSegment2D(lineSegment.P1, lineSegment.P2), new Rgb(color), thickness);

+ 0 - 4
bbiwarg/Images/EdgeImage.cs

@@ -60,9 +60,5 @@ namespace bbiwarg.Images
         {
             return new EdgeImage(Image.Copy());
         }
-
-        public EdgeImage getRoughEdgeImage(int numDilateIterations) {
-            return new EdgeImage(Image.Dilate(numDilateIterations));
-        }
     }
 }