Pārlūkot izejas kodu

added coordinateConverter (2D+depth -> 3D)

Alexander Hendrich 10 gadi atpakaļ
vecāks
revīzija
694fa16ded

+ 2 - 1
bbiwarg/Input/InputHandling/InputHandler.cs

@@ -24,6 +24,7 @@ namespace bbiwarg.Input.InputHandling
     public class InputHandler
     {
         private InputProvider inputProvider;
+        private CoordinateConverter coordinateConverter;
         private bool resetFlag;
 
         private FingerDetector fingerDetector;
@@ -55,6 +56,7 @@ namespace bbiwarg.Input.InputHandling
         private void initialize()
         {
             ImageSize = new ImageSize(inputProvider.ImageWidth, inputProvider.ImageHeight);
+            coordinateConverter = new CoordinateConverter(ImageSize, inputProvider.HFOV, inputProvider.VFOV);
             resetFlag = false;
 
             fingerDetector = new FingerDetector();
@@ -144,7 +146,6 @@ namespace bbiwarg.Input.InputHandling
             touchTracker.trackTouches(frameData);
             Timer.stop("InputHandler.handleNewFrame::trackTouches");
 
-
             Timer.start("InputHandler.handleNewFrame::exportResults");
             FrameData = frameData;
             if (NewProcessedFrameEvent != null)

+ 12 - 6
bbiwarg/Input/InputProviding/InputProvider.cs

@@ -35,15 +35,19 @@ namespace bbiwarg.Input.InputProviding
         protected IDevice device;
 
         protected IParameterHandle<float> frameRate;
-        protected IParameterHandle<int> imageWidth;
-        protected IParameterHandle<int> imageHeight;
+        protected IParameterHandle<int> width;
+        protected IParameterHandle<int> height;
+        protected IParameterHandle<float> hfov;
+        protected IParameterHandle<float> vfov;
         protected IDataHandle<Iisu.Data.IImageData> depthImage;
         protected IDataHandle<Iisu.Data.IImageData> confidenceImage;
 
         protected int lastFrameID;
 
-        public int ImageWidth { get { return imageWidth.Value; } }
-        public int ImageHeight { get { return imageHeight.Value; } }
+        public int ImageWidth { get { return width.Value; } }
+        public int ImageHeight { get { return height.Value; } }
+        public float HFOV { get { return hfov.Value; } }
+        public float VFOV { get { return vfov.Value; } }
         public bool IsActive { get; private set; }
         public virtual int CurrentFrameID { get { return device.FrameId; } }
 
@@ -95,8 +99,10 @@ namespace bbiwarg.Input.InputProviding
 
         protected virtual void registerHandles()
         {
-            imageWidth = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
-            imageHeight = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
+            width = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
+            height = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
+            hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
+            vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
             frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
 
             depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");

+ 1 - 1
bbiwarg/Parameters.cs

@@ -71,7 +71,7 @@ namespace bbiwarg
         public static readonly int FingerMinWidth = 2;
         public static readonly int FingerRemoveNumSlicesForCorrection = 10 / FingerStepSize;
         public static readonly int FingerNumSlicesForRelativeDirection = FingerRemoveNumSlicesForCorrection;
-        public static readonly int FingerOutMargin = 8;
+        public static readonly int FingerOutMargin = 6;
         public static readonly int FingerMaxCrippleDifference = 20;
         public static readonly int FingerContourMargin = 4;
 

+ 1 - 1
bbiwarg/Recognition/FingerRecognition/FingerDetector.cs

@@ -155,7 +155,7 @@ namespace bbiwarg.Recognition.FingerRecognition
 
             Vector2D end = edgeImageAdapted.findNextRoughEdge(position, dirEnd, Parameters.FingerMaxWidth);
             if (end == null) return null;
-
+            
             return getFingerSlice(start, end);
         }
         private FingerSlice findFingerSliceFromStartEdge(Vector2D start, Vector2D direction)

+ 49 - 0
bbiwarg/Utility/CoordinateConverter.cs

@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Utility
+{
+    class CoordinateConverter
+    {
+        private ImageSize imageSize;
+        private float hfov;
+        private float vfov;
+        private float hRatio;
+        private float vRatio;
+
+        public CoordinateConverter(ImageSize imageSize, float hfov, float vfov)
+        {
+            this.imageSize = imageSize;
+            this.hfov = hfov;
+            this.vfov = vfov;
+
+            hRatio = (float)Math.Tan(hfov / 2);
+            vRatio = (float)Math.Tan(vfov / 2);
+        }
+
+        public Vector3D convert(Vector2D p, float depth) {
+            return convert(p.X, p.Y, depth);
+        }
+
+        public Vector3D convert(float x, float y, float depth)
+        {
+            float deltaX = 2 * ((x / imageSize.Width) - 0.5f);
+            float deltaY = -2 * ((y / imageSize.Height) - 0.5f);
+
+            float tanX = deltaX * hRatio;
+            float tanY = deltaY * vRatio;
+
+            float a = depth * depth;
+            float b = (float)(Math.Pow(tanX, 2) + Math.Pow(tanY, 2));
+
+            float z3 = (float)(Math.Sqrt(0.5f*a + Math.Sqrt(0.25f*Math.Pow(a,2)-b)));
+            float x3 = tanX * z3;
+            float y3 = tanY * z3;
+
+            return new Vector3D(x3, y3, z3);
+        }
+    }
+}

+ 2 - 3
bbiwarg/Utility/Vector2D.cs

@@ -14,9 +14,8 @@ namespace bbiwarg.Utility
         public float Y { get; private set; }
         public int IntX { get { return (int)X; } }
         public int IntY { get { return (int)Y; } }
-        public float Length { get { counter++;  return (float)Math.Sqrt(X * X + Y * Y); } }
-        public static int counter = 0;
-
+        public float Length { get { return (float)Math.Sqrt(X * X + Y * Y); } }
+        
         public Vector2D(float x, float y)
         {
             X = x;

+ 62 - 0
bbiwarg/Utility/Vector3D.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Utility
+{
+    class Vector3D
+    {
+        public float X { get; private set; }
+        public float Y { get; private set; }
+        public float Z { get; private set; }
+        public int IntX { get { return (int)X; } }
+        public int IntY { get { return (int)Y; } }
+        public int IntZ { get { return (int)Z; } }
+        public float Length { get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); } }
+
+        public Vector3D(float x, float y, float z)
+        {
+            X = x;
+            Y = y;
+            Z = z;
+        }
+
+        public float getDistanceTo(Vector3D point)
+        {
+            return (this - point).Length;
+        }
+
+        public static Vector3D operator *(float scalar, Vector3D v)
+        {
+            return new Vector3D(scalar * v.X, scalar * v.Y, scalar * v.Z);
+        }
+
+        public static Vector3D operator *(Vector3D v, float scalar)
+        {
+            return new Vector3D(scalar * v.X, scalar * v.Y, scalar * v.Z);
+        }
+
+        public static Vector3D operator /(Vector3D v, float scalar)
+        {
+            return new Vector3D(v.X / scalar, v.Y / scalar, v.Z / scalar);
+        }
+
+        public static Vector3D operator /(Vector3D v1, Vector3D v2)
+        {
+            return new Vector3D(v1.X / v2.X, v1.Y / v2.Y, v1.Z / v2.Z);
+        }
+
+        public static Vector3D operator +(Vector3D v1, Vector3D v2)
+        {
+            return new Vector3D(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
+        }
+
+        public static Vector3D operator -(Vector3D v1, Vector3D v2)
+        {
+            return new Vector3D(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
+        }
+
+    }
+}

+ 2 - 0
bbiwarg/bbiwarg.csproj

@@ -89,6 +89,7 @@
     <Compile Include="Output\GlassesOutput\GlassesWindow.Designer.cs">
       <DependentUpon>GlassesWindow.cs</DependentUpon>
     </Compile>
+    <Compile Include="Utility\CoordinateConverter.cs" />
     <Compile Include="Utility\ImageSize.cs" />
     <Compile Include="Input\InputHandling\FrameData.cs" />
     <Compile Include="Output\DebugOutput\DebugImageCreator.cs" />
@@ -152,6 +153,7 @@
     <Compile Include="Utility\Quadrangle.cs" />
     <Compile Include="Utility\Timer.cs" />
     <Compile Include="Utility\Vector2D.cs" />
+    <Compile Include="Utility\Vector3D.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />