RANSAC(Random sample consensus)随机抽样一致性

1.算法介绍

RANSAC(Random Sample Consensus)是一种迭代的参数估计算法,用于从包含噪声和异常值的数据中拟合数学模型。它最初由Fischler和Bolles于1981年提出,被广泛应用于计算机视觉和计算机图形学等领域。

RANSAC的核心思想是随机选择数据中的一小部分样本,并根据这些样本拟合一个模型。然后,通过计算其他数据点到该模型的距离,并将距离小于一定阈值的数据点划分为内点,而距离大于阈值的数据点则划分为外点。重复此过程多次,并选择具有最多内点的模型作为最终的估计结果。

RANSAC算法的优点在于它对于大量异常值和噪声的数据有较好的鲁棒性。它可以估计出包含异常值的数据集中的准确模型,并且不要求事先知道异常值的数量,这种算法常被用于处理具有离群点或噪声的数据。

与最小二乘法相比,RANSAC的主要优点是对异常值的鲁棒性。最小二乘法试图最小化所有数据点的误差平方和,因此对异常值非常敏感。只要有一个数据点远离真实模型,就可能严重影响最小二乘法的结果。而RANSAC通过随机抽样和内点检测机制,能够有效地抵抗异常值的影响。

应用场景:

  • 特征匹配:在图像配准和目标识别中,我们需要匹配两幅图像中的特征点。由于噪声和异常值的存在,直接匹配可能会得到错误的结果。RANSAC可以用于鲁棒地估计特征点之间的映射关系,从而提高匹配的准确性。

  • 三维重建:在立体视觉和结构光扫描中,我们需要从多个视角的图像中重建三维结构。RANSAC可以用于估计相机的运动和场景的几何结构,从而实现鲁棒的三维重建。

  • 运动估计:在视频分析和机器人导航中,我们需要估计物体或相机的运动。RANSAC可以用于从时间序列的图像数据中鲁棒地估计运动参数。

  • 平面检测:在场景理解和物体检测中,我们需要从图像中检测平面。RANSAC可以用于从点云数据中鲁棒地检测平面。

  • 校准:在相机校准和图像拼接中,我们需要估计图像之间的几何变换。RANSAC可以用于鲁棒地估计这些变换,从而实现准确的校准和拼接。

2.实现过程

下面的伪代码来源于维基百科,仔细读一下这份伪代码,它已经把算法的实现过程说的很明确了。

Given:
    data – A set of observations.
    model – A model to explain the observed data points.
    n – The minimum number of data points required to estimate the model parameters.
    k – The maximum number of iterations allowed in the algorithm.
    t – A threshold value to determine data points that are fit well by the model (inlier).
    d – The number of close data points (inliers) required to assert that the model fits well to the data.

Return:
    bestFit – The model parameters which may best fit the data (or null if no good model is found).


iterations = 0
bestFit = null
bestErr = something really large // This parameter is used to sharpen the model parameters to the best data fitting as iterations go on.

while iterations < k do
    maybeInliers := n randomly selected values from data
    maybeModel := model parameters fitted to maybeInliers
    confirmedInliers := empty set
    for every point in data do
        if point fits maybeModel with an error smaller than t then
             add point to confirmedInliers
        end if
    end for
    if the number of elements in confirmedInliers is > d then
        // This implies that we may have found a good model.
        // Now test how good it is.
        betterModel := model parameters fitted to all the points in confirmedInliers
        thisErr := a measure of how well betterModel fits these points
        if thisErr < bestErr then
            bestFit := betterModel
            bestErr := thisErr
        end if
    end if
    increment iterations
end while

return bestFit

3.以直线拟合为例

随机构造一些散点,添加一部分噪声,用RANSAC的算法来拟合这些散点,结果示例如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.直线的描述

假设我们有一个点 P ( x 0 , y 0 ) P(x_0, y_0) P(x0,y0)和一个法向量 N ( a , b ) N(a, b) N(a,b),那么直线的点法式方程可以表示为:

a ( x − x 0 ) + b ( y − y 0 ) = 0 a(x - x_0) + b(y - y_0) = 0 a(xx0)+b(yy0)=0

这个方程的含义是,对于直线上的任意一点 ( x , y ) (x, y) (x,y),它与点 P P P的向量 < P , ( x , y ) > <P, (x, y)> <P,(x,y)>与法向量 N N N是垂直的。因为两个垂直的向量的点积为0,所以有这个等式。
给定两个点 P 1 ( x 1 , y 1 ) , P 2 ( x 2 , y 2 ) P_1(x_1,y_1),P_2(x2,y_2) P1(x1,y1),P2(x2,y2),

  • 直线的方向向量 D D D D = ( x 2 − x 1 , y 2 − y 1 ) D=(x_2-x_1, y_2-y_1) D=(x2x1,y2y1)

  • 直线的法向量 N N N N = ( − ( y 2 − y 1 ) , x 2 − x 1 ) N=(-(y_2-y_1), x_2-x_1) N=((y2y1),x2x1)
    将法向量 N N N和已知 P 1 P1 P1代入上面的方程,得到:

− ( y 2 − y 1 ) ( x − x 1 ) + ( x 2 − x 1 ) ( y − y 1 ) = 0 -(y_2 - y_1)(x - x_1) + (x_2 - x_1)(y - y_1) = 0 (y2y1)(xx1)+(x2x1)(yy1)=0

然后,我们可以将这个方程进一步整理为一般式方程ax + by + c = 0的形式:

( y 1 − y 2 ) x + ( x 2 − x 1 ) y + ( x 1 y 2 − x 2 y 1 ) = 0 (y_1 - y_2)x + (x_2 - x_1)y + (x_1y_2 - x_2y_1) = 0 (y1y2)x+(x2x1)y+(x1y2x2y1)=0

所以,一般式方程的系数 a = y 1 − y 2 , b = x 2 − x 1 , c = x 1 y 2 − x 2 y 1 a = y_1 - y_2,b = x_2 - x_1,c = x_1y_2 - x_2y_1 a=y1y2b=x2x1c=x1y2x2y1
点到直线的距离公式如下:

d = ∣ a x 0 + b y 0 + c ∣ ( a 2 + b 2 ) d = \frac{|ax_0 + by_0 + c|}{ \sqrt{(a^2 + b^2)}} d=(a2+b2) ax0+by0+c
有时候为了方便计算点到直线的距离,会对直线的一般式方程做归一化使得 a 2 + b 2 = 1 a^2+b^2=1 a2+b2=1这样上面的式子中分母项消失,变为 d = ∣ a x 0 + b y 0 + c ∣ d = |ax_0 + by_0 + c| d=ax0+by0+c

5.源码

#include <iostream>
#include <vector>
#include <cmath>

#include "opencv2/opencv.hpp"

// define point
struct Point
{
    float x;
    float y;
};

// define line
struct Line
{
    float a;
    float b;
    float c;
    Line(){};
    Line(Point p1, Point p2)
    {
        a = p2.y - p1.y;
        b = p1.x - p2.x;
        c = p2.x * p1.y - p1.x * p2.y;
        normal_factor = 1.0 / std::sqrt(a * a + b * b);
    }

    Line(float a, float b, float c)
    {
        this->a = a;
        this->b = b;
        this->c = c;
        normal_factor = 1.0 / std::sqrt(a * a + b * b);
    }

    // calculate the distance from a point to a line
    float Distance(const Point &p)
    {
        if (normal_factor < 0.0)
            normal_factor = 1.0 / std::sqrt(a * a + b * b);
        return std::abs(a * p.x + b * p.y + c) * normal_factor;
    }

private:
    float normal_factor = -1.0; // 1 / sqrt(a^2 + b^2)
    // Compute the Euclidean distance from a given point to the line
    // d = |ax + by + c| / sqrt(a^2 + b^2)
};

class MyRansac
{
public:
    MyRansac(){};
    ~MyRansac(){};
    Line FitLine(std::vector<Point> &points, int max_iterations, float distance_threshold)
    {
        Line best_line;
        int best_line_num_inliers = 0;
        for (int i = 0; i < max_iterations; i++)
        {
            // randomly select two points
            int idx1 = rand() % points.size();
            int idx2 = rand() % points.size();
            while (idx1 == idx2)
                idx2 = rand() % points.size();
            Point p1 = points[idx1];
            Point p2 = points[idx2];

            // fit line
            Line line(p1, p2);

            // count inliers
            int num_inliers = 0;
            for (int j = 0; j < points.size(); j++)
            {
                Point p = points[j];
                float distance = line.Distance(p);
                if (distance < distance_threshold)
                    num_inliers++;
            }

            // update best line
            if (num_inliers > best_line_num_inliers)
            {
                best_line_num_inliers = num_inliers;
                best_line = line;
                std::cout << "best_line_num_inliers: " << best_line_num_inliers << std::endl;
            }
        }
        return best_line;
    }
};

class PointFactory
{
public:
    std::vector<Point> CreatePoints(float slope, float intercept, int num_points, float x_min, float x_max, float noise)
    {
        std::vector<Point> points;
        for (int i = 0; i < num_points; i++)
        {
            float x = x_min + (x_max - x_min) * rand() / RAND_MAX;
            // float y = y_min + (y_max - y_min) * rand() / RAND_MAX;
            float y_line = slope * x + intercept;
            float y_noise = y_line + noise * rand() / RAND_MAX;
            Point p;
            p.x = x;
            p.y = y_noise;
            points.push_back(p);
        }

        // add outlier
        int num_outliers = num_points / 10;
        for (int i = 0; i < num_outliers; i++)
        {
            float x = x_min + (x_max - x_min) * rand() / RAND_MAX;
            float y = x_min + (x_max - x_min) * rand() / RAND_MAX;
            Point p;
            p.x = x;
            p.y = y;
            points.push_back(p);
        }
        return points;
    }
};

// visualize points and line
void Visualize(std::vector<Point> &points, Line &line, float distance_threshold = 0.0)
{
    cv::Mat img(500, 500, CV_8UC3, cv::Scalar(255, 255, 255));
    for (int i = 0; i < points.size(); i++)
    {
        Point p = points[i];
        float distance = line.Distance(p);
        if (distance < distance_threshold)
            cv::circle(img, cv::Point(p.x, p.y), 2, cv::Scalar(255, 0, 0), -1);
        else
            cv::circle(img, cv::Point(p.x, p.y), 2, cv::Scalar(0, 0, 255), -1);
    }
    if (std::abs(line.b) > 1e-6)
        cv::line(img, cv::Point(0, -line.c / line.b), cv::Point(img.cols, -(line.a * img.cols + line.c) / line.b), cv::Scalar(0, 255, 0), 2);

    cv::imshow("RANSAC", img);
    cv::waitKey(0);
}

int main(int argc, char *argv[])
{
    std::srand(std::time(nullptr));
    // create points
    PointFactory point_factory;
    float slope = 1;
    float intercept = 10.0;
    int num_points = 100;
    float x_min = 0.0;
    float x_max = 500.0;
    float noise = 50.0;
    std::vector<Point> points = point_factory.CreatePoints(slope, intercept, num_points, x_min, x_max, noise);

    // fit line
    MyRansac ransac;
    float distance_threshold = 20.0;
    int max_iterations = 1000;
    Line line = ransac.FitLine(points, max_iterations, distance_threshold);

    // visualize
    Visualize(points, line, distance_threshold);

    return 0;
}

参考连接

[1] Random sample consensus

  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RANSACRandom Sample Consensus)是一种用于拟合模型并对含有噪声和异常值的数据进行鲁棒估计的算法。它的基本思想是通过迭代的方式从数据集中随机选择一部分样本,然后根据这些样本拟合模型,再根据拟合得到的模型评估其他样本是否符合该模型。通过迭代过程,RANSAC可以找到一个对异常值具有鲁棒性的拟合模型。 RANSAC的步骤如下: 1. 随机选择一部分样本,这部分样本称为内群(inliers)。 2. 根据内群拟合模型,得到模型的参数。 3. 对于剩余的样本,计算其与模型的误差,将误差小于一定阈值的样本称为内群,将误差大于阈值的样本称为离群(outliers)。 4. 判断当前模型的质量,可以通过判断内群的数量或者拟合误差的均值来评估模型的好坏。 5. 重复以上步骤多次,选择拟合误差最小或者内群最多的模型作为最终结果。 RANSAC的优点在于它能够有效地处理含有噪声和异常值的数据,对于不同类型的模型都可以使用。相比于最小二乘法,RANSAC可以捕捉到那些偏离中心的离群点,并将其排除在拟合过程之外,从而得到更准确的结果。因此,在一些对数据质量要求较高的应用中,RANSAC的效果往往优于最小二乘法。 参考资料: 基于OpenCV实现的RANSAC随机抽样一致性直线拟合_thequitesunshine007的博客-CSDN博客 如下图所示,由于最小二乘法拟合数据的时候是考虑所有的数据,所以最小二乘法的误差较大,而RANSAC算法是将那些噪声点设为离群点,就像下图中偏离中心的那些点,所以RANSAC得到的结果准确度较高,所以RANSAC的效果总是远优于最小二乘法。 RANSAC数据分为内群数据和离群数据,离群数据也就是受噪声影响的数据RANSAC假定,给定一组内群,存在一个程序,这个程序可以估算最佳解释或最适用于这一数据模型的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值