# 利用RANSAC算法稳健估计平面模型参数和圆柱模型参数

## 平面方程参数估计

  //存放局内点索引
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
//创建分割对象
pcl::SACSegmentation<pcl::PointXYZ> seg;
//选择模型系数是否需要优化
seg.setOptimizeCoefficients (true);
//设置模型类型
seg.setModelType (pcl::SACMODEL_PLANE);
//设置模型估计方法
seg.setMethodType (pcl::SAC_RANSAC);
//设置最大迭代次数
seg.setMaxIterations(10000);
//距离阈值（离散点到模型表面距离）
seg.setDistanceThreshold (0.01);
//输入点云
seg.setInputCloud (cloud);
//计算模型参数和得到符合此模型的局内点索引
seg.segment (*inliers, *coefficients);
//输出平面模型的四个参数
std::cerr << "Model coefficients: " << coefficients->values[0] << " "
<< coefficients->values[1] << " "
<< coefficients->values[2] << " "
<< coefficients->values[3] << std::endl;


## 圆柱方程参数估计

  // Create the segmentation object for cylinder segmentation and set all the parameters
seg.setOptimizeCoefficients(true);
seg.setModelType(pcl::SACMODEL_CYLINDER);
seg.setMethodType(pcl::SAC_RANSAC);
seg.setNormalDistanceWeight(0.1);
seg.setMaxIterations(10000);
seg.setDistanceThreshold(0.1);
seg.setInputCloud(cloud);
seg.setInputNormals(cloud_normals);

// Obtain the cylinder inliers and coefficients
seg.segment(*inliers_cylinder, *coefficients_cylinder);
std::cerr << "Cylinder coefficients: " << *coefficients_cylinder << std::endl;

// Write the cylinder inliers to disk
extract.setInputCloud(cloud);
extract.setIndices(inliers_cylinder);
extract.setNegative(false);
pcl::PointCloud<PointT>::Ptr cloud_cylinder(new pcl::PointCloud<PointT>());
extract.filter(*cloud_cylinder);
if (cloud_cylinder->points.empty())
std::cerr << "Can't find the cylindrical component." << std::endl;
else
{
std::cerr << "PointCloud representing the cylindrical component: " << cloud_cylinder->points.size() << " data points." << std::endl;
writer.write("table_scene_mug_stereo_textured_cylinder.pcd", *cloud_cylinder, false);
}

## 计算效果

#### PCL中Sample_consensus模块支持的几何模型

2014-10-31 09:12:17

#### 稳健估计/M估计/最小二乘法

2017-02-15 18:13:29

#### RANSAC算法讲解

2016-11-15 19:51:54

#### ransac平面拟合

2017-06-03 20:27:35

#### 自适应阈值的ransac平面拟合

2017-06-25 16:07:18

#### PCL中RANSAC使用，点云平面检测，显示，存储

2016-11-24 16:35:56

#### PCL—低层次视觉—点云分割（RanSaC）

2018-02-07 19:18:33

#### RANSAC算法详解

2016-06-15 15:19:01

#### PCL: Segmentation模块之SACSegmentation点云分割

2014-09-07 15:02:50

#### pcl 提取子集（平面）

2017-04-01 20:46:40