Esempio n. 1
0
        public Matrix <double> FindBestModel(List <KeyValuePair <KeyPoint, KeyPoint> > pairs)
        {
            Matrix <double> bestModel = null;
            int             bestScore = 0;

            for (int i = 0; i < _numIters; i++)
            {
                Matrix <double> model = null;
                ISet <KeyValuePair <KeyPoint, KeyPoint> > samples = new HashSet <KeyValuePair <KeyPoint, KeyPoint> >();
                while (model == null)
                {
                    samples.Add(GetRandomPair(pairs));

                    if (samples.Count == _numSamples)
                    {
                        List <KeyPoint> pointsA = new List <KeyPoint>();
                        List <KeyPoint> pointsB = new List <KeyPoint>();
                        foreach (KeyValuePair <KeyPoint, KeyPoint> pair in samples)
                        {
                            pointsA.Add(pair.Key);
                            pointsB.Add(pair.Value);
                        }
                        List <KeyPoint> points = new List <KeyPoint>();
                        points.AddRange(pointsA);
                        points.AddRange(pointsB);
                        try
                        {
                            model = _transform.ComputeTransform(points);
                        }
                        catch (Exception e)
                        {
                            model   = null;
                            samples = new HashSet <KeyValuePair <KeyPoint, KeyPoint> >(new List <KeyValuePair <KeyPoint, KeyPoint> >(_numSamples));
                        }
                    }
                }

                int score = 0;

                foreach (KeyValuePair <KeyPoint, KeyPoint> pair in pairs)
                {
                    double error = ModelError(pair, model);
                    if (error < MaxError)
                    {
                        score++;
                    }
                }
                if (score > bestScore)
                {
                    bestScore = score;
                    bestModel = model;
                }
            }

            return(bestModel);
        }