Browse Source

added touchTracker (touchEvent is tracked if it has been detected in 3 consecutive frames)

Alexander Hendrich 10 years ago
parent
commit
e95c38610d

+ 2 - 2
bbiwarg/Detectors/Fingers/FingerPoint.cs

@@ -31,8 +31,8 @@ namespace bbiwarg.Detectors.Fingers
         }
 
         public float getDistanceTo(FingerPoint fingerPoint) {
-            int xDiff = x - fingerPoint.x;
-            int yDiff = y - fingerPoint.y;
+            int xDiff = x - fingerPoint.getX();
+            int yDiff = y - fingerPoint.getY();
             float distance = (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
             return distance;
         }

+ 20 - 11
bbiwarg/Detectors/Touch/TouchDetector.cs

@@ -15,33 +15,42 @@ namespace bbiwarg.Detectors.Touch
     {
         private DepthImage depthImage;
         private List<Finger> fingers;
+        private List<TouchEvent> touchEvents;
 
         public TouchDetector(DepthImage depthImage, List<Finger> fingers) {
             this.depthImage = depthImage;
             this.fingers = fingers;
+            this.touchEvents = new List<TouchEvent>();
+            float touchValueThreshold = 0.5f;
 
             foreach (Finger finger in fingers) {
-                //farthest
                 FingerPoint fp1 = finger.getFarthest();
-                float touchFP1 = isTouchAt(fp1.getX(), fp1.getY());
-
-                //nearest
                 FingerPoint fp2 = finger.getNearest();
-                float touchFP2 = isTouchAt(fp2.getX(), fp2.getY());
-
-                int depthDifference = fp1.getDepth() - fp2.getDepth();
+                FingerPoint fp;
 
 
-                if(touchFP1 > 0.6f && touchFP2 > 0.6f && depthDifference > 20 && depthDifference < 50)
-                Console.WriteLine("TouchEvent bei x:" + fp1.getX() + " y:" + fp1.getY() + " [FP1:" + Math.Round(touchFP1, 1) + " FP2:" + Math.Round(touchFP2, 1) + "]");
+                if (fp1.getY() < fp2.getY())
+                    fp = fp1;
+                else
+                    fp = fp2;
 
+                float touchValue = getTouchValueAt(fp.getX(), fp.getY());
+                if (touchValue > touchValueThreshold)
+                {
+                    TouchEvent touchEvent = new TouchEvent(fp.getX(), fp.getY(), touchValue);
+                    touchEvents.Add(touchEvent);
+                }
             }
         }
 
-        private float isTouchAt(int touchX, int touchY) {
+        public List<TouchEvent> getTouchEvents() {
+            return touchEvents;
+        }
+
+        private float getTouchValueAt(int touchX, int touchY) {
             int searchSize = 15;
             int maxDepthDifference = 20;
-            Int16 fingerDiameter = 5;
+            Int16 fingerDiameter = 10;
             Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
 
             int minX = Math.Max(touchX - searchSize, 0);

+ 41 - 0
bbiwarg/Detectors/Touch/TouchEvent.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Detectors.Touch
+{
+    class TouchEvent
+    {
+        private int x;
+        private int y;
+        private float touchValue;
+
+        public TouchEvent(int x, int y, float touchValue) {
+            this.x = x;
+            this.y = y;
+            this.touchValue = touchValue;
+        }
+
+        public int getX() {
+            return x;
+        }
+
+        public int getY() {
+            return y;
+        }
+
+        public float getTouchValue() {
+            return touchValue;
+        }
+
+        public float getDistanceTo(TouchEvent te) {
+            int xDiff = x - te.getX();
+            int yDiff = y - te.getY();
+
+            float distance = (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
+            return distance;
+        }
+    }
+}

+ 62 - 0
bbiwarg/Detectors/Touch/TouchTracker.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Detectors.Touch
+{
+    class TouchTracker
+    {
+        private List<TouchEvent>[] detectedTouchEvents;
+        private List<TouchEvent> trackedTouchEvents;
+        private int framesUntilTracked;
+
+        public TouchTracker() {
+            framesUntilTracked = 3;
+            detectedTouchEvents = new List<TouchEvent>[framesUntilTracked];
+
+            for (int i = 0; i < framesUntilTracked; i++) {
+                detectedTouchEvents[i] = new List<TouchEvent>();
+            }
+        }
+
+        public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame) {
+            for (int i = (framesUntilTracked-1); i > 0; i--) {
+                detectedTouchEvents[i] = detectedTouchEvents[i - 1]; 
+            }
+            detectedTouchEvents[0] = touchEventsThisFrame;
+
+
+            findTrackedTouches();
+        }
+
+        private void findTrackedTouches() {
+            trackedTouchEvents = new List<TouchEvent>();
+
+            foreach (TouchEvent te in detectedTouchEvents[0]) {
+                bool tracked = true;
+                for (int i = 1; i < framesUntilTracked; i++) {
+                    if (!hasSimilarTouchEventInFrame(te, i)) tracked = false;
+                }
+                if (tracked)
+                {
+                    trackedTouchEvents.Add(te);
+                    Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [touchValue:" + te.getTouchValue() + "]");
+                }
+            }
+        }
+
+        private bool hasSimilarTouchEventInFrame(TouchEvent touchEvent, int frame) {
+            float maxDistance = 20;
+
+            foreach (TouchEvent te in detectedTouchEvents[frame]) {
+                float distance = touchEvent.getDistanceTo(te);
+                if (distance < maxDistance)
+                    return true;
+            }
+
+            return false;
+        }
+    }
+}

+ 8 - 1
bbiwarg/VideoHandle.cs

@@ -25,9 +25,13 @@ namespace bbiwarg
 
         private FingerDetector fingerDetector;
         private TouchDetector touchDetector;
+        private TouchTracker touchTracker;
 
         public VideoHandle(IInputProvider inputProvider) {
             this.inputProvider = inputProvider;
+
+            //initialize trackers
+            touchTracker = new TouchTracker();
         }
 
         public void start() {
@@ -107,8 +111,11 @@ namespace bbiwarg
             //detect fingers
             fingerDetector = new FingerDetector(depthImage, edgeImage);
 
-            //detect touch
+            //detect touchEvents
             touchDetector = new TouchDetector(depthImage, fingerDetector.getFingers());
+
+            //track touchEvents
+            touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents());
         }
     }
 }

+ 2 - 0
bbiwarg/bbiwarg.csproj

@@ -70,6 +70,8 @@
     <Compile Include="Detectors\Fingers\FingerDetector.cs" />
     <Compile Include="Detectors\Fingers\FingerPoint.cs" />
     <Compile Include="Detectors\Touch\TouchDetector.cs" />
+    <Compile Include="Detectors\Touch\TouchEvent.cs" />
+    <Compile Include="Detectors\Touch\TouchTracker.cs" />
     <Compile Include="Graphics\OutputWindow.cs" />
     <Compile Include="Images\DepthImage.cs" />
     <Compile Include="Images\EdgeImage.cs" />