#include <iostream>
#include <iostream>
#include <pcl-1.9/pcl/io/pcd_io.h>
#include <pcl-1.9/pcl/point_types.h>
#include <pcl-1.9/pcl/ModelCoefficients.h>
#include <pcl-1.9/pcl/sample_consensus/method_types.h>
#include <pcl-1.9/pcl/sample_consensus/model_types.h>
#include <pcl-1.9/pcl/segmentation/sac_segmentation.h>
int main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
cloud->width = 15;
cloud->height = 1;
cloud->points.resize (cloud->width * cloud->height);
for (auto& point: *cloud)
{
point.x = 1024 * rand () / (RAND_MAX + 1.0f);
point.y = 1024 * rand () / (RAND_MAX + 1.0f);
point.z = 1.0;
}
(*cloud)[0].z = 2.0;
(*cloud)[3].z = -2.0;
(*cloud)[6].z = 4.0;
pcl::io::savePCDFileASCII("test_pcd.pcd", *cloud);
std::cerr << "Point cloud data: "
<< cloud->size()
<< " points"
<< std::endl;
for (const auto& point: *cloud)
std::cerr << " " << point.x
<< " " << point.y
<< " " << point.z
<< std::endl;
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
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.setDistanceThreshold (0.01);
seg.setInputCloud (cloud);
seg.segment (*inliers, *coefficients);
if (inliers->indices.size () == 0)
{
PCL_ERROR ("Could not estimate a planar model for the given dataset.");
return (-1);
}
std::cerr << "Model coefficients: "
<< coefficients->values[0] << " "
<< coefficients->values[1] << " "
<< coefficients->values[2] << " "
<< coefficients->values[3] << std::endl;
std::cerr << "Model inliers: " << inliers->indices.size () << std::endl;
for (std::size_t i = 0; i < inliers->indices.size (); ++i)
for (const auto& idx: inliers->indices)
std::cerr << idx << " " << cloud->points[idx].x << " "
<< cloud->points[idx].y << " "
<< cloud->points[idx].z << std::endl;
return (0);
}
使用 RAN SAC 方法( pcl: : SAC_RANSAC )作为选择的稳健估计方法,距离阔值是 0.01m ,即只要点到 z=1 平面距离小于该阈值的点都作为内点看待,而大于该阁值的则看做外点。