瀏覽代碼

improved findFingerPoints

Alexander Hendrich 11 年之前
父節點
當前提交
3ba799c9ce
共有 3 個文件被更改,包括 51 次插入55 次删除
  1. 39 49
      bbiwarg/DepthImage.cs
  2. 11 5
      bbiwarg/Graphics/OutputWindow.cs
  3. 1 1
      bbiwarg/MainBBWIWARG.cs

+ 39 - 49
bbiwarg/DepthImage.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Drawing;
 using System.Linq;
 using System.Text;
@@ -30,7 +31,7 @@ namespace bbiwarg
             findMinDepth();
 
             //maxDepth
-            maxDepth = (Int16) (minDepth + 200);
+            maxDepth = (Int16)(minDepth + 200);
             thresholdDepth(minDepth, maxDepth);
 
             //edges
@@ -66,7 +67,7 @@ namespace bbiwarg
         }
 
         public bool isFingerPoint(int x, int y) {
-            return fingerPoints[x,y];
+            return fingerPoints[x, y];
         }
 
         private void setDepthAt(int x, int y, Int16 value) {
@@ -103,7 +104,7 @@ namespace bbiwarg
 
         private void calculateEdges() {
             Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(Int16 depth) { return (byte)(((depth - minDepth) * Byte.MaxValue) / (maxDepth - minDepth)); });
-            Image<Gray, byte> tmp2 = tmp.Canny(100, 75, 3);
+            Image<Gray, byte> tmp2 = tmp.Canny(150, 75, 3);
             edges = tmp2.Convert<Int16>(delegate(byte depth) { if(depth > 0) {return Int16.MaxValue;} else {return 0;}});//((depth * Int16.MaxValue) / Byte.MaxValue); });
         }
 
@@ -112,65 +113,54 @@ namespace bbiwarg
             int maxFingerSize = 30;
             int minFingerSize = 10;
             fingerPoints = new bool[width, height];
-            bool[,] pixelsCheckedHorizontal = new bool[width, height];
-            bool[,] pixelsCheckedVertical = new bool[width, height];
-
-            for (int x = 0; x < width; x++) {
-                for (int y = 0; y < height; y++) {
-                    if (pixelsCheckedHorizontal[x,y] == false && getDepthAt(x, y) < maxDepth && getEdgeAt(x,y) == 0) {
-                        pixelsCheckedHorizontal[x, y] = true;
-                        pixelsCheckedVertical[x, y] = true;
-
-                        //check horizontal
-                        int edgeLeftX = x-1;
-                        int edgeRightX = x+1;
-                        bool edgeLeftFound = false;
+
+            for (int y = 0; y < height; y++) {
+                for (int x = 0; x < width; x++) {
+                    if (getEdgeAt(x, y) > 0) {
+                        //search horizontal
                         bool edgeRightFound = false;
-                        while (edgeLeftFound == false && (x-edgeLeftX) < maxFingerSize && edgeLeftX > 0)
-                        {
-                            if (getEdgeAt(edgeLeftX, y) > 0) 
-                                edgeLeftFound = true;
-                            edgeLeftX--;
-                        }
-                        while (edgeRightFound == false && (edgeRightX - x) < maxFingerSize && edgeRightX < width)
-                        {
+                        int edgeRightX = x + minFingerSize;
+                        while (!edgeRightFound && edgeRightX < width) {
                             if (getEdgeAt(edgeRightX, y) > 0)
                                 edgeRightFound = true;
-                            edgeRightX++;
+                            else
+                                edgeRightX++;
                         }
 
-                        if (edgeLeftFound && edgeRightFound && (edgeRightX - edgeLeftX) < maxFingerSize && (edgeRightX - edgeLeftX) > minFingerSize) {
-                            for (int i = edgeLeftX; i < edgeRightX; i++) {
-                                pixelsCheckedHorizontal[i, y] = true;
-                            }
-                            fingerPoints[(edgeLeftX+edgeRightX)/2, y] = true;
+                        if (edgeRightFound){
+                            int midX = (edgeRightX + x) / 2;
+                            Int16 depthLeft = getDepthAt(x, y);
+                            Int16 depthMid = getDepthAt(midX, y);
+                            Int16 depthRight = getDepthAt(edgeRightX, y);
+
+                            if ((edgeRightX - x) < maxFingerSize && depthLeft > depthMid && depthMid < depthRight) {
+                                fingerPoints[midX, y] = true;
+                            } 
                         }
 
-                        //check vertical
-                        int edgeTopY = y-1;
-                        int edgeBottomY = y+1;
-                        bool edgeTopFound = false;
+                        //search vertical
                         bool edgeBottomFound = false;
-                        while (edgeTopFound == false && (y-edgeTopY) < maxFingerSize && edgeTopY > 0)
-                        {
-                            if (getEdgeAt(x, edgeTopY) > 0)
-                                edgeTopFound = true;
-                            edgeTopY--;
-                        }
-                        while (edgeBottomFound == false && (edgeBottomY-y) < maxFingerSize && edgeBottomY < height)
-                        {
+                        int edgeBottomY = y + minFingerSize;
+                        while (!edgeBottomFound && edgeBottomY < height) {
                             if (getEdgeAt(x, edgeBottomY) > 0)
                                 edgeBottomFound = true;
-                            edgeBottomY++;
+                            else
+                                edgeBottomY++;
                         }
-                        if (edgeBottomFound && edgeTopFound && (edgeBottomY - edgeTopY) < maxFingerSize && (edgeBottomY - edgeTopY) > minFingerSize)
-                        {
-                            for (int i = edgeTopY; i < edgeBottomY; i++)
-                            {
-                                pixelsCheckedVertical[x, i] = true;
+
+                        if (edgeBottomFound) {
+                            int midY = (edgeBottomY + y) / 2;
+                            Int16 depthTop = getDepthAt(x, y);
+                            Int16 depthMid = getDepthAt(x, midY);
+                            Int16 depthBottom = getDepthAt(x, edgeBottomY);
+
+                            if ((edgeBottomY - y) < maxFingerSize && depthTop > depthMid && depthMid < depthBottom) {
+                                fingerPoints[x, midY] = true;
                             }
-                            fingerPoints[x, (edgeTopY+edgeBottomY)/2] = true;
+
                         }
+
+                        x = edgeRightX - 1;
                     }
                 }
             }

+ 11 - 5
bbiwarg/Graphics/OutputWindow.cs

@@ -61,14 +61,19 @@ namespace bbiwarg.Graphics
 
         protected override void OnRenderFrame(FrameEventArgs e)
         {
+            
             base.OnRenderFrame(e);
             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
             Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
             GL.MatrixMode(MatrixMode.Modelview);
             GL.LoadMatrix(ref modelview);
-            
+            Stopwatch sw = new Stopwatch();
+            sw.Start();
+
             videoHandle.nextFrame();
 
+            sw.Stop();
+            Console.WriteLine(sw.ElapsedMilliseconds);
             //draw textures
             DepthImage depthImage = videoHandle.getDepthImage();
             Int16 minDepth = 0;// depthImage.getMinDepth();
@@ -91,11 +96,12 @@ namespace bbiwarg.Graphics
 
                     //edgeTexture
                     Int16 edge = depthImage.getEdgeAt(x, y);
+                    Int16 fingerPoint;
+                    if (depthImage.isFingerPoint(x, y)) fingerPoint = Int16.MaxValue;
+                    else fingerPoint = 0;
 
-                    if (depthImage.isFingerPoint(x, y)) edge = Int16.MaxValue / 2;
-
-                    edgeTextureData[index] = edge;
-                    edgeTextureData[index + 1] = edge;
+                    edgeTextureData[index] = fingerPoint;
+                    edgeTextureData[index + 1] = fingerPoint;
                     edgeTextureData[index + 2] = edge;
 
                     index += 3;

+ 1 - 1
bbiwarg/MainBBWIWARG.cs

@@ -12,7 +12,7 @@ namespace bbiwarg
     {
         static void Main(string[] args)
         {
-            IInputProvider inputProvider = new IisuInputProvider("..\\..\\videos\\touch\\4.skv");
+            IInputProvider inputProvider = new IisuInputProvider("..\\..\\videos\\touch\\3.skv");
             VideoHandle videoHandle = new VideoHandle(inputProvider);
 
             OutputWindow output = new OutputWindow(videoHandle);