Example #1
2
        public static Image<Bgr, Byte> getBetterImage(List<Bitmap> imgs, TouchPoint2 point)
        {
            int maxNonZero = 0;

            if (imgs.Count == 0)
                return null;

            int x = Convert.ToInt32(point.Bounds.Location.X) - 350;
            if (x < 0)
                x = 0;
            int y = Convert.ToInt32(point.Bounds.Location.Y) - 300;
            if (y < 0)
                y = 0;
            int width = Convert.ToInt32(point.Bounds.Width);
            int height = Convert.ToInt32(point.Bounds.Height);
            System.Drawing.Rectangle r = new System.Drawing.Rectangle(x, y, 400, 400);

            Image<Bgr, Byte> bestImage = null;

            foreach (Bitmap img in imgs)
            {
                Image<Bgr, Byte> bitImg = new Image<Bgr, byte>(img);
                bitImg.ROI = r;
                int count = bitImg.CountNonzero()[0];
                if (count > maxNonZero)
                {
                    maxNonZero = count;
                    bestImage = bitImg;
                }
            }

            return bestImage;
        }
        public static double CalculatePathLength(TouchPoint2 point)
        {
            // Paths generally contain a lot of points, we are skipping some points
            // to improve performance. The 'step' variable decides how much we should skip

            int step = 3;
            double pathLength = 0f;
            StylusPointCollection filteredPoints = removeRedundancy(point.Stroke.StylusPoints);

            int len = filteredPoints.Count;
            if (len > step + 1)
            {
                // Initial point
                StylusPoint p1 = filteredPoints[0];

                for (int i = 1; i < len; i += step)
                {
                    StylusPoint p2 = filteredPoints[i - 1];

                    pathLength += TrigonometricCalculationHelper.GetDistanceBetweenPoints(p1, p2);

                    p1 = p2;
                }
            }

            return pathLength;
        }
 public void DistanceChangedCalculator_Calculate_Test_With_1_Input()
 {
     DistanceChangedCalculator target = new DistanceChangedCalculator();
     TouchPoint2 t2 = new TouchPoint2(new TouchInfo(), new System.Windows.UIElement());
     ValidSetOfTouchPoints set = new ValidSetOfTouchPoints();
     set.Add(t2);
     target.Calculate(set);
 }
Example #4
0
        public static void Add(TouchPoint2 touch)
        {
            if (count < size)
                count++;

            top = top % size;
            _History[top] = touch;
            top++;
        }
Example #5
0
        public static Rect GetBoundingBox(TouchPoint2 point)
        {
            Rect location = point.Stroke.GetBounds();
            location.Height += 60;
            location.Width += 60;
            location.X -= 30;
            location.Y -= 30;

            return location;
        }
Example #6
0
        public HoughCircle(TouchPoint2 p)
        {
            points = p;

            IsCircle = false;
            FindRanges();
            binmax = 0;
            Accumulator = new int[rmax];
            for (int r = 0; r < rmax; r++)
            {
                Accumulator[r] = 0;
            }

            FindCircle();
        }
Example #7
0
 public static bool HasPreviousTouchPoints(Rect location, TouchPoint2 point, int depth, DateTime endTime)
 {
     if (depth == 1)
     {
         return true;
     }
     else
     {
         depth--;
         TouchPoint2 oldTouchPoint = TouchHistoryTracker.GetTouchPoint(location, endTime, point.StartTime);
         if (oldTouchPoint != null)
             return HasPreviousTouchPoints(location, oldTouchPoint, depth, endTime);
         else
             return false;
     }
 }
Example #8
0
        public TouchPoint2 AddNewTouchPoint(TouchInfo info, UIElement source)
        {
            TouchPoint2 newTouchPoint = null;
            if (!ActiveTouchPoints.ContainsKey(info.TouchDeviceId))
            {
                newTouchPoint = new TouchPoint2(info, source);
                ActiveTouchPoints.Add(info.TouchDeviceId, newTouchPoint);
            }
            else
            {
                newTouchPoint = ActiveTouchPoints[info.TouchDeviceId];
                newTouchPoint.Source = source;
            }

            return newTouchPoint;
        }
Example #9
0
        public CircleRecognizer(TouchPoint2 points)
        {
            //Estimate the center (ie. the average: x,y coords)
            double xavg = 0;
            double yavg = 0;
            foreach (var p in points.Stroke.StylusPoints)
            {
                xavg += p.X;
                yavg += p.Y;
            }
            xavg = xavg / points.Stroke.StylusPoints.Count;
            yavg = yavg / points.Stroke.StylusPoints.Count;
            Y_Center = yavg;
            X_Center = xavg;
            StylusPoint center = new StylusPoint(X_Center, Y_Center);

            //Estimate the radius (ie. the avg distance from center)
            Radius = 0;
            foreach (var p in points.Stroke.StylusPoints)
            {
                Radius += TrigonometricCalculationHelper.GetDistanceBetweenPoints(center, p);
            }
            Radius = Radius / points.Stroke.StylusPoints.Count;

            //Calculate the average distance from the estimated circle

            double avgDist = 0;
            foreach (var p in points.Stroke.StylusPoints)
            {
                avgDist += TrigonometricCalculationHelper.GetDistanceBetweenPoints(p, center) - Radius;
            }
            avgDist = avgDist / points.Stroke.StylusPoints.Count;

            //Calculate the 'correlation'

            double residual = 0;
            double total = 0;
            foreach (var p in points.Stroke.StylusPoints)
            {
                double dist = TrigonometricCalculationHelper.GetDistanceBetweenPoints(p,center);
                residual += Math.Pow(dist - Radius, 2);
                total += Math.Pow(dist - avgDist,2);
            }

            R = 1 - (residual / total);
        }
Example #10
0
 public static bool HasPreviousTouchPoints(Rect location, TouchPoint2 point, int depth, DateTime endTime, List<TouchPoint2> selectedPoints)
 {
     if (depth == 0)
     {
         return true;
     }
     else
     {
         depth--;
         TouchPoint2 oldTouchPoint = TouchHistoryTracker.GetTouchPoint(location, endTime, point.StartTime);
         if (oldTouchPoint != null)
         {
             selectedPoints.Add(oldTouchPoint);
             return HasPreviousTouchPoints(location, oldTouchPoint, depth, endTime, selectedPoints);
         }
         else
             return false;
     }
 }
Example #11
0
        // Handles the lists of elements.
        protected CombinatorialBase(TouchPoint2[] listObjects, int nKlass)
        {
            // Check the validity of the arguments
            DoArgumentCheck(listObjects.Length, nKlass);

            m_nKlass = nKlass;

            // Always takes the ZERO (FIRST) dimension of the array. There is no
            // problem in manipulation multidimensional arrays.
            m_nMaxIndex = listObjects.Length - 1;

            m_arrayIndeces = new int[m_nKlass];
            m_arrayIndecesDummy = new int[m_nKlass];

            m_arrayCurrentObj = new TouchPoint2[m_nKlass];

            // Make a shallow copy of the source array.
            //			m_arrayObj = Array.CreateInstance(listObjects[0].GetType(), listObjects.Count);
            m_arrayObj = new TouchPoint2[listObjects.Length];// Array.CreateInstance(Type.GetType("System.Object"), listObjects.Count);
            Array.Copy(listObjects, m_arrayObj, listObjects.Length);
        }
Example #12
0
        private bool IsClosedLoop(TouchPoint2 point)
        {
            double length = TrigonometricCalculationHelper.CalculatePathLength(point);

            if (length < 50)
                return false;
            // Check the distance between start and end point
            if (point.Stroke.StylusPoints.Count > 1)
            {
                StylusPoint firstPoint = point.Stroke.StylusPoints[0];
                StylusPoint lastPoint = point.Stroke.StylusPoints[point.Stroke.StylusPoints.Count - 1];

                double distance = TrigonometricCalculationHelper.GetDistanceBetweenPoints(firstPoint, lastPoint);
                Correlation recognizer = new Correlation(point);
                if (Math.Abs(recognizer.RSquared) < 0.1)
                {
                    if (distance < threshHold)
                    {
                        return true;
                    }
                }

            }
            return false;
        }
Example #13
0
        public Correlation(TouchPoint2 points)
        {
            //Account for a degenerate amount of points
            if (points.Stroke.StylusPoints.Count <= 1)
            {
                RSquared = 0;
                Slope = 0;
                Intercept = 0;
                VerticalLine = false;
                SlopeRad = 0;
                return;
            }

            var pointlist = points.Stroke.StylusPoints;

            //Remove duplicate points
            var workingList = new StylusPointCollection();
            for (int i = 1; i < pointlist.Count; i++ )
            {
                var point1 = pointlist[i - 1];
                var point2 = pointlist[i];
                if (!(point1.X == point2.X && point1.Y == point2.Y))
                {
                    workingList.Add(point1);
                }
            }
            VerticalLine = false;

            xavg = 0;
            yavg = 0;

            foreach (var p in workingList)
            {
                xavg += p.X;
                yavg += p.Y;
            }
            xavg = xavg / workingList.Count;
            yavg = yavg / workingList.Count;

            double numerator = 0;
            double denominator = 0;
            foreach (var p in workingList)
            {
                numerator += (p.X - xavg) * (p.Y - yavg);
                denominator += Math.Pow(p.X - xavg,2);
            }

            SlopeRad = Math.Atan2(numerator, denominator);
            if (denominator != 0)
            {
                Slope = numerator / denominator;
                Intercept = yavg - Slope * xavg;
            }
            else
            {
                VerticalLine = true;
            }
            TouchPoint2 tp = points.GetEmptyCopy();
            if(workingList.Count > 0)
                tp.Stroke.StylusPoints = workingList;
            RSquared = CalculateRSquared(tp);
        }
Example #14
0
        private double CalculateRSquared(TouchPoint2 points)
        {
            if (VerticalLine)
            {
                return 1;
            }
            //Sum of Squares
            double total = 0;
            double residual = 0;

            foreach (var p in points.Stroke.StylusPoints)
            {
                residual += Math.Pow(p.Y - F(p.X),2);
                total += Math.Pow(p.Y - yavg,2);
            }

            return 1 - (residual/total);
        }
Example #15
0
        private DateTime GetEarliestValidTime(TouchPoint2 point)
        {
            DateTime time;

            if (_data.Unit.StartsWith("min"))
                time = point.StartTime.AddMinutes(-_data.TimeLimit);
            else if (_data.Unit.StartsWith("msec"))
                time = point.StartTime.AddMilliseconds(-_data.TimeLimit);
            else // default is seconds unit
                time = point.StartTime.AddSeconds(-_data.TimeLimit);

            return time;
        }
Example #16
0
        private ValidSetOfTouchPoints ValidateCheck(TouchPoint2 point)
        {
            ValidSetOfTouchPoints ret = new ValidSetOfTouchPoints();

            PointTranslator.NoiseReduction = PointTranslator.NoiseReductionType.Low;
            List<TouchPoint2> lines = PointTranslator.FindLines(point);

            if (lines.Count == 2)
            {
                // Calculate angle
                var stylusPoints1 = TrigonometricCalculationHelper.removeRedundancy(lines[0].Stroke.StylusPoints);
                var stylusPoints2 = TrigonometricCalculationHelper.removeRedundancy(lines[1].Stroke.StylusPoints);

                double length1 = TrigonometricCalculationHelper.CalculatePathLength(lines[0]);
                double length2 = TrigonometricCalculationHelper.CalculatePathLength(lines[1]);

                if (length1 > length2)
                    return ret;
                int avg1 = stylusPoints1.Count;
                int avg2 = stylusPoints2.Count;

                double set1Angle = TrigonometricCalculationHelper.GetSlopeBetweenPoints(stylusPoints1[avg1 / 4], stylusPoints1[avg1 * 3 / 4]);
                double set2Angle = TrigonometricCalculationHelper.GetSlopeBetweenPoints(stylusPoints2[avg2 / 4], stylusPoints2[avg2 * 3 / 4]);

                double angularDiff = (set1Angle - set2Angle) * 180 / 3.14;

                if (angularDiff < 0)
                    angularDiff = 180 + angularDiff;

                if (angularDiff > 180)
                    angularDiff = angularDiff - 180;

                if (angularDiff < 100 && angularDiff > 75)
                    ret.Add(point);

            }

            return ret;
        }
        private List<Bitmap> getImageAtTouchTime(TouchPoint2 point)
        {
            List<Bitmap> result = new List<Bitmap>();
            var images = (from imgs in snapshots
                          where
                              imgs.Key.Ticks >= point.StartTime.Ticks && imgs.Key.Ticks <= point.EndTime.Ticks
                          select imgs);

            foreach (KeyValuePair<DateTime, Bitmap> img in images.ToList())
            {
                result.Add(img.Value);
                snapshots.Remove(img.Key);
            }
            return result;
        }
 private ValidSetOfTouchPoints ValidateLine(TouchPoint2 points)
 {
     ValidSetOfTouchPoints ret = new ValidSetOfTouchPoints();
     Correlation recognizer = new Correlation(points);
     if (Math.Abs(recognizer.RSquared) > .1)
     {
         ret.Add(points);
     }
     return ret;
 }
Example #19
0
        public TouchPoint2 GetRange(int index1, int index2)
        {
            if (index1 == index2)
                return this;

            TouchInfo info = new TouchInfo();
            info.ActionType = Action.ToTouchAction();
            info.Position = Position;
            info.TouchDeviceId = TouchDeviceId;
            info.Bounds = Bounds;
            info.IsFinger = isFinger;
            info.Tag = Tag;
            info.Snapshot = Snapshot;

            TouchPoint2 output = new TouchPoint2(info, Source);
            StylusPointCollection spc = new StylusPointCollection();
            for(int i = index1; i < index2; i++)
            {
                spc.Add(Stroke.StylusPoints[i]);
            }
            output.Stroke.StylusPoints = spc;
            return output;
        }
        private bool checkForVariable(TouchPoint2 point, string gestureName)
        {
            string id = PartiallyEvaluatedGestures.getIDFromVariable(_data.VariableMin);
            double length = TrigonometricCalculationHelper.CalculatePathLength(point);

            if (id == "")
                return false;

            if (id == _data.VariableMin)
            {
                // It's the declaration of the variable, just read the lenght and store it there
                PartiallyEvaluatedGestures.addInBuffer(id, length);
                return true;
            }
            else {

              //  List<ValidateBlockResult> firstBlockResults = PartiallyEvaluatedGestures.Get(gestureName, "step1");
            //    ValidSetOfPointsCollection step1 = firstBlockResults[0].Data;
            //    double idValue = TrigonometricCalculationHelper.CalculatePathLength(step1[0][0]);
                double idValue = PartiallyEvaluatedGestures.getValueFromID(id);

              //  double idValue = PartiallyEvaluatedGestures.getValueFromID(id);
                double min = PartiallyEvaluatedGestures.getMultiplierFromVariable(_data.VariableMin);
                double max = PartiallyEvaluatedGestures.getMultiplierFromVariable(_data.VariableMax);
                min = idValue * min;
                max = idValue * max;

                if (max == 0)
                    max = Double.MaxValue;

                if (idValue > 0 && length >= min && length <= max)
                {
                    return true;
                }
                else
                    return false;
            }
        }
Example #21
0
        private void doneRecording()
        {
            AntiUnificator.BreakIntoSteps = (breakGesture.IsChecked == true);

            gdl = AntiUnificator.GDLToPrimitive(points);
            if ((gdl != null)&&(gdl != ""))
            {
                points = null;
                _touchPoint = null;
                TreeViewItem newChild = new TreeViewItem();
                newChild.Header = "name: sample";
                newChild.Items.Add(gdl);
                gestures.Items.Add(newChild);
            }
            button1.Content = "Record gesture";
        }
Example #22
0
        //TODO
        /*    private ValidSetOfTouchPoints ValidateHorizontalLine(TouchPoint2 points)
            {
                ValidSetOfTouchPoints ret = new ValidSetOfTouchPoints();
                Correlation recognizer = new Correlation(points);
                if (Math.Abs(recognizer.RSquared) > TOLERANCE - 0.1)
                {
                    ret.Add(points);
                }
                return ret;
            }*/
        private ValidSetOfTouchPoints ValidateCircle(TouchPoint2 points)
        {
            ValidSetOfTouchPoints ret = new ValidSetOfTouchPoints();
            CircleRecognizer recognizer = new CircleRecognizer(points);
            if (recognizer.R > TOLERANCE + 0.265)
            {
                ret.Add(points);
            }

            return ret;
        }
Example #23
0
        /// <summary>
        /// Return an empty version of this point
        /// </summary>
        public TouchPoint2 GetEmptyCopy()
        {
            TouchInfo info = new TouchInfo();
            info.ActionType = Action.ToTouchAction();
            info.Position = Position;
            info.TouchDeviceId = TouchDeviceId;
            info.Bounds = Bounds;
            info.IsFinger = isFinger;
            info.Tag = Tag;
              //  info.Snapshot = new List<System.Drawing.Bitmap>();

            TouchPoint2 output = new TouchPoint2(info, Source);
            return output;
        }
Example #24
0
 public SingleTouchEventArgs(TouchPoint2 point)
 {
     touchPoint = point;
 }
Example #25
0
        public static void getHandType(out string _type, out string _side, TouchPoint2 point)
        {
            accuracy = 0.9;
            int fingers = ComputeFingersNum(point.Snapshot, out _side);

            if (fingers >= 4)
            {
                _type = TouchHand.OPEN;
                //  _side = TouchHand.RIGHT;
            }
            else if (fingers == 0 && _side == "")
            {
                _type = "";
            }
            else
            {

                _type = TouchHand.VERTICAL;

            }

            //   _side = TouchHand.RIGHT;
        }
        private bool IsValid(TouchPoint2[] arr)
        {
            bool result = false;
            double distanceN = 0f, distanceN_1 = 0f;

            if (arr.Length != 2)
                throw new UnexpectedDataFormatException("Distance can be measured between two points only");

            int firstStrokeLen = arr[0].Stroke.StylusPoints.Count;
            int secondStrokeLen = arr[1].Stroke.StylusPoints.Count;

            //Sometimes we get to much precious data that captures unwanted behaviours...
            //try few times to match the pattern
            for (int n = 1; n < 4; n++)
            {
                if (firstStrokeLen - n < 0 || secondStrokeLen - n < 0)
                    break;

                // Get the point of two gestures at nth position
                StylusPoint p1 = arr[0].Stroke.StylusPoints[firstStrokeLen - n];
                StylusPoint p2 = arr[1].Stroke.StylusPoints[secondStrokeLen - n];
                distanceN = TrigonometricCalculationHelper.GetDistanceBetweenPoints(p1, p2);

                if (_data.Behaviour.ToLower() == BehaviourTypes.Increasing
                    || _data.Behaviour.ToLower() == BehaviourTypes.Decreasing
                    || _data.Behaviour.ToLower() == BehaviourTypes.UnChanged)
                {
                    if (firstStrokeLen - n - 1 < 0 || secondStrokeLen - n - 1 < 0)
                        break;

                    // Get the point of two gestures at (n-1)th position
                    p1 = arr[0].Stroke.StylusPoints[firstStrokeLen - n - 1];
                    p2 = arr[1].Stroke.StylusPoints[secondStrokeLen - n - 1];
                    distanceN_1 = TrigonometricCalculationHelper.GetDistanceBetweenPoints(p1, p2);
                }

                // Validate expected  pattern
                if (_data.Behaviour.ToLower() == BehaviourTypes.Increasing)
                {
                    if (distanceN > distanceN_1)
                    {
                        result = true;
                        break;
                    }
                }
                else if (_data.Behaviour.ToLower() == BehaviourTypes.Decreasing)
                {
                    if (distanceN < distanceN_1)
                    {
                        result = true;
                        break;
                    }
                }
                else if (_data.Behaviour.ToLower() == BehaviourTypes.Range)
                {
                    if (distanceN >= _data.Min && distanceN <= _data.Max)
                    {
                        result = true;
                        break;
                    }
                }
                else if (_data.Behaviour.ToLower() == BehaviourTypes.UnChanged)
                {
                    // Note: Check if the change is within acceptable range
                    // For example: unchanged 10% means if the change is
                    // below 10% of the distance than consider it acceptable

                    double diff = Math.Abs(distanceN - distanceN_1);
                    if (diff < distanceN / _data.Min)
                    {
                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
Example #27
0
 public Combinations(TouchPoint2[] listObjects, int nKlass)
     : base(listObjects, nKlass)
 {
 }
Example #28
0
        private ValidSetOfTouchPoints ValidateBox(TouchPoint2 points)
        {
            ValidSetOfTouchPoints output = new ValidSetOfTouchPoints();
            int length = points.Stroke.StylusPoints.Count;
            if (length < 1)
            {
                return output;
            }

            List<string> slopes = new List<string>();
            TouchPoint2 newPoints = points.GetEmptyCopy();

            for (int i = 0; i < length - 1; i++)
            {
                var point1 = points.Stroke.StylusPoints[i];
                var point2 = points.Stroke.StylusPoints[i + 1];
                double slope = TrigonometricCalculationHelper.GetSlopeBetweenPoints(point1, point2);
                double distance = TrigonometricCalculationHelper.GetDistanceBetweenPoints(point1, point2);
                string stringSlope = TouchPointExtensions.SlopeToDirection(slope);
                if (distance > 0)
                {
                    newPoints.Stroke.StylusPoints.Add(point1);
                    Correlation recognizer = new Correlation(newPoints);
                    if (Math.Abs(recognizer.RSquared) < TOLERANCE + 0.15)
                    {
                        int linelength = newPoints.Stroke.StylusPoints.Count;
                        double lineSlope = TrigonometricCalculationHelper.GetSlopeBetweenPoints(newPoints.Stroke.StylusPoints[1],
                            newPoints.Stroke.StylusPoints[linelength - 1]);
                        string lineStringSlope = TouchPointExtensions.SlopeToDirection(lineSlope);
                        slopes.Add(lineStringSlope);
                        newPoints = newPoints.GetEmptyCopy();
                    }
                }
            }
            RectangleParser parser = new RectangleParser();
            bool hasRect = parser.Advance(slopes);
            if (hasRect)
            {
                output.Add(points);
            }
            return output;
        }
        public void SlopeChangedCalculator_Calculate_With_History_Test()
        {
            TouchInfo ti1 = new TouchInfo();
            ti1.TouchDeviceId = 2;
            ti1.ActionType = TouchAction2.Down;
            ti1.Position = new Point(1, 5);
            TouchPoint2 tp1 = new TouchPoint2(ti1, new UIElement());
            tp1.Stroke.StylusPoints.Add(new System.Windows.Input.StylusPoint());
            tp1.Stroke.StylusPoints.Add(new System.Windows.Input.StylusPoint());

            TouchInfo ti2 = new TouchInfo();
            ti2.TouchDeviceId = 2;
            ti2.ActionType = TouchAction2.Down;
            ti2.Position = new Point(3, 6);
            TouchPoint2 tp2 = new TouchPoint2(ti1, new UIElement());
            tp2.Stroke.StylusPoints.Add(new System.Windows.Input.StylusPoint());
            tp2.Stroke.StylusPoints.Add(new System.Windows.Input.StylusPoint());

            ValidSetOfTouchPoints vp = new ValidSetOfTouchPoints();
            vp.Add(tp1);
            vp.Add(tp2);

            SlopeChangedCalculator sc = new SlopeChangedCalculator();

            SlopeChanged actualP = sc.Calculate(vp) as SlopeChanged;

            double expectedSlope = 0;
            double expectedDelta = 0;

            Assert.IsTrue(expectedDelta == actualP.Delta);
            Assert.AreEqual(expectedSlope, Math.Round(actualP.NewSlope, 2));
        }
        private List<Bitmap> AddSnapshots(TouchPoint2 point)
        {
            List<Bitmap> result = new List<Bitmap>();
            if (!point.isFinger && point.Tag == null)
            {
                List<Bitmap> copy = getImageAtTouchTime(point);
                if (copy == null)
                    return result;

                foreach (Bitmap img in copy)
                {
                    result.Add(img);
                }
            }

            return result;
        }