本文主要是介绍RANSAC与LSP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近又要用到ransac算法,之前学过也用过,但是好久没用,只知道是干什么用的,现在来重新总结一下,再来复习一遍:
Ransac和最小二乘法都用于模型的最优估计;RANSAC是考虑局部有用的那些数据,取有用数据的模型;LSP考虑的是全局数据,取全局误差最小的模型
一、基础原理:
RANSAC:称为随机抽样一致算法(random sample consensus,RANSAC),采用迭代的方式从一组包含outliers(离群)的被观测数据中估算出最优inliers数学模型。
算法简介:RANSAC算法的基本假设是样本中包含正确数据(inliers,可以被模型描述的数据),也包含异常数据(outliers,偏离正常范围很远、无法适应数学模型的数据),即数据集中含有噪声。这些异常数据可能是由于错误的测量、错误的假设、错误的计算等产生的。同时RANSAC也假设,给定一组正确的数据,存在可以计算出符合这些数据的模型参数的方法。
使用条件:1).一组带噪声的观测数据,其中的inliers数据占了大多数;2).有一个可以解释可信观测数据的参数化模型
二、对比:
最小二乘法(LSP):是从全局出发,考虑所有数据,从所有数据中估计出一个整体误差最小的模型,默认为所有的数据都为inliers,都会参与建模;
RANSAC:算法是从一堆数据中选择出某一个模型,前提是假设这组数据有某种特性,数据里有inliers和outliers,也就是在建模的数据中掺杂着噪声数据,在建模的是时候应该剔除这些噪声数据,仅选择inliers数据来建模,这是和LSP最本质的区别;
假设一组数据符合一个直线模型:y=ax+b,用最小二乘就可以很容易求解出未知参数a和b。
当观测的数据充满了噪声时,比如下面图中的黑点,很明显中间有一条直线,但是用最小二乘去解它,于是出问题啦:
很显然最小二乘失效了,这时候就要用RANSAC去解决它。
RANSAC的算法大致可以表述为(来自wikipedia):
Given:data – a set of observed data pointsmodel – a model that can be fitted to data pointsn – the minimum number of data values required to fit the modelk – the maximum number of iterations allowed in the algorithmt – a threshold value for determining when a data point fits a modeld – the number of close data values required to assert that a model fits well to dataReturn: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 datamaybemodel = model parameters fitted to maybeinliersalsoinliers = empty setfor every point in data not in maybeinliers {if point fits maybemodel with an error smaller than tadd 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 isbettermodel = model parameters fitted to all points in maybeinliers and alsoinliersthiserr = a measure of how well model fits these pointsif thiserr < besterr {bestfit = bettermodelbesterr = thiserr}}increment iterations
}
return bestfit
三、RANSAC应用简介
RANSAC其实就是一种采样方式,例如在图像拼接(Image stitching)技术中:
第一步:预处理后(据说桶形变换,没有去了解过)提取图像特征(如SIFT)
第二步:特征点进行匹配,可利用归一化互相关(NormalizedCross Correlation method, NCC)等方法。
但这个时候会有很多匹配错误的点:
这就好比拟合曲线,有很多的误差点,这个时候就想到了RANSAC算法:我不要再兼顾所有了,每次选取Nums个点匹配→计算匹配后容差范围内的点数→重复实验,迭代结束后,找出点数最多的情况,就是最优的匹配。
利用RANSAC匹配:
第三步:图像拼接,这个就涉及拼接技术了,直接给出结果:
参考:
https://en.wikipedia.org/wiki/Random_sample_consensus
https://blog.csdn.net/zinnc/article/details/52319716
https://www.cnblogs.com/xingshansi/p/6763668.html
https://blog.csdn.net/laobai1015/article/details/51682596
这篇关于RANSAC与LSP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!