(十八)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
只展示球体模型上的点