【PCL】(十八)随机采样一致性(RANSAC)模型

(十八)RANdom SAmple Consensus(RANSAC)模型

假设我们正在查看的所有数据都由内部值和外部值(异常值)组成,其中内部值可以用一组特定参数值的模型来解释,而异常值则不适合该模型。RANSAC用于估计内部值的数学模型参数。

下面的图片展示了RANSAC算法在二维数据上的简单应用。左图是原始数据。右图中红色点代表异常值,蓝色点代表内部值,蓝线是RANSAC建模的结果。

原始数据RANSAC建模结果

random_sample_consensus.cpp

#include <iostream>
#include <thread>

#include <pcl/console/parse.h>
#include <pcl/point_cloud.h> // for PointCloud
#include <pcl/common/io.h> // for copyPointCloud
#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>

using namespace std::chrono_literals;

pcl::visualization::PCLVisualizer::Ptr
simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
      // --------------------------------------------
      // -----Open 3D viewer and add point cloud-----
      // --------------------------------------------
      pcl::visualization::PCLVisualizer::Ptr 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->addCoordinateSystem (1.0, "global");
      viewer->initCameraParameters ();
      return (viewer);
      }

int main(int argc, char** argv)
{

      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
      pcl::PointCloud<pcl::PointXYZ>::Ptr final (new pcl::PointCloud<pcl::PointXYZ>);
      // 用特定模型生成点云,并随机加入一些异常点
      cloud->width    = 500;
      cloud->height   = 1;
      cloud->is_dense = false;
      cloud->points.resize (cloud->width * cloud->height);
      for (int i = 0; i <cloud->size (); ++i)
      {
            if (pcl::console::find_argument (argc, argv, "-s") >= 0 || pcl::console::find_argument (argc, argv, "-sf") >= 0)
            {
                  (*cloud)[i].x = 1024 * rand () / (RAND_MAX + 1.0);
                  (*cloud)[i].y = 1024 * rand () / (RAND_MAX + 1.0);
                  if (i % 5 == 0)  // 异常点
                        (*cloud)[i].z = 1024 * rand () / (RAND_MAX + 1.0);
                  else if (i % 2 == 0) // 上半球
                        (*cloud)[i].z =  sqrt( 1 - ((*cloud)[i].x * (*cloud)[i].x)
                                                - ((*cloud)[i].y * (*cloud)[i].y));
                  else  // // 下半球
                        (*cloud)[i].z =  - sqrt( 1 - ((*cloud)[i].x * (*cloud)[i].x)
                                                - ((*cloud)[i].y * (*cloud)[i].y));
            }
            else
            {
                  (*cloud)[i].x = 1024 * rand () / (RAND_MAX + 1.0);
                  (*cloud)[i].y = 1024 * rand () / (RAND_MAX + 1.0);
                  if( i % 2 == 0)  // 异常点
                        (*cloud)[i].z = 1024 * rand () / (RAND_MAX + 1.0);
                  else   // 平面
                        (*cloud)[i].z = -1 * ((*cloud)[i].x + (*cloud)[i].y);
            }
      }
      // 用于存储PointCloud中内部点的索引位置
      std::vector<int> inliers;

      // 使用平面或球体模型来构建RandomSampleConsensus对象。
      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);
      }

      // 将内部点复制到final
      pcl::copyPointCloud (*cloud, inliers, *final);

      // 在查看器中显示内部点或完整的点云云。
      pcl::visualization::PCLVisualizer::Ptr 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);
            std::this_thread::sleep_for(100ms);
      }
      return 0;
 }

CMakeLists.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(random_sample_consensus)

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (random_sample_consensus random_sample_consensus.cpp)
target_link_libraries (random_sample_consensus ${PCL_LIBRARIES})

编译并运行

$ ./random_sample_consensus

显示带有异常值的平面PointCloud。

$ ./random_sample_consensus -f

只展示平面模型上的点

在这个程序中还有一个使用球体的例子。如果您使用以下选项运行它:

$ ./random_sample_consensus -s

显示带有异常值的球形PointCloud。

$ ./random_sample_consensus -sf

只展示球体模型上的点

在这里插入图片描述

官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制人工智能

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值