Selaa lähdekoodia

moved fingerDetection constants to constants class

Alexander Hendrich 11 vuotta sitten
vanhempi
commit
1337b15a31
2 muutettua tiedostoa jossa 24 lisäystä ja 27 poistoa
  1. 9 0
      bbiwarg/Constants.cs
  2. 15 27
      bbiwarg/Detectors/Fingers/FingerDetector.cs

+ 9 - 0
bbiwarg/Constants.cs

@@ -35,6 +35,15 @@ namespace bbiwarg
         public static readonly Color PalmConvexHullColor = Color.Green;
         public static readonly Color PalmThumbDefectColor = Color.Lime;
 
+        // finger detection
+        public static readonly int FingerStepSize = 2;
+        public static readonly int FingerMinNumSlices = 7;
+        public static readonly int FingerRemoveNumSlicesForCorrection = 3;
+        public static readonly int FingerMaxGapCounter = 10;
+        public static readonly int FingerMaxSliceDifferencePerStep = 5;
+        public static readonly int FingerMaxSize = 35;
+        public static readonly int FingerMinSize = 5;
+
         // palm detection
         public static readonly float PalmMinDefectMidFingerLineDistance = 20; // defects with mid point ((start + end) / 2) closer than this to a finger line are removed 
         public static readonly float PalmMaxThumbDefectAngle = 100; // degree

+ 15 - 27
bbiwarg/Detectors/Fingers/FingerDetector.cs

@@ -31,15 +31,12 @@ namespace bbiwarg.Detectors.Fingers
 
         private void findFingers()
         {
-            int minNumSlices = 7;
-            int width = depthImage.Width;
-            int height = depthImage.Height;
-            int maxX = width - 1;
-            int maxY = height - 1;
+            int maxX = depthImage.Width - 1;
+            int maxY = depthImage.Height - 1;
 
             Fingers = new List<Finger>();
 
-            for (int y = 1; y < maxY; y+=5) //y++ for 100% coverage, but y++ for 99% coverage and 3 times better perfomance
+            for (int y = 1; y < maxY; y += 5) //y++ for 100% coverage, but y++ for 99% coverage and 3 times better perfomance
             {
                 for (int x = 1; x < maxX; x++)
                 {
@@ -57,7 +54,7 @@ namespace bbiwarg.Detectors.Fingers
                             if (slice != null)
                             {
                                 FingerSliceTrail trail = findFingerSliceTrail(slice, edgeDirection);
-                                if (trail != null && trail.NumSlices > minNumSlices)
+                                if (trail != null && trail.NumSlices > Constants.FingerMinNumSlices)
                                     createFingerFromTrail(trail);
                             }
                         }
@@ -80,16 +77,13 @@ namespace bbiwarg.Detectors.Fingers
 
         private FingerSliceTrail findFingerSliceTrail(FingerSlice startSlice, Vector2D startDirection)
         {
-            int minNumSlicesForCorrection = 7;
-            int numRemoveForCorrection = 3;
-
             int maxX = depthImage.Width - 1;
             int maxY = depthImage.Height - 1;
 
             FingerSliceTrail trail = new FingerSliceTrail(startSlice);
 
             Vector2D direction = startDirection;
-            Vector2D position = startSlice.Mid + 2*direction;
+            Vector2D position = startSlice.Mid + Constants.FingerStepSize * direction;
 
             if (position.isWithin(0, 0, maxX, maxY))
             {
@@ -99,9 +93,9 @@ namespace bbiwarg.Detectors.Fingers
                     trail.addSlice(nextSlice);
                     trail = expandTrail(trail);
 
-                    if (trail.NumSlices > minNumSlicesForCorrection)
+                    if (trail.NumSlices > Constants.FingerMinNumSlices)
                     {
-                        trail.Slices.RemoveRange(0, numRemoveForCorrection);
+                        trail.Slices.RemoveRange(0, Constants.FingerRemoveNumSlicesForCorrection);
                         trail.Slices.Reverse();
                         trail = expandTrail(trail);
                         trail.Slices.Reverse();
@@ -120,7 +114,7 @@ namespace bbiwarg.Detectors.Fingers
             int maxY = depthImage.Height - 1;
 
             Vector2D currentDirection = trail.getEndDirection();
-            Vector2D currentPosition = trail.End.Mid + 2*currentDirection;
+            Vector2D currentPosition = trail.End.Mid + Constants.FingerStepSize * currentDirection;
 
             int gapCounter = 0;
             int numSlices = trail.NumSlices;
@@ -128,16 +122,16 @@ namespace bbiwarg.Detectors.Fingers
             FingerSlice lastSlice = trail.End;
             FingerSlice nextSlice;
 
-            while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, 10))
+            while (currentPosition.isWithin(0, 0, maxX, maxY) && gapCounter <= Math.Min(numSlices, Constants.FingerMaxGapCounter))
             {
                 nextSlice = findFingerSliceFromMid(currentPosition, currentDirection);
-                if (nextSlice != null && (nextSlice.Length < lastSlice.Length + 5 && nextSlice.Length > lastSlice.Length - 5))
+                if (nextSlice != null && Math.Abs(nextSlice.Length - lastSlice.Length) <= Constants.FingerMaxSliceDifferencePerStep)
                 {
                     gapCounter = 0;
                     numSlices++;
                     trail.addSlice(nextSlice);
                     currentDirection = trail.getEndDirection();
-                    currentPosition = nextSlice.Mid + 2*currentDirection;
+                    currentPosition = nextSlice.Mid + Constants.FingerStepSize * currentDirection;
 
                     lastSlice = nextSlice;
                 }
@@ -168,7 +162,6 @@ namespace bbiwarg.Detectors.Fingers
         }
         private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)
         {
-
             Vector2D end = findNextEdge(start, direction);
             if (end == null) return null;
 
@@ -180,8 +173,6 @@ namespace bbiwarg.Detectors.Fingers
             int maxX = depthImage.Width - 1;
             int maxY = depthImage.Height - 1;
 
-            int maxFingerSize = 35;
-
             int maxStepsX;
             if (direction.X > 0)
                 maxStepsX = (int)((maxX - start.X) / direction.X);
@@ -198,7 +189,7 @@ namespace bbiwarg.Detectors.Fingers
             else
                 maxStepsY = int.MaxValue;
 
-            int maxStepsLength = (int)(maxFingerSize / direction.Length);
+            int maxStepsLength = (int)(Constants.FingerMaxSize / direction.Length);
 
             int maxSteps = Math.Min(maxStepsLength, Math.Min(maxStepsX, maxStepsY));
 
@@ -220,15 +211,12 @@ namespace bbiwarg.Detectors.Fingers
             int maxX = depthImage.Width - 1;
             int maxY = depthImage.Height - 1;
 
-            int minFingerSize = 5;
-            int maxFingerSize = 35;
-
             Vector2D direction = (end - start).normalize();
-            Vector2D beforeStart = (start - direction).moveInBound(0,0,maxX,maxY);
+            Vector2D beforeStart = (start - direction).moveInBound(0, 0, maxX, maxY);
             Vector2D behindEnd = (end + direction).moveInBound(0, 0, maxY, maxY);
 
             FingerSlice slice = new FingerSlice(beforeStart, behindEnd);
-            if (slice.Length >= minFingerSize && slice.Length <= maxFingerSize && fingerSliceDepthTest(slice))
+            if (slice.Length >= Constants.FingerMinSize && slice.Length <= Constants.FingerMaxSize && fingerSliceDepthTest(slice))
                 return slice;
 
             return null;
@@ -251,7 +239,7 @@ namespace bbiwarg.Detectors.Fingers
 
             //draw finger
             drawDetectedFinger(finger);
-        
+
             //remove edges around detected finger to improve performance
             edgeImage.removeFingerEdges(finger);
         }