机器学习读书笔记之5 - RANSAC

        随机采样一致性RANdom SAmpleConsensus),是 指从一组包含 “局外点”的数据集中,通过迭代方法估算数学模型的参数。

        RANSAC是一种不确定的算法,它有一定的概率得出一个合理的结果,为了提高概率必须增加迭代次数。

        来看这样一个简单样例,假设我们想拟合一条直线:

        机器学习读书笔记之5 - RANSAC_第1张图片


        可以看到,图中 红色的点是 “局外点”,蓝色的为 “内部点”,很明显,我们想通过不断随机抽取的方式,使得我们选中的点恰好为蓝色点,因为在这种情况下,拟合得到的模型误差最小。

        RANSAC 算法思路非常简单,在模型确定以及最大迭代次数允许的情况下,RANSAC总是能找到最优解。对于包含50%以上误差的数据集,RANSAC的效果远优于直接的最小二乘法。

       RANSAC 应用最多的场景是图片的拼接,基于得到的特征点进行 随机采样匹配

           机器学习读书笔记之5 - RANSAC_第2张图片

        机器学习读书笔记之5 - RANSAC_第3张图片

      关于图片拼接这里不多说,前面的章节有过介绍,可以参考作者之前的博文【全景拼接 】。

       我们来看一段作者搜集的代码(来自于 Ziv Yaniv):

#include   
#include "LineParamEstimator.h"  
  
LineParamEstimator::LineParamEstimator(double delta) : m_deltaSquared(delta*delta) {}  
  
  
void LineParamEstimator::estimate(std::vector &data,std::vector ¶meters)  
{  
    parameters.clear();  
    if(data.size()<2)  
        return;  
    double nx = data[1]->y - data[0]->y;  
    double ny = data[0]->x - data[1]->x;// 原始直线的斜率为K,则法线的斜率为-1/k  
    double norm = sqrt(nx*nx + ny*ny);  
      
    parameters.push_back(nx/norm);  
    parameters.push_back(ny/norm);  
    parameters.push_back(data[0]->x);  
    parameters.push_back(data[0]->y);          
}  
  
  
void LineParamEstimator::leastSquaresEstimate(std::vector &data, std::vector ¶meters)  
{  
    double meanX, meanY, nx, ny, norm;  
    double covMat11, covMat12, covMat21, covMat22; // The entries of the symmetric covarinace matrix  
    int i, dataSize = data.size();  
  
    parameters.clear();  
    if(data.size()<2)  
        return;  
  
    meanX = meanY = 0.0;  
    covMat11 = covMat12 = covMat21 = covMat22 = 0;  
    for(i=0; ix;  
        meanY +=data[i]->y;  
  
        covMat11    +=data[i]->x * data[i]->x;  
        covMat12    +=data[i]->x * data[i]->y;  
        covMat22    +=data[i]->y * data[i]->y;  
    }  
  
    meanX/=dataSize;  
    meanY/=dataSize;  
  
    covMat11 -= dataSize*meanX*meanX;  
        covMat12 -= dataSize*meanX*meanY;  
    covMat22 -= dataSize*meanY*meanY;  
    covMat21 = covMat12;  
  
    if(covMat11<1e-12) {  
        nx = 1.0;  
        ny = 0.0;  
    }  
    else {     //lamda1 is the largest eigen-value of the covariance matrix   
               //and is used to compute the eigne-vector corresponding to the smallest  
               //eigenvalue, which isn't computed explicitly.  
        double lamda1 = (covMat11 + covMat22 + sqrt((covMat11-covMat22)*(covMat11-covMat22) + 4*covMat12*covMat12)) / 2.0;  
        nx = -covMat12;  
        ny = lamda1 - covMat22;  
        norm = sqrt(nx*nx + ny*ny);  
        nx/=norm;  
        ny/=norm;  
    }  
    parameters.push_back(nx);  
    parameters.push_back(ny);  
    parameters.push_back(meanX);  
    parameters.push_back(meanY);  
}  
  
  
bool LineParamEstimator::agree(std::vector ¶meters, Point2D &data)  
{  
    double signedDistance = parameters[0]*(data.x-parameters[2]) + parameters[1]*(data.y-parameters[3]);   
    return ((signedDistance*signedDistance) < m_deltaSquared);  
}  


// RANSAC寻找匹配的代码
template  
double Ransac::compute(std::vector &meters,ParameterEsitmator *paramEstimator,std::vector &data,int numForEstimate)  
{  
    std::vector leastSquaresEstimateData;  
    int numDataObjects = data.size();  
    int numVotesForBest = -1;  
    int *arr = new int[numForEstimate];// numForEstimate表示拟合模型所需要的最少点数,对本例的直线来说,该值为2  
    short *curVotes = new short[numDataObjects];  //one if data[i] agrees with the current model, otherwise zero  
    short *bestVotes = new short[numDataObjects];  //one if data[i] agrees with the best model, otherwise zero  
      
  
    //there are less data objects than the minimum required for an exact fit  
    if(numDataObjects < numForEstimate)   
        return 0;  
    // 计算所有可能的直线,寻找其中误差最小的解。对于100点的直线拟合来说,大约需要100*99*0.5=4950次运算,复杂度无疑是庞大的。一般采用随机选取子集的方式。  
    computeAllChoices(paramEstimator,data,numForEstimate, bestVotes, curVotes, numVotesForBest, 0, data.size(), numForEstimate, 0, arr);  
  
    //compute the least squares estimate using the largest sub set  
    for(int j=0; jleastSquaresEstimate(leastSquaresEstimateData,parameters);  
  
    delete [] arr;  
    delete [] bestVotes;  
    delete [] curVotes;   
  
    return (double)leastSquaresEstimateData.size()/(double)numDataObjects;  
}  

你可能感兴趣的:(机器学习,计算机视觉,机器学习,读书笔记,RANSAC,随机采样一致性,图片拼接)