random_sample_consensus(RANSAC随机抽样一致性)

首先说明一下随机算法(RANSAC):
RANSAC算法:是使用一个比较小的数据集,然后再尽可能的使用一致的数据来扩大原来初始化的数据集。
简单的说就是:我们要要拟合一段二维点中的弧线,RANSAC会选择三个点作为一个集合,然后计算中心和半径,也就是说这样圆的弧线就基本确定了。

源代码如下:

#include<iostream>
#include<pcl/filters/extract_indices.h>
#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
#include<pcl/sample_consensus/ransac.h>
#include<pcl/sample_consensus/sac_model_plane.h>
#include<pcl/sample_consensus/sac_model_sphere.h>
#include<pcl/visualization/pcl_visualizer.h>
#include<boost/thread/thread.hpp>
#include<pcl/console/parse.h>

boost::shared_ptr < pcl::visualization::PCLVisualizer>
simpleVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
    //打开3D窗口并添加点云
    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer->setBackgroundColor(0, 0, 0);
    viewer->addPointCloud<pcl::PointXYZ>(cloud, "sample cloud");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
    viewer->initCameraParameters();
    return (viewer);
}

int main(int argc, char ** argv)
{
    srand(time(NULL));
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr final(new pcl::PointCloud<pcl::PointXYZ>);

    cloud->width = 5000;
    cloud->height = 1;
    cloud->is_dense = false;
    cloud->points.resize(cloud->width * cloud->height);

    for (size_t i = 0; i < cloud->points.size(); ++i)
    {
        if (pcl::console::find_argument(argc, argv, "-s") >= 0 || pcl::console::find_argument(argc, argv, "-sf") >= 0)
        {
            cloud->points[i].x = rand() / (RAND_MAX + 1.0);
            cloud->points[i].y = rand() / (RAND_MAX + 1.0);
            if (i % 5 == 0)
                cloud->points[i].z = rand() / (RAND_MAX + 1.0);
            else if (i % 2 == 0)
                cloud->points[i].z = sqrt(1 - (cloud->points[i].x * cloud->points[i].x)
                    - (cloud->points[i].y * cloud->points[i].y));
            else
                cloud->points[i].z = -sqrt(1 - (cloud->points[i].x * cloud->points[i].x)
                    - (cloud->points[i].y * cloud->points[i].y));
        }
        else
        {
            cloud->points[i].x = rand() / (RAND_MAX + 1.0);
            cloud->points[i].y = rand() / (RAND_MAX + 1.0);
            if (i % 5 == 0)
                cloud->points[i].z = rand() / (RAND_MAX + 1.0);
            else
                cloud->points[i].z = -1 * (cloud->points[i].x + cloud->points[i].y);
        }
    }

    std::vector<int> inliers;

    //创建对象和模拟计算随机抽样一致性
    pcl::SampleConsensusModelSphere<pcl::PointXYZ>::Ptr
        model_s(new pcl::SampleConsensusModelSphere<pcl::PointXYZ>(cloud));
    pcl::SampleConsensusModelPlane<pcl::PointXYZ>::Ptr
        model_p(new pcl::SampleConsensusModelPlane<pcl::PointXYZ>(cloud));
    if (pcl::console::find_argument(argc, argv, "-f") >= 0)
    {
        pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_p);
        ransac.setDistanceThreshold(.01);
        ransac.computeModel();
        ransac.getInliers(inliers);
    }
    else  if (pcl::console::find_argument(argc, argv, "-sf") >= 0)
    {
        pcl::RandomSampleConsensus<pcl::PointXYZ> ransac(model_s);
        ransac.setDistanceThreshold(.01);
        ransac.computeModel();
        ransac.getInliers(inliers);
    }

    pcl::copyPointCloud<pcl::PointXYZ>(*cloud, inliers, *final);

    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
    if (pcl::console::find_argument(argc, argv, "-f") >= 0 || pcl::console::find_argument(argc, argv, "-sf") >= 0)
        viewer = simpleVis(final);
    else
        viewer = simpleVis(cloud);
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }
    return 0;
}

实验结果
这里写图片描述

阅读更多
文章标签: 算法 数据
个人分类: PCL学习之路
想对作者说点什么? 我来说一句

随机抽样一致性算法(matlab)

2012年05月18日 707KB 下载

没有更多推荐了,返回首页

不良信息举报

random_sample_consensus(RANSAC随机抽样一致性)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭