Przeglądaj źródła

added touchImage

Alexander Hendrich 10 lat temu
rodzic
commit
9a66b0da7b

+ 8 - 6
bbiwarg/Detectors/Touch/TouchDetector.cs

@@ -14,12 +14,14 @@ namespace bbiwarg.Detectors.Touch
     class TouchDetector
     {
         private DepthImage depthImage;
+        private TouchImage touchImage;
         private List<Finger> fingers;
         private List<TouchEvent> touchEvents;
 
-        public TouchDetector(DepthImage depthImage, List<Finger> fingers) {
+        public TouchDetector(DepthImage depthImage, List<Finger> fingers, TouchImage touchImage) {
             this.depthImage = depthImage;
             this.fingers = fingers;
+            this.touchImage = touchImage;
             this.touchEvents = new List<TouchEvent>();
             float touchValueThreshold = 0.5f;
 
@@ -37,7 +39,7 @@ namespace bbiwarg.Detectors.Touch
                 float touchValue = getTouchValueAt(fp.getX(), fp.getY());
                 if (touchValue > touchValueThreshold)
                 {
-                    TouchEvent touchEvent = new TouchEvent(fp.getX(), fp.getY(), touchValue);
+                    TouchEvent touchEvent = new TouchEvent(fp.getX(), fp.getY(), touchValue, finger);
                     touchEvents.Add(touchEvent);
                 }
             }
@@ -50,7 +52,7 @@ namespace bbiwarg.Detectors.Touch
         private float getTouchValueAt(int touchX, int touchY) {
             int searchSize = 15;
             int maxDepthDifference = 20;
-            Int16 fingerDiameter = 10;
+            Int16 fingerDiameter = 7;
             Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
 
             int minX = Math.Max(touchX - searchSize, 0);
@@ -63,10 +65,10 @@ namespace bbiwarg.Detectors.Touch
             for (int x = minX; x < maxX; x++) {
                 for (int y = minY; y < maxY; y++) {
                     Int16 depth = depthImage.getDepthAt(x,y);
-                    depthImage.setDepthAt(x, y, Int16.MaxValue - 1);//counted pixels -> red
+                    touchImage.setTouchAt(x, y, TouchImageState.touchArea);
                     if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
                         matchedPixels++;
-                       depthImage.setDepthAt(x, y, Int16.MaxValue);//matched pixels -> blue
+                        touchImage.setTouchAt(x, y, TouchImageState.touchAreaMatched);
                     }
                     countedPixels++;
                 }
@@ -76,7 +78,7 @@ namespace bbiwarg.Detectors.Touch
 
             //status bar (% of matched pixels) -> green
             for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
-                depthImage.setDepthAt(x, maxY-1, Int16.MaxValue-2);
+                touchImage.setTouchAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
             }
 
                 return rel;

+ 4 - 1
bbiwarg/Detectors/Touch/TouchEvent.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using bbiwarg.Detectors.Fingers;
 
 namespace bbiwarg.Detectors.Touch
 {
@@ -11,11 +12,13 @@ namespace bbiwarg.Detectors.Touch
         private int x;
         private int y;
         private float touchValue;
+        private Finger finger;
 
-        public TouchEvent(int x, int y, float touchValue) {
+        public TouchEvent(int x, int y, float touchValue, Finger finger) {
             this.x = x;
             this.y = y;
             this.touchValue = touchValue;
+            this.finger = finger;
         }
 
         public int getX() {

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

@@ -21,6 +21,10 @@ namespace bbiwarg.Detectors.Touch
             }
         }
 
+        public List<TouchEvent> getTrackedTouchEvents() {
+            return trackedTouchEvents;
+        }
+
         public void setDetectedTouchEventsThisFrame(List<TouchEvent> touchEventsThisFrame) {
             for (int i = (framesUntilTracked-1); i > 0; i--) {
                 detectedTouchEvents[i] = detectedTouchEvents[i - 1]; 

+ 20 - 7
bbiwarg/Graphics/OutputWindow.cs

@@ -7,6 +7,7 @@ using System.Text;
 using System.Threading.Tasks;
 using OpenTK;
 using OpenTK.Graphics.OpenGL;
+using bbiwarg.Images;
 
 namespace bbiwarg.Graphics
 {
@@ -85,15 +86,27 @@ namespace bbiwarg.Graphics
 
                     //depthTexture
                     float relDepth = videoHandle.getRelativeDepth(x,y);
-                    if (relDepth <= 1 && relDepth >= 0)
+                    red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
+                    
+                    TouchImageState tis = videoHandle.getTouchImageStateAt(x,y);
+                    if (tis == TouchImageState.touchArea)
                     {
-                        red = green = blue = (Int16)((1.0f - videoHandle.getRelativeDepth(x, y)) * Int16.MaxValue);
+                        red = Int16.MaxValue;
+                        green = (Int16) (green / 2);
+                        blue = (Int16) (blue / 2);
                     }
-                    else {
-                        Int16 depth = videoHandle.getDepthAt(x,y);
-                        if (depth == Int16.MaxValue - 1) red = Int16.MaxValue;
-                        if (depth == Int16.MaxValue) blue = Int16.MaxValue;
-                        if (depth == Int16.MaxValue-2) green = Int16.MaxValue;
+                    else if (tis == TouchImageState.touchAreaMatched)
+                    {
+                        red = (Int16) (red / 2);
+                        blue = Int16.MaxValue;
+                        green = (Int16) (green / 2);
+                        
+                    }
+                    else if (tis == TouchImageState.touchAreaStatusBar)
+                    {
+                        red = (Int16)(red / 2);
+                        green = Int16.MaxValue;
+                        blue = (Int16)(blue / 2); ;
                     }
 
                     depthTextureData[index] = red;

+ 35 - 0
bbiwarg/Images/TouchImage.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Emgu.CV;
+using Emgu.CV.Structure;
+namespace bbiwarg.Images
+{
+    public enum TouchImageState {
+        none = 0,
+        touchArea = 1,
+        touchAreaMatched = 2,
+        touchAreaStatusBar = 3,
+        touchDetected = 4,
+        touchTracked = 5
+    }
+
+    class TouchImage
+    {
+        private Image<Gray, Int16> image;
+
+        public TouchImage(DepthImage depthImage) {
+            image = depthImage.getImage().CopyBlank();
+        }
+
+        public void setTouchAt(int x, int y, TouchImageState tis) {
+            image.Data[y, x, 0] = (Int16) tis;
+        }
+
+        public TouchImageState getStateAt(int x, int y) {
+            return (TouchImageState)image.Data[y, x, 0];
+        }
+    }
+}

+ 10 - 1
bbiwarg/VideoHandle.cs

@@ -22,6 +22,7 @@ namespace bbiwarg
         private int height;
         private DepthImage depthImage;
         private EdgeImage edgeImage;
+        private TouchImage touchImage;
 
         private FingerDetector fingerDetector;
         private TouchDetector touchDetector;
@@ -89,6 +90,10 @@ namespace bbiwarg
             return fingerDetector.isFingerPointAt(x, y);
         }
 
+        public TouchImageState getTouchImageStateAt(int x, int y) {
+            return touchImage.getStateAt(x, y);
+        }
+
         private void processFrameUpdate()
         {
             //read data from inputProvider
@@ -111,11 +116,15 @@ namespace bbiwarg
             //detect fingers
             fingerDetector = new FingerDetector(depthImage, edgeImage);
 
+            //create touchImage
+            touchImage = new TouchImage(depthImage);
+
             //detect touchEvents
-            touchDetector = new TouchDetector(depthImage, fingerDetector.getFingers());
+            touchDetector = new TouchDetector(depthImage, fingerDetector.getFingers(), touchImage);
 
             //track touchEvents
             touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents());
         }
     }
 }
+ 

+ 1 - 0
bbiwarg/bbiwarg.csproj

@@ -75,6 +75,7 @@
     <Compile Include="Graphics\OutputWindow.cs" />
     <Compile Include="Images\DepthImage.cs" />
     <Compile Include="Images\EdgeImage.cs" />
+    <Compile Include="Images\TouchImage.cs" />
     <Compile Include="InputProvider\InputFrame.cs" />
     <Compile Include="InputProvider\IInputProvider.cs" />
     <Compile Include="InputProvider\IisuInputProvider.cs" />