# 利用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);
}

## 计算效果

• 本文已收录于以下专栏：

举报原因： 您举报文章：利用RANSAC算法稳健估计平面模型参数和圆柱模型参数 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)