浏览代码

fixed bugs + improvements

Alexander Hendrich 10 年之前
父节点
当前提交
0422eef838

+ 3 - 3
bbiwarg/Constants.cs

@@ -51,8 +51,8 @@ namespace bbiwarg
         public static readonly int DepthImageDepthRange = 200; // <255
 
         // edge image
-        public static readonly int EdgeImageCannyStartThreshold = 80;
-        public static readonly int EdgeImageCannyLinkingThreshold = 60;
+        public static readonly int EdgeImageCannyStartThreshold = 60;
+        public static readonly int EdgeImageCannyLinkingThreshold = 40;
         public static readonly int EdgeImageCannySize = 3;
         public static readonly int EdgeImageRoughNumDilationIterations = 1;
 
@@ -128,7 +128,7 @@ namespace bbiwarg
         public static readonly int TouchTrackerNumFramesLostUntilDeleted = 5;
         public static readonly float TouchmXX = 0.003f;
         public static readonly float TouchmXY = 0.0f;
-        public static readonly float TouchmYY = 0.003f;
+        public static readonly float TouchmYY = 0.0165f;
         public static readonly float TouchProcessNoise = 3.0e-4f;
 
         // touchEventVisualizer

+ 6 - 3
bbiwarg/InputHandler.cs

@@ -279,13 +279,16 @@ namespace bbiwarg
                 OutputImages[2].drawText(h.Centroid.IntX, h.Centroid.IntY, h.TrackID.ToString(), Constants.HandIDColor);
 
                 if (h.ThumbDefect != null)
-                    OutputImages[2].drawDefect(h.ThumbDefect, Color.CornflowerBlue, Color.Yellow);        
+                    OutputImages[2].drawDefect(h.ThumbDefect, Color.CornflowerBlue, Color.Yellow);
             }
 
             //image3
             OutputImages[3].drawImage((depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image.Or(255 - handDetector.HandMask.ThresholdBinary(new Gray(0), new Gray(255))), Constants.DepthImageColor);
-            foreach (TouchEvent te in touchTracker.TouchEvents)
-                OutputImages[3].fillCircle(te.AbsolutePosition.IntX, te.AbsolutePosition.IntY, 5, Constants.TouchEventTrackedColor);
+            foreach (TrackedTouchEvent tte in touchTracker.TrackedObjects)
+            {
+                Vector2D position = tte.AbsolutePositionPrediction;
+                OutputImages[3].fillCircle(position.IntX, position.IntY, 5, Constants.TouchEventTrackedColor);
+            }
             foreach (Palm p in palmTracker.OptimizedPalms)
             {
                 OutputImages[3].drawQuadrangleGrid(p.Quad, Constants.PalmQuadColor, Constants.PalmGridColor, Constants.PalmGridNumRows, Constants.PalmGridNumColumns);

+ 1 - 3
bbiwarg/Recognition/FingerRecognition/Finger.cs

@@ -19,9 +19,7 @@ namespace bbiwarg.Recognition.FingerRecognition
         public Vector2D TipPoint { get { return SliceTrail.StartSlice.Mid; } }
         public Vector2D HandPoint { get { return SliceTrail.EndSlice.Mid; } }
         public Vector2D MidPoint { get { return SliceTrail.MidSlice.Mid; } }
-        public Vector2D Direction { get { return LineSegment.Direction; } }
-        public Vector2D TipDirection { get { return SliceTrail.getStartDirection(); } }
-        public Vector2D HandDirection { get { return SliceTrail.getEndDirection(); } }
+        public Vector2D Direction { get { return SliceTrail.FittedDirection; } }
         public LineSegment2D LineSegment { get { return SliceTrail.LineSegment; } }
         public FingerSliceTrail SliceTrail { get; private set; }
         public Contour<Point> Contour { get { return SliceTrail.Contour; } }

+ 26 - 1
bbiwarg/Recognition/FingerRecognition/FingerSliceTrail.cs

@@ -15,10 +15,12 @@ namespace bbiwarg.Recognition.FingerRecognition
         private Contour<Point> contour;
         private Contour<Point> innerContour;
         private Contour<Point> outerContour;
+        private Vector2D fittedDirection;
         private bool lineSegmentUpToDate;
         private bool contourUpToDate;
         private bool innerContourUpToDate;
         private bool outerContourUpToDate;
+        private bool fittedDirectionUpToDate;
 
         public List<FingerSlice> Slices { get; private set; }
         public FingerSlice StartSlice { get { return Slices[0]; } }
@@ -30,6 +32,7 @@ namespace bbiwarg.Recognition.FingerRecognition
         public Contour<Point> Contour { get { if (!contourUpToDate) updateContour(); return contour; } }
         public Contour<Point> InnerContour { get { if (!innerContourUpToDate) updateInnerContour(); return innerContour; } }
         public Contour<Point> OuterContour { get { if (!outerContourUpToDate) updateOuterContour(); return outerContour; } }
+        public Vector2D FittedDirection { get { if (!fittedDirectionUpToDate) updateFittedDirection(); return fittedDirection; } }
 
         public FingerSliceTrail(FingerSlice slice)
         {
@@ -37,6 +40,7 @@ namespace bbiwarg.Recognition.FingerRecognition
             addSlice(slice);
             lineSegmentUpToDate = false;
             contourUpToDate = false;
+            fittedDirectionUpToDate = false;
         }
 
         public void addSlice(FingerSlice slice)
@@ -44,6 +48,7 @@ namespace bbiwarg.Recognition.FingerRecognition
             Slices.Add(slice);
             lineSegmentUpToDate = false;
             contourUpToDate = false;
+            fittedDirectionUpToDate = false;
         }
 
         public Vector2D getStartDirection()
@@ -63,6 +68,7 @@ namespace bbiwarg.Recognition.FingerRecognition
             Slices.RemoveRange(0, numSlices);
             lineSegmentUpToDate = false;
             contourUpToDate = false;
+            fittedDirectionUpToDate = false;
         }
 
         public void reverse()
@@ -70,14 +76,33 @@ namespace bbiwarg.Recognition.FingerRecognition
             Slices.Reverse();
             lineSegmentUpToDate = false;
             contourUpToDate = false;
+            fittedDirectionUpToDate = false;
         }
 
         private void updateLineSegment()
         {
-            lineSegment = new LineSegment2D(EndSlice.Mid, StartSlice.Mid);
+            //lineSegment = new LineSegment2D(EndSlice.Mid, StartSlice.Mid);
+            float length_2 = 0.5f * (EndSlice.Mid.getDistanceTo(StartSlice.Mid));
+            Vector2D p1 = MidSlice.Mid + length_2 * FittedDirection;
+            Vector2D p2 = MidSlice.Mid - length_2 * FittedDirection;
+            lineSegment = new LineSegment2D(p1, p2);
+
             lineSegmentUpToDate = true;
         }
 
+        private void updateFittedDirection()
+        {
+            List<PointF> midPoints = new List<PointF>();
+            foreach (FingerSlice slice in Slices)
+                midPoints.Add(slice.Mid);
+
+            PointF pointOnLine, direction;
+            PointCollection.Line2DFitting(midPoints.ToArray(), Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_FAIR, out direction, out pointOnLine);
+            fittedDirection = new Vector2D(direction).normalize().getInverse();
+
+            fittedDirectionUpToDate = true;
+        }
+
         private void updateContour()
         {
             List<Point> pointsA = new List<Point>();

+ 3 - 3
bbiwarg/Recognition/HandRecognition/HandDetector.cs

@@ -205,10 +205,10 @@ namespace bbiwarg.Recognition.HandRecognition
             {
                 //get other hands fingers
                 List<Finger> otherFingers = new List<Finger>();
-                foreach (Hand otherHand in Hands)
+                foreach (Finger finger in fingers)
                 {
-                    if (hand != otherHand)
-                        otherFingers.AddRange(otherHand.Fingers);
+                    if (!hand.Fingers.Contains(finger))
+                        otherFingers.Add(finger);
                 }
 
                 hand.fillOverlappingFingers(otherFingers);

+ 13 - 3
bbiwarg/Recognition/PalmRecognition/PalmDetector.cs

@@ -42,7 +42,7 @@ namespace bbiwarg.Recognition.PalmRecognition
             foreach (Hand palmHand in palmHands)
             {
                 ConvexityDefect thumbDefect = palmHand.ThumbDefect;
-
+                /*
                 Vector2D handLength = 1.5f * thumbDefect.VectorLong;
                 Vector2D handWidth = 0.45f * handLength.getOrthogonal(palmHand.Side == HandSide.Right);
 
@@ -50,12 +50,16 @@ namespace bbiwarg.Recognition.PalmRecognition
                 Vector2D wristUpperOld = thumbDefect.Inner - 0.2f * handLength;
                 Vector2D wristLowerOld = wristUpperOld + handWidth;
                 Vector2D fingersLowerOld = wristLowerOld + 0.75f * handLength - 0.3f * handWidth;
+                */
 
                 Vector2D directionWristFinger = thumbDefect.VectorLong.normalize();
                 Vector2D directionFingerWrist = directionWristFinger.getInverse();
                 Vector2D directionUpperLower = directionWristFinger.getOrthogonal(palmHand.Side == HandSide.Right);
                 Vector2D directionLowerUpper = directionUpperLower.getInverse();
 
+                //handLength
+                Vector2D handLength = 1.5f * thumbDefect.VectorLong;
+                
                 //fingersUpper
                 Vector2D fingersUpper = thumbDefect.OuterLong;
                 bool handBelow = true;
@@ -77,11 +81,17 @@ namespace bbiwarg.Recognition.PalmRecognition
                 while (wristUpper.isInBound(Vector2D.Zero, Constants.MaxPixel) && palmHand.isInside(wristUpper))
                     wristUpper += directionFingerWrist;
 
+                //handWidth
+                Vector2D lower = thumbDefect.Inner + 10 * directionUpperLower;
+                while (lower.isInBound(Vector2D.Zero, Constants.MaxPixel) && palmHand.isInside(lower))
+                    lower += directionUpperLower;
+                Vector2D handWidth = (lower - thumbDefect.Inner);
+
                 //wristLower
-                Vector2D wristLower = wristLowerOld;
+                Vector2D wristLower = wristUpper + handWidth;
 
                 //fingersLower
-                Vector2D fingersLower = fingersLowerOld;
+                Vector2D fingersLower = wristLower + 0.75f * handLength - 0.25f * handWidth;
 
 
                 Palm palm = new Palm(palmHand, wristUpper, wristLower, fingersLower, fingersUpper);

+ 1 - 1
bbiwarg/Recognition/TouchRecognition/TouchDetector.cs

@@ -37,7 +37,7 @@ namespace bbiwarg.Recognition.TouchRecognition
             foreach (Finger finger in fingers)
             {
                 Vector2D tipPoint = finger.TipPoint;
-                Vector2D direction = finger.TipDirection;
+                Vector2D direction = finger.Direction;
                 Vector2D directionInv = direction.getInverse();
                 Vector2D tipPointInside = (tipPoint + Constants.TouchTipInsideFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
                 Vector2D tipPointOutside = (tipPoint + Constants.TouchTipOutsideFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);

+ 5 - 5
bbiwarg/Recognition/TouchRecognition/TrackedTouchEvent.cs

@@ -61,15 +61,15 @@ namespace bbiwarg.Recognition.TouchRecognition
 
             if (detectedTouchEvent != null)
             {
-                Vector2D correctedAbsolutePosition = absolutePositionKalman.getCorrectedPosition(detectedTouchEvent.AbsolutePosition);
-                Vector2D correctedRelativePosition = relativePositionKalman.getCorrectedPosition(detectedTouchEvent.RelativePosition);
+                absolutePositionKalman.getCorrectedPosition(detectedTouchEvent.AbsolutePosition);
+                relativePositionKalman.getCorrectedPosition(detectedTouchEvent.RelativePosition);
 
                 if (CurrentState == TrackingState.Tracked)
                 {
                     if (!isTouchActive)
-                        TriggerTouchDown(new TouchEventArgs(ID, correctedAbsolutePosition, correctedRelativePosition));
+                        TriggerTouchDown(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
                     else
-                        TriggerTouchMove(new TouchEventArgs(ID, correctedAbsolutePosition, correctedRelativePosition));
+                        TriggerTouchMove(new TouchEventArgs(ID, AbsolutePositionPrediction, RelativePositionPrediction));
                 }
             }
             else if (isTouchActive && CurrentState == TrackingState.Deleted)
@@ -85,7 +85,7 @@ namespace bbiwarg.Recognition.TouchRecognition
             //absolute position similarity
             float absoluteDistance = detectedTouchEvent.AbsolutePosition.getDistanceTo(AbsolutePositionPrediction);
             float absoluteSimilarity = Math.Max(0, 1 - absoluteDistance / Constants.ImageDiagonalLength);
-            
+
             //relative position similarity
             float relativeDistance = detectedTouchEvent.RelativePosition.getDistanceTo(RelativePositionPrediction);
             float relativeSimilarity = Math.Max(0, 1 - relativeDistance / 1f);