Browse Source

implemented findFingers (clusters fingerPoints and filters fingers by a minimum length)

Alexander Hendrich 10 years ago
parent
commit
691a9c95fa
3 changed files with 111 additions and 0 deletions
  1. 51 0
      bbiwarg/DepthImage.cs
  2. 59 0
      bbiwarg/Finger.cs
  3. 1 0
      bbiwarg/bbiwarg.csproj

+ 51 - 0
bbiwarg/DepthImage.cs

@@ -17,6 +17,7 @@ namespace bbiwarg
         private Image<Gray, Int16> image;
         private Image<Gray, Int16> edges;
         private bool[,] fingerPoints;
+        private List<Finger> fingers;
 
         private Int16 minDepth;
         private Int16 maxDepth;
@@ -39,6 +40,10 @@ namespace bbiwarg
 
             //findFingerPoints
             findFingerPoints();
+
+            //findFingers
+            findFingers();
+
         }
 
         public int getWidth() {
@@ -135,6 +140,7 @@ namespace bbiwarg
 
                             if ((edgeRightX - x) < maxFingerSize && depthLeft > depthMid && depthMid < depthRight) {
                                 fingerPoints[midX, y] = true;
+                             
                             } 
                         }
 
@@ -165,5 +171,50 @@ namespace bbiwarg
                 }
             }
         }
+
+        private void findFingers() {
+            float maxDistanceTogether = 5.0f;
+            float minFingerLength = 20.0f;
+            List<Finger> possibleFingers = new List<Finger>();
+
+            for (int y = 0; y < height; y++) {
+                for (int x = 0; x < width; x++) {
+                    if (fingerPoints[x, y]) {
+                        Point fingerPoint = new Point(x,y);
+                        float minDistanceValue = float.MaxValue;
+                        int minDistanceIndex = 0;
+                        for (int i = 0; i < possibleFingers.Count; i++)
+                        {
+                            float distance = possibleFingers[i].getMinDistance(fingerPoint);
+                            if (distance < minDistanceValue) {
+                                minDistanceValue = distance;
+                                minDistanceIndex = i;
+                            }
+                        }
+                        if (minDistanceValue < maxDistanceTogether)
+                        {
+                            possibleFingers[minDistanceIndex].add(fingerPoint);
+                        }
+                        else 
+                        {
+                            possibleFingers.Add(new Finger(fingerPoint));
+                        }
+                    }
+                }
+            }
+
+            fingers = new List<Finger>();
+            fingerPoints = new bool[width, height];
+            foreach (Finger finger in possibleFingers) {
+                float length = finger.getLength();
+                if (length > minFingerLength)
+                {
+                    fingers.Add(finger);
+                    foreach (Point fingerPoint in finger.getFingerPoints()) {
+                        fingerPoints[fingerPoint.X, fingerPoint.Y] = true;
+                    }
+                }
+            }
+        }
     }
 }

+ 59 - 0
bbiwarg/Finger.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg
+{
+    class Finger
+    {
+        private List<Point> fingerPoints;
+
+        public Finger(Point fingerPoint) {
+            fingerPoints = new List<Point>();
+            fingerPoints.Add(fingerPoint);
+        }
+
+        public float getMinDistance(Point fingerPoint) {
+            float minDinstance = float.MaxValue;
+            foreach(Point fp in fingerPoints) {
+                int xDiff = fp.X-fingerPoint.X;
+                int yDiff = fp.Y-fingerPoint.Y;
+                float distance = (float)Math.Sqrt(xDiff*xDiff + yDiff*yDiff);
+                if(distance < minDinstance) {
+                    minDinstance = distance;
+                }
+            }
+
+            return minDinstance;
+        }
+
+        public void add(Point fingerPoint) {
+            fingerPoints.Add(fingerPoint);
+        }
+
+        public float getLength() {
+            int minX = int.MaxValue;
+            int maxX = int.MinValue;
+            int minY = int.MaxValue;
+            int maxY = int.MinValue;
+            foreach (Point fingerPoint in fingerPoints) {
+                if (fingerPoint.X < minX) minX = fingerPoint.X;
+                if (fingerPoint.X > maxX) maxX = fingerPoint.X;
+                if (fingerPoint.Y < minY) minY = fingerPoint.Y;
+                if (fingerPoint.Y > maxY) maxY = fingerPoint.Y;
+            }
+
+            int xDiff = maxX-minX;
+            int yDiff = maxY-minY;
+
+            return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
+        }
+
+        public List<Point> getFingerPoints() {
+            return fingerPoints;
+        }
+    }
+}

+ 1 - 0
bbiwarg/bbiwarg.csproj

@@ -67,6 +67,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="DepthImage.cs" />
+    <Compile Include="Finger.cs" />
     <Compile Include="Graphics\OutputWindow.cs" />
     <Compile Include="InputProvider\InputFrame.cs" />
     <Compile Include="InputProvider\IInputProvider.cs" />