Browse Source

Hand now knows which side (left or right) it is.

Daniel Kauth 11 years ago
parent
commit
e1d7a954a1

+ 15 - 0
bbiwarg/Detectors/HandDetection/Hand.cs

@@ -12,11 +12,20 @@ namespace bbiwarg.Detectors.HandDetection
 {
     class Hand
     {
+        public enum HandSide
+        {
+            Left,
+            Right
+        }
+
+        public Vector2D Centroid { private set; get; }
+        public HandSide Side { get; set; }
         public Image<Gray, byte> Mask { get; private set; }
         public List<Finger> Fingers { get; private set; }
 
         public Hand(Image<Gray, byte> mask) {
             Mask = mask;
+            Centroid = getCentroid();
             Fingers = new List<Finger>();
         }
 
@@ -27,5 +36,11 @@ namespace bbiwarg.Detectors.HandDetection
         public void addFinger(Finger finger) {
             Fingers.Add(finger);
         }
+
+        private Vector2D getCentroid()
+        {
+            MCvPoint2D64f gravityCenter = Mask.GetMoments(true).GravityCenter;
+            return new Vector2D((float)gravityCenter.x, (float)gravityCenter.y);
+        }
     }
 }

+ 27 - 3
bbiwarg/Detectors/HandDetection/HandDetector.cs

@@ -78,13 +78,37 @@ namespace bbiwarg.Detectors.HandDetection
                     }
                 }
             }
+
+            float minX = float.MaxValue;
+            foreach (Hand hand in Hands)
+            {
+                if (hand.Centroid.X < minX)
+                    minX = hand.Centroid.X;
+            }
+
+            foreach (Hand hand in Hands)
+            {
+                if (hand.Centroid.X == minX)
+                    hand.Side = Hand.HandSide.Left;
+                else
+                    hand.Side = Hand.HandSide.Right;
+            }
         }
 
         private void drawHands() {
-            int maxIndex = Math.Min(3, Hands.Count);
-            for (int i = 0; i < maxIndex ; i++)
+            if (Hands.Count == 1)
+            {
+                outputImage.Image[0] = outputImage.Image[1] = outputImage.Image[2] = Hands[0].Mask * 255;
+            }
+            else if (Hands.Count > 1)
             {
-                outputImage.Image[i] = Hands[i].Mask.Mul(255);
+                for (int i = 0; i < 2; ++i)
+                {
+                    if (Hands[i].Side == Hand.HandSide.Left)
+                        outputImage.Image[0] = outputImage.Image[0] | (255 * Hands[i].Mask);
+                    else
+                        outputImage.Image[1] = outputImage.Image[1] | (255 * Hands[i].Mask);
+                }
             }
         }
     }