Consistent random sampling (RANSAC) Principle Analysis

Interview questions: implementation framework of RANSAC

MRPT is a better written, attention needs to be updated after each iteration of this number of iterations. See https://github.com/MRPT/mrpt/blob/master/libs/math/src/ransac.cpp , paper documents in respect of the analysis of RANSAC.

First out about the steps

1. Concentrated Random data is randomly selected from the samples s data, a plurality of model fitting (not collinear between this four samples) is calculated transformation matrix H, denoted as model M;

2. The error is calculated for all projection data set and the data model M, if the error is less than the threshold value, added to the set point I;

3. If the number of elements in the current I is greater than the set point set point within an optimum I_best, update I_best = I, and update iteration number K;

4. If the number of iterations is greater than k, then exit; otherwise, the number of iterations by 1 and repeat the above steps;

Note: In the case where the number of iterations k is not greater than the maximum number of iterations, is constantly updated, rather than fixed;

Wherein, p is the confidence, and generally 0.995; w ratio of "the point"; and m is the minimum number of samples required to calculate the model = 4;

 

Below in connection with the code corresponding to the above-described four steps, the idea of MRPT mentioned program from https://www.peterkovesi.com/matlabfns/index.html , which is a Matlab code, combining two files to see better, I made a comment on the program, and identifies the various steps.

template <typename NUMTYPE>
bool RANSAC_Template<NUMTYPE>::execute(
    const CMatrixDynamic<NUMTYPE>& data, const TRansacFitFunctor& fit_func,
    const TRansacDistanceFunctor& dist_func,
    const TRansacDegenerateFunctor& degen_func, const double distanceThreshold,
    const unsigned int minimumSizeSamplesToFit,
    std::vector<size_t>& out_best_inliers,
    CMatrixDynamic<NUMTYPE>& out_best_model, const double p,
    const size_t maxIter) const
{
    MRPT_START

    ASSERT_(minimumSizeSamplesToFit >= 1);

    // OF reference http://www.csse.uwa.edu.au/ ~ PK / matlab code 
    const size_t data.rows D = ();   //   dimension 
    const size_t npts = data.cols ();

    ASSERT_(D >= 1);
    ASSERT_(Npts > 1);

    const size_t maxDataTrials =
         100 ;   // maximum number of attempts of random sampling of the original data set.

    out_best_model.setSize(
        0 , 0 );   // initialize 
    out_best_inliers.clear ();

    size_t trialcount = 0;
    bestscore size_t = STD :: String :: NPoS;   // number npos will mean the point "none" of the original document is initialized to zero 
    size_t N = . 1 ;   // entire process iterations 

    STD :: Vector <size_t> IND ( ; minimumSizeSamplesToFit) // data sampled random

    while (N > trialcount)
    {
        // Select at random s datapoints to form a trial model, M.
        // In selecting these points we have to check that they are not in
        // a degenerate configuration.
        bool degenerate = true;
        size_t count = 1;
        Vector :: STD <CMatrixDynamic <>> NUMTYPE the MODELS;
         //////////////////////// . 1 Concentrated Random samples randomly selected from the data s, fitting a plurality of models /////////////////////////// / 
        the while (degenerate)
        {
            // generates a random index s in the range 1 to npts 
            ind.resize (minimumSizeSamplesToFit);

            // The +0.99... is due to the floor rounding afterwards when
            // converting from random double samples to size_t
            getRandomGenerator().drawUniformVector(
                ind, 0.0, Npts - 1 + 0.999999);

            // test whether these points are degenerate, if true, the degradation can not 
            degenerate = degen_func (Data, IND);

            if (!degenerate)
            {
            // fit model by a random sampling of the data points, where fitted model many 
                fit_func (data, ind, MODELS) ;

            // model is empty then continue with this process 
                degenerate = MODELS.empty ();
            }

            // number of cycles to determine the maximum is 100 
            IF (COUNT ++> maxDataTrials)
            {
                MRPT_LOG_WARN("Unable to select a nondegenerate data set");
                break;
            }
        }

       // run here have a lot of models, and calculate the distance between the point and the following model is returned bestModelIdx index of the element. 
//////////////////////////////////// // 2 calculates projection errors of all data in the data set and the model M If the error is less than the threshold value, added to the set point of the I; ////////////////////////////// / 
        unsigned int bestModelIdx = 1000 ;
        std::vector<size_t> inliers;
        if (!degenerate)
        {
            dist_func(
                data, MODELS, NUMTYPE(distanceThreshold), bestModelIdx,
                inliers);
            ASSERT_(bestModelIdx < MODELS.size());
        }
        
        // the Find The Number of inliers to the this Model. 
        Const size_t ninliers = inliers.size ();
         BOOL update_estim_num_iters = 
            (trialcount == 0 );   // even the zero point, but also update 
/////// ////////////////////////////////////// 3 If the number of elements in the current I greater than the minimum set point the advantages set I_best, update I_best = I, and update iteration number K; //////////////////////////////// / // 
        IF (ninliers> bestscore || 
            (bestscore == STD :: String :: NPoS ninliers &&! = 0 )) // the number of the points is greater than 0, the assignment to find the optimal model global variables 
        {
            bestscore = ninliers;  // Record data for this model

            out_best_model = MODELS[bestModelIdx];
            out_best_inliers = inliers;
            update_estim_num_iters = true ;
        }
//////////////////////////////////////////////// / 4 If the number of iterations is greater than k, then exit; otherwise, the number of iterations by 1 and repeat the above steps; /////////////////////////// // 
        IF (update_estim_num_iters)
        {
            // update n number of trials, with probability p to make sure we do not select a data set outliers. 
            Double fracinliers = ninliers / static_cast < Double > (npts);
             Double pNoOutliers =
                 . 1 - 
                POW (fracinliers, static_cast < Double > (minimumSizeSamplesToFit));

            pNoOutliers = std::max(
                std::numeric_limits<double>::epsilon(),
                pNoOutliers);   // to avoid negative infinity 
            pNoOutliers = STD :: min (
                 1.0 - :: the numeric_limits STD < Double > :: Epsilon (),
                pNoOutliers);   // to avoid division by zero 
            N = static_cast <size_t> (log ( . 1 - P) / log (pNoOutliers));
            MRPT_LOG_DEBUG(format(
                "Iter #%u Estimated number of iters: %u  pNoOutliers = %f  "
                "#inliers: %u\n",
                (unsigned)trialcount, (unsigned)N, pNoOutliers,
                (unsigned)ninliers));
        }

        ++trialcount;

        MRPT_LOG_DEBUG(format(
            "trial %u out of %u \r", (unsigned int)trialcount,
            (unsigned int)ceil(static_cast<double>(N))));

        // Safeguard against being stuck in this loop forever
        if (trialcount > maxIter)
        {
            MRPT_LOG_WARN(format(
                "Warning: maximum number of trials (%u) reached\n",
                (unsigned)maxIter));
            break;
        }
    }

    if (out_best_model.rows() > 0)
    {  // We got a solution
        MRPT_LOG_INFO(
            format("Finished in %u iterations.\n", (unsigned)trialcount));
        return true;
    }
    else
    {
        MRPT_LOG_WARN("Finished without any proper solution!");
        return false;
    }

    MRPT_END
}

 

Guess you like

Origin www.cnblogs.com/jxLuTech/p/11073972.html