ソースを参照

added PalmTouchDetector (split up from TouchDetector)

Alexander Hendrich 10 年 前
コミット
418abebfbf

+ 7 - 2
bbiwarg/Detectors/Palm/Palm.cs → bbiwarg/Detectors/Palm/PalmRect.cs

@@ -16,11 +16,11 @@ namespace bbiwarg.Detectors.Palm
     {
         private Vector2D origin;
         private Matrix<float> transformationMatrix;
-        
+
         public PalmRect(MCvBox2D palmRect)
         {
             PointF[] vertices = palmRect.GetVertices();
-            
+
             origin = new Vector2D(vertices[1]);
             PointF v0 = vertices[0];
             PointF v2 = vertices[2];
@@ -30,6 +30,11 @@ namespace bbiwarg.Detectors.Palm
             CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
         }
 
+        public bool isWithinMargin(Vector2D position)
+        {
+            Vector2D relativePosition = getRelativePosition(position);
+            return (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1);
+        }
         public Vector2D getRelativePosition(Vector2D absolutePosition)
         {
             Vector2D v = absolutePosition - origin;

+ 33 - 0
bbiwarg/Detectors/Touch/PalmTouchDetector.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using bbiwarg.Detectors.Fingers;
+using bbiwarg.Detectors.Palm;
+using bbiwarg.Utility;
+
+namespace bbiwarg.Detectors.Touch
+{
+    class PalmTouchDetector
+    {
+        private List<PalmTouchEvent> palmTouchEvents;
+
+        public PalmTouchDetector(List<TouchEvent> touchEvents, PalmRect palm) {
+            palmTouchEvents = new List<PalmTouchEvent>();
+
+            foreach (TouchEvent touchEvent in touchEvents) {
+                if (palm.isWithinMargin(touchEvent.Position)) {
+                    Vector2D relativePalmPosition = palm.getRelativePosition(touchEvent.Position);
+                    PalmTouchEvent pte = new PalmTouchEvent(touchEvent.Position, relativePalmPosition, touchEvent.FloodValue, touchEvent.Finger, palm);
+                    palmTouchEvents.Add(pte);
+                }
+            }
+
+        }
+
+        public List<PalmTouchEvent> getPalmTouchEvents() {
+            return palmTouchEvents;
+        }
+    }
+}

+ 27 - 0
bbiwarg/Detectors/Touch/PalmTouchEvent.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using bbiwarg.Utility;
+using bbiwarg.Detectors.Fingers;
+using bbiwarg.Detectors.Palm;
+
+namespace bbiwarg.Detectors.Touch
+{
+    class PalmTouchEvent : TouchEvent
+    {
+        private Vector2D _relativePalmPosition;
+        public Vector2D RelativePalmPosition { get { return _relativePalmPosition; } private set { _relativePalmPosition = value; } }
+
+        private PalmRect _palm;
+        public PalmRect Palm { get { return _palm; } private set { _palm = value; } }
+
+        public PalmTouchEvent(Vector2D absolutePosition, Vector2D relativePalmPosition, float floodValue, Finger finger, PalmRect palm)
+            : base(absolutePosition, floodValue, finger)
+        {
+            RelativePalmPosition = relativePalmPosition;
+            Palm = palm;
+        }
+    }
+}

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

@@ -20,7 +20,7 @@ namespace bbiwarg.Detectors.Touch
         private List<Finger> fingers;
         private List<TouchEvent> touchEvents;
 
-        public TouchDetector(List<Finger> fingers, DepthImage depthImage, TouchImage touchImage, Palm.PalmRect palm) {
+        public TouchDetector(List<Finger> fingers, DepthImage depthImage, TouchImage touchImage) {
             this.depthImage = depthImage;
             this.touchImage = touchImage;
             this.fingers = fingers;
@@ -39,17 +39,10 @@ namespace bbiwarg.Detectors.Touch
                     float x = HelperFunctions.thresholdRange<float>(0, depthImage.getWidth() - 1, tipPoint.X + directionFactor * direction.X);
                     float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);
                     Vector2D tep = new Vector2D(x,y);
-                    Vector2D relativePosition = palm.getRelativePosition(tep);
 
-                    // detect touch event if positon is in 10% margin of palm rect (threshold coordinates to range [0,1])
-                    if (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1)
-                    {
-                        relativePosition = new Vector2D(HelperFunctions.thresholdRange<float>(0.0f, 1.0f, relativePosition.X), 
-                                                        HelperFunctions.thresholdRange<float>(0.0f, 1.0f, relativePosition.Y));
-                        touchImage.setTouchAt((int)tep.X, (int)tep.Y, TouchImageState.touchDetected);
-                        TouchEvent touchEvent = new TouchEvent((int)tep.X, (int)tep.Y, relativePosition.X, relativePosition.Y, floodValue, finger);
-                        touchEvents.Add(touchEvent);
-                    }
+                    touchImage.setTouchAt((int)tep.X, (int)tep.Y, TouchImageState.touchDetected);
+                    TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
+                    touchEvents.Add(touchEvent);
                 }
             }
         }

+ 12 - 44
bbiwarg/Detectors/Touch/TouchEvent.cs

@@ -4,62 +4,30 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using bbiwarg.Detectors.Fingers;
+using bbiwarg.Utility;
 
 namespace bbiwarg.Detectors.Touch
 {
     class TouchEvent
     {
-        private int absoluteX;
-        private int absoluteY;
-        private float relativeX;
-        private float relativeY;
-        private float floodValue;
-        private Finger finger;
+        private Vector2D _position;
+        public Vector2D Position { get { return _position; } private set { _position = value; } }
 
-        public TouchEvent(int absoluteX, int absoluteY, float relativeX, float relativeY, float floodValue, Finger finger) {
-            this.absoluteX = absoluteX;
-            this.absoluteY = absoluteY;
+        private float _floodValue;
+        public float FloodValue { get { return _floodValue; } private set { _floodValue = value; } }
 
-            this.relativeX = relativeX;
-            this.relativeY = relativeY;
+        private Finger _finger;
+        public Finger Finger { get { return _finger; } private set { _finger = value; } }
 
-            this.floodValue = floodValue;
-            this.finger = finger;
-        }
-
-        public int getAbsoluteX() {
-            return absoluteX;
-        }
-
-        public int getAbsoluteY() {
-            return absoluteY;
-        }
-
-        public float getRelativeX()
-        {
-            return relativeX;
-        }
-
-        public float getRelativeY()
-        {
-            return relativeY;
-        }
-
-        public float getFloodValue() {
-            return floodValue;
-        }
-
-        public float getDistanceTo(TouchEvent te) {
-            int xDiff = absoluteX - te.getAbsoluteX();
-            int yDiff = absoluteY - te.getAbsoluteY();
-
-            float distance = (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
-            return distance;
+        public TouchEvent(Vector2D position, float floodValue, Finger finger) {
+            Position = position;
+            FloodValue = floodValue;
+            Finger = finger;
         }
 
         public bool isSimilarTo(TouchEvent compareTouchEvent) {
             float maxDistance = 20;
-            float distance = getDistanceTo(compareTouchEvent);
+            float distance = Position.getDistanceTo(compareTouchEvent.Position);
             return (distance < maxDistance);
         }
     }

+ 1 - 1
bbiwarg/Detectors/Touch/TouchTracker.cs

@@ -49,7 +49,7 @@ namespace bbiwarg.Detectors.Touch
                 }
                 if (tracked)
                 {
-                    touchImage.setTouchAt(te.getAbsoluteX(), te.getAbsoluteY(), TouchImageState.touchTracked);
+                    touchImage.setTouchAt((int)te.Position.X, (int)te.Position.Y, TouchImageState.touchTracked);
                     trackedTouchEvents.Add(te);
                     //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
                 }

+ 0 - 9
bbiwarg/Graphics/OutputWindow.cs

@@ -125,15 +125,6 @@ namespace bbiwarg.Graphics
                 videoHandle.nextFrame();
             }
 
-            if (changedFrame)
-            {
-                //print tracked touch events
-                foreach (TouchEvent ev in videoHandle.getTrackedTouchEvents())
-                {
-                    Console.WriteLine("Touch event tracked at (" + ev.getAbsoluteX() + ", " + ev.getAbsoluteY() + ") --> (" + ev.getRelativeX() + ", " + ev.getRelativeY() + ")");
-                }
-            }
-
             //draw textures
             Int16[] depthTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];
             Int16[] edgeTextureData = new Int16[3 * videoHandle.getWidth() * videoHandle.getHeight()];

+ 3 - 6
bbiwarg/VideoHandle.cs

@@ -30,6 +30,7 @@ namespace bbiwarg
         private FingerDetector fingerDetector;
         private PalmDetector palmDetector;
         private TouchDetector touchDetector;
+        private PalmTouchDetector palmTouchDetector;
 
         private FingerTracker fingerTracker;
         private TouchTracker touchTracker;
@@ -122,11 +123,6 @@ namespace bbiwarg
             return touchImage.getStateAt(x, y);
         }
 
-        public List<TouchEvent> getTrackedTouchEvents()
-        {
-            return touchTracker.getTouchEvents();
-        }
-
         private void processFrameUpdate()
         {
             //read data from inputProvider
@@ -157,7 +153,8 @@ namespace bbiwarg
             palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector, palmImage);
 
             //detect+track touchEvents
-            touchDetector = new TouchDetector(fingerTracker.getFingers(), depthImage, touchImage, palmDetector.getPalm());
+            touchDetector = new TouchDetector(fingerTracker.getFingers(), depthImage, touchImage);
+            palmTouchDetector = new PalmTouchDetector(touchDetector.getTouchEvents(), palmDetector.getPalm());
             touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents(), touchImage);
         }
     }

+ 3 - 1
bbiwarg/bbiwarg.csproj

@@ -71,8 +71,10 @@
     <Compile Include="Detectors\Fingers\FingerSliceTrail.cs" />
     <Compile Include="Detectors\Fingers\FingerSlice.cs" />
     <Compile Include="Detectors\Fingers\FingerTracker.cs" />
-    <Compile Include="Detectors\Palm\Palm.cs" />
+    <Compile Include="Detectors\Palm\PalmRect.cs" />
     <Compile Include="Detectors\Palm\PalmDetector.cs" />
+    <Compile Include="Detectors\Touch\PalmTouchDetector.cs" />
+    <Compile Include="Detectors\Touch\PalmTouchEvent.cs" />
     <Compile Include="Detectors\Touch\TouchDetector.cs" />
     <Compile Include="Detectors\Touch\TouchEvent.cs" />
     <Compile Include="Detectors\Touch\TouchTracker.cs" />