1. 程式人生 > >看懂Ransac,一篇就夠了

看懂Ransac,一篇就夠了

隨機取樣一致性(RANSAC)
和一般的最小二乘擬合的區別是:RANSAC可以在有大量噪聲的情況下擬合出令人滿意的效果。
演算法步驟如下:
首先從輸入的資料中隨機選擇一些點並計算使用者給定模型的引數,對資料集中的所有點設定距離閾值,如果點到模型的距離在距離閾值的範圍內,則將該點歸為局內點,否則為局外點,然後統計所有局內點的個數,判斷是否大於設定的閾值,如果是,則用內殿重新估計模型,作為模型輸出,儲存所有內點作為分割結果,如果不是,則與當前最大的內點個數對比,如果大於則取代當前最大局內點個數,並存儲當前的模型係數,然後進行迭代計算,直到分割出使用者滿意的模型。
下面上偽碼:

Given:
    data – a set of observed data points
    model – a model that can be fitted to data points
    n – the minimum number of data values required to fit the model
    k – the maximum number of iterations allowed in the algorithm
    t – a threshold value for determining when a data point fits a model
    d – the number of close data values required to assert that a model fits well to data

Return:
    bestfit – model parameters which best fit the data (or nul if no good model is found)

iterations = 0
bestfit = nul
besterr = something really large
while iterations < k {
    maybeinliers = n randomly selected values from data
    maybemodel = model parameters fitted to maybeinliers
    alsoinliers = empty set
    for every point in data not in maybeinliers {
        if point fits maybemodel with an error smaller than t
             add point to alsoinliers
    }
    if the number of elements in alsoinliers is > d {
        % this implies that we may have found a good model
        % now test how good it is
        bettermodel = model parameters fitted to all points in maybeinliers and alsoinliers
        thiserr = a measure of how well model fits these points
        if thiserr < besterr {
            bestfit = bettermodel
            besterr = thiserr
        }
    }
    increment iterations
}
return bestfit

RANSAC的缺點是它計算引數的迭代次數沒有上限