Browse Source

fixed cut of finger due to twist in fingerContour

Alexander Hendrich 10 years ago
parent
commit
88ad617975

+ 10 - 56
bbiwarg/Detectors/FingerDetection/FingerDetector.cs

@@ -49,7 +49,7 @@ namespace bbiwarg.Detectors.FingerDetection
                         Vector2D edgeDirection = getEdgeDirection(edgePoint);
                         if (edgeDirection != null)
                         {
-                            Vector2D dir = edgeDirection.getOrthogonal(true);
+                            Vector2D dir = edgeDirection.getOrthogonal();
                             if (depthImage.getDepthAt(edgePoint - dir) < depthImage.getDepthAt(edgePoint + dir))
                                 dir = dir.getInverse();
 
@@ -99,7 +99,7 @@ namespace bbiwarg.Detectors.FingerDetection
                     {
                         trail.removeFirstSlices(Constants.FingerRemoveNumSlicesForCorrection);
                         trail.reverse();
-                        trail = expandTrail(trail, true);
+                        trail = expandTrail(trail);
                         trail.reverse();
                         return trail;
                     }
@@ -110,7 +110,7 @@ namespace bbiwarg.Detectors.FingerDetection
 
         }
 
-        private FingerSliceTrail expandTrail(FingerSliceTrail trail, bool reversed = false)
+        private FingerSliceTrail expandTrail(FingerSliceTrail trail)
         {
             Vector2D currentDirection = trail.getEndDirection();
             Vector2D currentPosition = trail.EndSlice.Mid + Constants.FingerStepSize * currentDirection;
@@ -123,10 +123,7 @@ namespace bbiwarg.Detectors.FingerDetection
 
             while (currentPosition.isInBound(Vector2D.Zero, depthImage.BottomRight) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
             {
-                if (reversed)
-                    nextSlice = findFingerSliceFromMid(currentPosition, currentDirection.getInverse());
-                else
-                    nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
+                nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
 
                 if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
                 {
@@ -155,66 +152,23 @@ namespace bbiwarg.Detectors.FingerDetection
             Vector2D dirStart = direction.getOrthogonal(true);
             Vector2D dirEnd = direction.getOrthogonal(false);
 
-            Vector2D start = findNextEdge(position, dirStart);
+            Vector2D start = edgeImageAdapted.findNextEdge(position, dirStart, Constants.FingerMaxSize);
             if (start == null) return null;
 
-            Vector2D end = findNextEdge(position, dirEnd);
+            Vector2D end = edgeImageAdapted.findNextEdge(position, dirEnd, Constants.FingerMaxSize);
             if (end == null) return null;
 
             return getFingerSlice(start, end);
         }
         private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
         {
-            Vector2D end = findNextEdge(start + Constants.FingerSliceOverlapFactor * direction, direction);
+            Vector2D searchStart = start + Constants.FingerSliceOverlapFactor * direction;
+            Vector2D end = edgeImageAdapted.findNextEdge(searchStart, direction, Constants.FingerMaxSize);
             if (end == null) return null;
 
             return getFingerSlice(start, end);
         }
 
-        private Vector2D findNextEdge(Vector2D start, Vector2D direction, bool adaptedEdgeImage = true, bool stopAtMaxFingerSize = true, bool returnBoundIfNoEdge = false)
-        {
-            Vector2D max = depthImage.BottomRight;
-            Vector2D maxGrow = (max - start) / direction;
-            Vector2D maxDecline = start / direction.getAbsolute();
-
-            int maxStepsX;
-            if (direction.X > 0)
-                maxStepsX = maxGrow.IntX;
-            else if (direction.X < 0)
-                maxStepsX = maxDecline.IntX;
-            else
-                maxStepsX = int.MaxValue;
-
-            int maxStepsY;
-            if (direction.Y > 0)
-                maxStepsY = maxGrow.IntY;
-            else if (direction.Y < 0)
-                maxStepsY = maxDecline.IntY;
-            else
-                maxStepsY = int.MaxValue;
-
-            int maxSteps = Math.Min(maxStepsX, maxStepsY);
-
-            if (stopAtMaxFingerSize)
-                maxSteps = Math.Min(maxSteps, (int)(Constants.FingerMaxSize / direction.Length));
-
-            Vector2D end = new Vector2D(start);
-            for (int i = 0; i < maxSteps; i++)
-            {
-                end += direction;
-
-                if ((adaptedEdgeImage && edgeImageAdapted.isRoughEdgeAt(end)) || (!adaptedEdgeImage && edgeImageOriginal.isRoughEdgeAt(end)))
-                {
-                    return end;
-                }
-            }
-
-            if (returnBoundIfNoEdge)
-                return end;
-            else
-                return null;
-        }
-
         private FingerSlice getFingerSlice(Vector2D start, Vector2D end)
         {
             int maxX = depthImage.Width - 1;
@@ -284,8 +238,8 @@ namespace bbiwarg.Detectors.FingerDetection
             Vector2D dirOrth2 = direction.getOrthogonal(false);
 
             Vector2D outPoint = (start + Constants.FingerOutSliceFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction.getInverse());
-            Vector2D p1 = findNextEdge(outPoint, dirOrth1, false, false, true);
-            Vector2D p2 = findNextEdge(outPoint, dirOrth2, false, false, true);
+            Vector2D p1 = edgeImageOriginal.findNextEdge(outPoint, dirOrth1, 0, true, false);
+            Vector2D p2 = edgeImageOriginal.findNextEdge(outPoint, dirOrth2, 0, true, false);
 
             FingerSlice slice = new FingerSlice(p1, p2);
 

+ 9 - 1
bbiwarg/Detectors/FingerDetection/FingerSliceTrail.cs

@@ -50,8 +50,11 @@ namespace bbiwarg.Detectors.FingerDetection
             return (EndSlice.Mid - slices[innerEndIndex].Mid).normalize();
         }
 
-        public void removeFirstSlices(int numSlices) {
+        public void removeFirstSlices(int numSlices)
+        {
             slices.RemoveRange(0, numSlices);
+            pointsA.RemoveRange(0, numSlices);
+            pointsB.RemoveRange(0, numSlices);
             LineSegment = new LineSegment2D(StartSlice.Mid, EndSlice.Mid);
         }
 
@@ -59,6 +62,11 @@ namespace bbiwarg.Detectors.FingerDetection
             slices.Reverse();
             pointsA.Reverse();
             pointsB.Reverse();
+
+            List<Point> dummy = pointsA;
+            pointsA = pointsB;
+            pointsB = dummy;
+
             LineSegment = new LineSegment2D(StartSlice.Mid, EndSlice.Mid);
         }
 

+ 44 - 0
bbiwarg/Images/EdgeImage.cs

@@ -57,6 +57,50 @@ namespace bbiwarg.Images
             RoughImage.FillConvexPoly(polygon, new Gray(0));
         }
 
+        public Vector2D findNextEdge(Vector2D start, Vector2D direction, int maxSearchSize = 0, bool roughEdge=true, bool returnNullIfNoEdgeFound=true)
+        {
+            Vector2D max = new Vector2D(Image.Width-1, Image.Height-1);
+            Vector2D maxGrow = (max - start) / direction;
+            Vector2D maxDecline = start / direction.getAbsolute();
+
+            int maxStepsX;
+            if (direction.X > 0)
+                maxStepsX = maxGrow.IntX;
+            else if (direction.X < 0)
+                maxStepsX = maxDecline.IntX;
+            else
+                maxStepsX = int.MaxValue;
+
+            int maxStepsY;
+            if (direction.Y > 0)
+                maxStepsY = maxGrow.IntY;
+            else if (direction.Y < 0)
+                maxStepsY = maxDecline.IntY;
+            else
+                maxStepsY = int.MaxValue;
+
+            int maxSteps = Math.Min(maxStepsX, maxStepsY);
+
+            if (maxSearchSize != 0)
+                maxSteps = Math.Min(maxSteps, maxSearchSize);
+
+            Vector2D end = new Vector2D(start);
+            for (int i = 0; i < maxSteps; i++)
+            {
+                end += direction;
+
+                if((roughEdge && isRoughEdgeAt(end)) || (!roughEdge && isEdgeAt(end)))
+                {
+                    return end;
+                }
+            }
+
+            if (returnNullIfNoEdgeFound)
+                return null;
+            else
+                return end;
+        }
+
         public EdgeImage copy()
         {
             return new EdgeImage(Image.Copy());

+ 1 - 1
bbiwarg/Utility/Vector2D.cs

@@ -92,7 +92,7 @@ namespace bbiwarg.Utility
             return new Vector2D(X / length, Y / length);
         }
 
-        public Vector2D getOrthogonal(bool side)
+        public Vector2D getOrthogonal(bool side=true)
         {
             if (side)
                 return new Vector2D(Y, -X);