random_sample_consensus(RANSAC随机抽样一致性)

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

源代码如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

boost::shared_ptr < pcl::visualization::PCLVisualizer>
simpleVis(pcl::PointCloud::PointXYZ>::ConstPtr cloud)
{
    //打开3D窗口并添加点云
    boost::shared_ptr::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::PointXYZ>::Ptr cloud(new pcl::PointCloud::PointXYZ>);
    pcl::PointCloud::PointXYZ>::Ptr final(new pcl::PointCloud::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 inliers;

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

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

    boost::shared_ptr::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;
}

实验结果
random_sample_consensus(RANSAC随机抽样一致性)_第1张图片

你可能感兴趣的:(PCL学习之路)