Browse Source

added some shortcuts for pixel operations

Alexander Hendrich 10 years ago
parent
commit
85431864bd

+ 11 - 11
bbiwarg/Detectors/Touch/TouchDetector.cs

@@ -30,7 +30,7 @@ namespace bbiwarg.Detectors.Touch
             foreach (Finger finger in fingers) {
                  Vector2D tipPoint = finger.Tip;
 
-                float floodValue = getFloodValue((int)tipPoint.X, (int)tipPoint.Y);
+                float floodValue = getFloodValue(tipPoint);
                 if (floodValue > floodValueThreshold)
                 {
                     //correct touchEvent position
@@ -40,7 +40,7 @@ namespace bbiwarg.Detectors.Touch
                     float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);
                     Vector2D tep = new Vector2D(x,y);
 
-                    touchImage.setTouchAt(tep.IntX, tep.IntY, TouchImageState.touchDetected);
+                    touchImage.setStateAt(tep, TouchImageState.touchDetected);
                     TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
                     touchEvents.Add(touchEvent);
                 }
@@ -51,26 +51,26 @@ namespace bbiwarg.Detectors.Touch
             return touchEvents;
         }
 
-        private float getFloodValue(int touchX, int touchY) {
+        private float getFloodValue(Point touchPoint) {
             int searchSize = 15;
             int maxDepthDifference = 20;
             Int16 fingerDiameter = 5;
-            Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchX, touchY) + fingerDiameter);
+            Int16 depthAtTouch = (Int16) (depthImage.getDepthAt(touchPoint) + fingerDiameter);
 
-            int minX = Math.Max(touchX - searchSize, 0);
-            int maxX = Math.Min(touchX + searchSize, depthImage.getWidth());
-            int minY = Math.Max(touchY - searchSize, 0);
-            int maxY = Math.Min(touchY + searchSize, depthImage.getHeight());
+            int minX = Math.Max(touchPoint.X - searchSize, 0);
+            int maxX = Math.Min(touchPoint.X + searchSize, depthImage.getWidth());
+            int minY = Math.Max(touchPoint.Y - searchSize, 0);
+            int maxY = Math.Min(touchPoint.Y + searchSize, depthImage.getHeight());
 
             int matchedPixels = 0;
             int countedPixels = 0;
             for (int x = minX; x < maxX; x++) {
                 for (int y = minY; y < maxY; y++) {
                     Int16 depth = depthImage.getDepthAt(x,y);
-                    touchImage.setTouchAt(x, y, TouchImageState.touchArea);
+                    touchImage.setStateAt(x, y, TouchImageState.touchArea);
                     if (Math.Abs(depthAtTouch - depth) < maxDepthDifference) {
                         matchedPixels++;
-                        touchImage.setTouchAt(x, y, TouchImageState.touchAreaMatched);
+                        touchImage.setStateAt(x, y, TouchImageState.touchAreaMatched);
                     }
                     countedPixels++;
                 }
@@ -80,7 +80,7 @@ namespace bbiwarg.Detectors.Touch
 
             //status bar (% of matched pixels) -> green
             for (int x = minX; x < minX + (maxX-minX)*rel; x++) {
-                touchImage.setTouchAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
+                touchImage.setStateAt(x, maxY - 1, TouchImageState.touchAreaStatusBar);
             }
 
                 return rel;

+ 1 - 1
bbiwarg/Detectors/Touch/TouchTracker.cs

@@ -49,7 +49,7 @@ namespace bbiwarg.Detectors.Touch
                 }
                 if (tracked)
                 {
-                    touchImage.setTouchAt(te.Position.IntX, te.Position.IntY, TouchImageState.touchTracked);
+                    touchImage.setStateAt(te.Position, TouchImageState.touchTracked);
                     trackedTouchEvents.Add(te);
                     //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
                 }

+ 15 - 2
bbiwarg/Images/DepthImage.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -31,15 +32,27 @@ namespace bbiwarg.Images
 
         }
 
+        public Int16 getDepthAt(Point point) {
+            return getDepthAt(point.X, point.Y);
+        }
+
         public Int16 getDepthAt(int x, int y) {
             return image.Data[y, x, 0];
         }
 
+        public void setDepthAt(Point point, Int16 depth) {
+            setDepthAt(point.X, point.Y, depth);
+        }
+
         public void setDepthAt(int x, int y, Int16 depth) {
             image.Data[y, x, 0] = depth;
         }
 
-        public float getRelativeDepth(int x, int y) {
+        public float getRelativeDepthAt(Point point) {
+            return getRelativeDepthAt(point.X, point.Y);
+        }
+
+        public float getRelativeDepthAt(int x, int y) {
             float minMaxInterval = Math.Max(maxDepth - minDepth, 1);
             return (getDepthAt(x, y)-minDepth) / minMaxInterval;
         }
@@ -85,7 +98,7 @@ namespace bbiwarg.Images
                 {
                     Int16 depth = getDepthAt(x, y);
                     if (depth > max || depth < min)
-                        image.Data[y, x, 0] = max;
+                        setDepthAt(x, y, max);
                 }
             }
         }

+ 9 - 4
bbiwarg/Images/TouchImage.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -31,14 +32,18 @@ namespace bbiwarg.Images
             oldTouches = new List<Vector2D>();
         }
 
-        public void setTouchAt(int x, int y, TouchImageState tis)
+        public void setStateAt(Point point, TouchImageState state) {
+            setStateAt(point.X, point.Y, state);
+        }
+
+        public void setStateAt(int x, int y, TouchImageState state)
         {
-            image.Data[y, x, 0] = (byte)tis;
+            image.Data[y, x, 0] = (byte)state;
 
             int size = 5;
-            if (tis == TouchImageState.touchTracked || tis == TouchImageState.touchDetected)
+            if (state == TouchImageState.touchTracked || state == TouchImageState.touchDetected)
             {
-                image.Draw(new CircleF(new System.Drawing.PointF(x, y), 5), new Gray((byte) tis), 0);
+                image.Draw(new CircleF(new System.Drawing.PointF(x, y), size), new Gray((byte) state), 0);
             }
         }
 

+ 4 - 0
bbiwarg/Utility/Vector2D.cs

@@ -112,5 +112,9 @@ namespace bbiwarg.Utility
             return new PointF(vec.X, vec.Y);
         }
 
+        public static implicit operator Point(Vector2D vec) {
+            return new Point(vec.IntX, vec.IntY);
+        }
+
     }
 }

+ 1 - 1
bbiwarg/VideoHandle.cs

@@ -103,7 +103,7 @@ namespace bbiwarg
         }
 
         public float getRelativeDepth(int x, int y) {
-            return depthImage.getRelativeDepth(x, y);
+            return depthImage.getRelativeDepthAt(x, y);
         }
 
         public bool isEdgeAt(int x, int y) {