本教程举例说明了如何为圆柱模型运行样本共识分割。为了使示例更实用,对输入数据集进行以下操作(按顺序):
-
超过 1.5 米的数据点被过滤
-
估计每个点的表面法线
-
平面模型(描述我们演示数据集中的表)被分割并保存到磁盘
-
一个圆柱形模型(描述我们演示数据集中的杯子)被分割并保存到磁盘
下载数据集 https://raw.github.com/PointCloudLibrary/data/master/tutorials/table_scene_mug_stereo_textured.pcd
创建cylinder_segmentation.cpp文件
源码:
1#include <pcl/ModelCoefficients.h>
2#include <pcl/io/pcd_io.h>
3#include <pcl/point_types.h>
4#include <pcl/filters/extract_indices.h>
5#include <pcl/filters/passthrough.h>
6#include <pcl/features/normal_3d.h>
7#include <pcl/sample_consensus/method_types.h>
8#include <pcl/sample_consensus/model_types.h>
9#include <pcl/segmentation/sac_segmentation.h>
10
11typedef pcl::PointXYZ PointT;
12
13int
14main ()
15{
16 // All the objects needed
17 pcl::PCDReader reader;
18 pcl::PassThrough<PointT> pass;
19 pcl::NormalEstimation<PointT, pcl::Normal> ne;
20 pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg;
21 pcl::PCDWriter writer;
22 pcl::ExtractIndices<PointT> extract;
23 pcl::ExtractIndices<pcl::Normal> extract_normals;
24 pcl::search::KdTree<PointT>::Ptr tree (new pcl::search::KdTree<PointT> ());
25
26 // Datasets
27 pcl::PointCloud<PointT>::Ptr cloud (new pcl::PointCloud<PointT>);
28 pcl::PointCloud<PointT>::Ptr cloud_filtered (new pcl::PointCloud<PointT>);
29 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);
30 pcl::PointCloud<PointT>::Ptr cloud_filtered2 (new pcl::PointCloud<PointT>);
31 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2 (new pcl::PointCloud<pcl::Normal>);
32 pcl::ModelCoefficients::Ptr coefficients_plane (new pcl::ModelCoefficients), coefficients_cylinder (new pcl::ModelCoefficients);
33 pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices), inliers_cylinder (new pcl::PointIndices);
34
35 // Read in the cloud data
36 reader.read ("table_scene_mug_stereo_textured.pcd", *cloud);
37 std::cerr << "PointCloud has: " << cloud->size () << " data points." << std::endl;
38
39 // Build a passthrough filter to remove spurious NaNs and scene background
40 pass.setInputCloud (cloud);
41 pass.setFilterFieldName ("z");
42 pass.setFilterLimits (0, 1.5);
43 pass.filter (*cloud_filtered)