Kaynağa Gözat

Added Quadrangle.

Daniel Kauth 10 yıl önce
ebeveyn
işleme
cdf3989b0d

+ 7 - 7
bbiwarg/Detectors/Palm/PalmDetector.cs

@@ -32,7 +32,7 @@ namespace bbiwarg.Detectors.Palm
         private List<MCvConvexityDefect> convexityDefects;
         private Vector2D wristPoint, wristDirection;
         private LineSegment2DF wristLine, thumbLine;
-        private Quad2D palmRect;
+        private Quadrangle palmQuad;
 
         private bool valid = false;
         private Vector2D topLeft;
@@ -72,9 +72,9 @@ namespace bbiwarg.Detectors.Palm
             }
         }
 
-        public PalmRect getPalm()
+        public Quadrangle getPalmQuad()
         {
-            return new PalmRect(palmRect);
+            return palmQuad;
         }
 
         private List<Finger> getFingersWithoutThumb(FingerDetector fingerDetector)
@@ -220,12 +220,12 @@ namespace bbiwarg.Detectors.Palm
                 wristLine = new LineSegment2DF(bottomLeft - 1000 * handWidth, bottomRight + 1000 * handWidth);
                 thumbLine = new LineSegment2DF(topLeft + 1000 * handLength, bottomLeft - 1000 * handLength);
 
-                palmRect = new Quad2D(bottomLeft, topLeft, topRight, bottomRight);
+                palmQuad = new Quadrangle(new Vector2D[] {bottomLeft, topLeft, topRight, bottomRight});
 
                 valid = true;
             }
             else {
-                palmRect = null;
+                palmQuad = null;
                 valid = false;
             }
         }
@@ -252,9 +252,9 @@ namespace bbiwarg.Detectors.Palm
             palmImage.drawLine(wristLine, PalmImageState.wristLine);
             palmImage.drawLine(thumbLine, PalmImageState.thumbLine);
 
-            if (palmRect != null)
+            if (palmQuad != null)
             {
-                PointF[] vertices = palmRect.getVertices();
+                Vector2D[] vertices = palmQuad.Vertices;
                 for (int i = 0; i < 4; ++i)
                     palmImage.drawLine(new LineSegment2DF(vertices[i], vertices[(i + 1) % 4]), PalmImageState.palmRect);
 

+ 0 - 57
bbiwarg/Detectors/Palm/PalmRect.cs

@@ -1,57 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Drawing;
-
-using bbiwarg.Utility;
-
-using Emgu.CV;
-using Emgu.CV.Structure;
-
-namespace bbiwarg.Detectors.Palm
-{
-    class PalmRect
-    {
-        private Vector2D origin;
-        private Matrix<float> transformationMatrix;
-        bool valid = true;
-
-        public PalmRect(Quad2D palmRect)
-        {
-            if (palmRect == null)
-            {
-                valid = false;
-                return;
-            }
-
-            //TODO
-            origin = palmRect.Origin;
-            PointF v0 = palmRect.Origin + palmRect.DirWidth;
-            PointF v2 = palmRect.Origin + palmRect.DirLength;
-
-            Matrix<float> tmp = new Matrix<float>(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } });
-            transformationMatrix = new Matrix<float>(2, 2);
-            CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
-        }
-
-        public bool isWithinMargin(Vector2D position)
-        {
-            if (!valid)
-                return false;
-            
-            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)
-        {
-            if (!valid)
-                return null;
-            
-            Vector2D v = absolutePosition - origin;
-            Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
-            return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
-        }
-    }
-}

+ 4 - 4
bbiwarg/Detectors/Touch/PalmTouchDetector.cs

@@ -13,13 +13,13 @@ namespace bbiwarg.Detectors.Touch
     {
         private List<PalmTouchEvent> palmTouchEvents;
 
-        public PalmTouchDetector(List<TouchEvent> touchEvents, PalmRect palm) {
+        public PalmTouchDetector(List<TouchEvent> touchEvents, Quadrangle palmQuad) {
             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);
+                Vector2D relativePos = palmQuad.getRelativePosition(touchEvent.Position);
+                if (relativePos.X >= 0 && relativePos.X <= 1.0 && relativePos.Y >= 0 && relativePos.Y <= 1.0) {
+                    PalmTouchEvent pte = new PalmTouchEvent(touchEvent.Position, relativePos, touchEvent.FloodValue, touchEvent.Finger);
                     palmTouchEvents.Add(pte);
                 }
             }

+ 1 - 5
bbiwarg/Detectors/Touch/PalmTouchEvent.cs

@@ -14,14 +14,10 @@ namespace bbiwarg.Detectors.Touch
         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)
+        public PalmTouchEvent(Vector2D absolutePosition, Vector2D relativePalmPosition, float floodValue, Finger finger)
             : base(absolutePosition, floodValue, finger)
         {
             RelativePalmPosition = relativePalmPosition;
-            Palm = palm;
         }
     }
 }

+ 0 - 31
bbiwarg/Utility/Quad2D.cs

@@ -1,31 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-using System.Drawing;
-
-namespace bbiwarg.Utility
-{
-    class Quad2D
-    {
-        public Vector2D TopLeft {private set; get;}
-        public Vector2D TopRight { private set; get; }
-        public Vector2D BottomLeft { private set; get; }
-        public Vector2D BottomRight { private set; get; }
-
-        public Quad2D(Vector2D bottomLeft, Vector2D topLeft, Vector2D topRight, Vector2D bottomRight)
-        {
-            TopLeft = topLeft;
-            TopRight = topRight;
-            BottomLeft = bottomLeft;
-            BottomRight = BottomRight;
-        }
-
-        public PointF[] getVertices()
-        {
-            return new PointF[] {TopLeft,TopRight,BottomRight,BottomLeft};
-        }
-    }
-}

+ 48 - 0
bbiwarg/Utility/Quadrangle.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using System.Drawing;
+
+namespace bbiwarg.Utility
+{
+    class Quadrangle
+    {
+        public Quadrangle(Vector2D[] vertices)
+        {
+            Vertices = vertices;
+        }
+
+        public Vector2D[] Vertices
+        {
+            private set;
+            get;
+        }
+
+        public Vector2D getRelativePosition(Vector2D p)
+        {
+            Vector2D a = Vertices[0];
+            Vector2D b = Vertices[1];
+            Vector2D c = Vertices[2];
+            Vector2D d = Vertices[3];
+
+            float C = (a.Y - p.Y) * (d.X - p.X) - (a.X - p.X) * (d.Y - p.Y);
+            float B = (a.Y - p.Y) * (c.X - d.X) + (b.Y - a.Y) * (d.X - p.X) - (a.X - p.X) * (c.Y - d.Y) - (b.X - a.X) * (d.Y - p.Y);
+            float A = (b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y);
+
+            float D = B * B - 4 * A * C;
+
+            float x = (-B - (float) Math.Sqrt(D)) / (2 * A);
+
+            float p1x = a.X + (b.X - a.X) * x;
+            float p2x = d.X + (c.X - d.X) * x;
+            float px = p.X;
+
+            float y = (px - p1x) / (p2x - p1x);
+
+            return new Vector2D(x, y);
+        }
+    }
+}

+ 1 - 1
bbiwarg/VideoHandle.cs

@@ -154,7 +154,7 @@ namespace bbiwarg
 
             //detect+track touchEvents
             touchDetector = new TouchDetector(fingerTracker.getFingers(), depthImage, touchImage);
-            palmTouchDetector = new PalmTouchDetector(touchDetector.getTouchEvents(), palmDetector.getPalm());
+            palmTouchDetector = new PalmTouchDetector(touchDetector.getTouchEvents(), palmDetector.getPalmQuad());
             touchTracker.setDetectedTouchEventsThisFrame(touchDetector.getTouchEvents(), touchImage);
         }
     }

+ 1 - 2
bbiwarg/bbiwarg.csproj

@@ -71,7 +71,6 @@
     <Compile Include="Detectors\Fingers\FingerSliceTrail.cs" />
     <Compile Include="Detectors\Fingers\FingerSlice.cs" />
     <Compile Include="Detectors\Fingers\FingerTracker.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" />
@@ -91,7 +90,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Utility\HelperFunctions.cs" />
     <Compile Include="Utility\Line2D.cs" />
-    <Compile Include="Utility\Quad2D.cs" />
+    <Compile Include="Utility\Quadrangle.cs" />
     <Compile Include="Utility\Vector.cs" />
     <Compile Include="Utility\Vector2D.cs" />
     <Compile Include="VideoHandle.cs" />