Explorar el Código

moved touch-constants to constants-class

Alexander Hendrich hace 11 años
padre
commit
38bc93ac75

+ 12 - 1
bbiwarg/Constants.cs

@@ -43,15 +43,26 @@ namespace bbiwarg
         public static readonly int FingerMaxSliceDifferencePerStep = 5;
         public static readonly int FingerMaxSize = 35;
         public static readonly int FingerMinSize = 5;
+        public static readonly int FingerNumSlicesForRelativeDirection = 5;
+        public static readonly int FingerNumFramesUntilTracked = 2;
+        public static readonly float FingerMinSimilarityForTracking = 0.75f;
 
         // 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
 
-        //palm Grid
+        // palm Grid
         public static readonly int PalmGridRows = 4;
         public static readonly int PalmGridColumns = 3;
 
+        // touch detection
+        public static readonly float TouchEventMinFloodValue = 0.33f;
+        public static readonly int TouchEventTipCorrectionFactor = 5;
+        public static readonly int TouchEventAreaSize = 30;
+        public static readonly int TouchEventMaxDepthDifference = 20;
+        public static readonly int TouchEventFingerDiameter = 5;
+        public static readonly int TouchEventNumFramesUntilTracked = 2;
+
         // output window
         public static readonly int NumImagesPerRow = 2;
         public static readonly float WindwoSizeFactor = 1f; // output window size is scaled by this factor (from necessary size for images)

+ 1 - 1
bbiwarg/Detectors/Fingers/FingerSliceTrail.cs

@@ -27,7 +27,7 @@ namespace bbiwarg.Detectors.Fingers
         }
 
         public Vector2D getEndDirection() {
-            int numSlicesToInnerEnd = 5;
+            int numSlicesToInnerEnd = Constants.FingerNumSlicesForRelativeDirection;
             int innerEndIndex = Math.Max(0, NumSlices - numSlicesToInnerEnd);
 
             return (End.Mid - Slices[innerEndIndex].Mid).normalize();

+ 3 - 5
bbiwarg/Detectors/Fingers/FingerTracker.cs

@@ -18,11 +18,10 @@ namespace bbiwarg.Detectors.Fingers
 
         public FingerTracker()
         {
-            framesUntilTracked = 3;
-            detectedFingers = new List<Finger>[framesUntilTracked];
+            detectedFingers = new List<Finger>[Constants.FingerNumFramesUntilTracked];
             TrackedFingers = new List<Finger>();
 
-            for (int i = 0; i < framesUntilTracked; i++)
+            for (int i = 0; i < Constants.FingerNumFramesUntilTracked; i++)
             {
                 detectedFingers[i] = new List<Finger>();
             }
@@ -44,7 +43,6 @@ namespace bbiwarg.Detectors.Fingers
 
         private void findTrackedFingers()
         {
-            float minSimilarity = 0.75f;
             TrackedFingers = new List<Finger>();
 
             foreach (Finger finger in detectedFingers[0])
@@ -54,7 +52,7 @@ namespace bbiwarg.Detectors.Fingers
                 for (int i = 1; i < framesUntilTracked; i++)
                 {
                     Finger mostSimilarThisFrame = getMostSimilarFingerInFrame(mostSimilarLastFrame, i);
-                    if (mostSimilarThisFrame == null || mostSimilarLastFrame.getSimilarity(mostSimilarThisFrame) < minSimilarity)
+                    if (mostSimilarThisFrame == null || mostSimilarLastFrame.getSimilarity(mostSimilarThisFrame) < Constants.FingerMinSimilarityForTracking)
                     {
                         tracked = false;
                         break;

+ 14 - 21
bbiwarg/Detectors/Touch/TouchDetector.cs

@@ -27,7 +27,6 @@ namespace bbiwarg.Detectors.Touch
             this.outputImage = outputImage;
             this.fingers = fingers;
             this.TouchEvents = new List<TouchEvent>();
-            float floodValueThreshold = 0.33f;
 
             foreach (Finger finger in fingers)
             {
@@ -36,12 +35,12 @@ namespace bbiwarg.Detectors.Touch
                 outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
 
                 float floodValue = getFloodValue(tipPoint);
-                if (floodValue > floodValueThreshold)
+                if (floodValue > Constants.TouchEventMinFloodValue)
                 {
                     //correct touchEvent position
                     Vector2D direction = finger.LineSegment.Line.Direction;
-                    Vector2D tep = (tipPoint + 5 * direction).moveInBound(0,0,depthImage.Width-1,depthImage.Height-1);
-                    
+                    Vector2D tep = (tipPoint + Constants.TouchEventTipCorrectionFactor * direction).moveInBound(0, 0, depthImage.Width - 1, depthImage.Height - 1);
+
                     outputImage.fillCircle(tep.IntX, tep.IntY, 5, Constants.TouchEventDetectedColor);
                     TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
                     TouchEvents.Add(touchEvent);
@@ -51,26 +50,23 @@ namespace bbiwarg.Detectors.Touch
 
         private float getFloodValue(Point touchPoint)
         {
-            int searchSize = 15;
-            int maxDepthDifference = 20;
-            Int16 fingerDiameter = 5;
-            Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + fingerDiameter);
+            Int16 depthAtTouch = (Int16)(depthImage.getDepthAt(touchPoint) + Constants.TouchEventFingerDiameter);
 
-            int minX = Math.Max(touchPoint.X - searchSize, 0);
-            int maxX = Math.Min(touchPoint.X + searchSize, depthImage.Width);
-            int minY = Math.Max(touchPoint.Y - searchSize, 0);
-            int maxY = Math.Min(touchPoint.Y + searchSize, depthImage.Height);
+            int minX = Math.Max(touchPoint.X - Constants.TouchEventAreaSize / 2, 0);
+            int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 2, depthImage.Width-1);
+            int minY = Math.Max(touchPoint.Y - Constants.TouchEventAreaSize / 2, 0);
+            int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 2, depthImage.Height-1);
 
             int matchedPixels = 0;
             int countedPixels = 0;
-            for (int x = minX; x < maxX; x++)
+            for (int x = minX; x <= maxX; x++)
             {
-                for (int y = minY; y < maxY; y++)
+                for (int y = minY; y <= maxY; y++)
                 {
                     Int16 depth = depthImage.getDepthAt(x, y);
                     Color color = outputImage.getColotAt(x, y);
                     Color subtractColor;
-                    if (depthAtTouch < depth && Math.Abs(depthAtTouch - depth) < maxDepthDifference)
+                    if (depthAtTouch < depth && Math.Abs(depthAtTouch - depth) < Constants.TouchEventMaxDepthDifference)
                     {
                         matchedPixels++;
                         subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
@@ -87,12 +83,9 @@ namespace bbiwarg.Detectors.Touch
 
             float rel = (float)matchedPixels / (float)countedPixels;
 
-            //status bar (% of matched pixels) -> green
-            for (int x = minX; x < minX + (maxX - minX) * rel; x++)
-            {
-                outputImage.drawPixel(x, maxY - 1, Constants.TouchEventStatusBarColor);
-            }
-
+            outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor);
+            outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY-1), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY-1)), Constants.TouchEventStatusBarColor);
+            
             return rel;
         }
     }

+ 2 - 3
bbiwarg/Detectors/Touch/TouchTracker.cs

@@ -18,10 +18,9 @@ namespace bbiwarg.Detectors.Touch
 
         public TouchTracker()
         {
-            framesUntilTracked = 2;
-            detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
+            detectedTouchEvents = new List<TouchEvent>[Constants.TouchEventNumFramesUntilTracked];
 
-            for (int i = 0; i < framesUntilTracked; i++)
+            for (int i = 0; i < Constants.TouchEventNumFramesUntilTracked; i++)
             {
                 detectedTouchEvents[i] = new List<TouchEvent>();
             }