因项目需求,需提取平面上的物体,不提取平面下的物体,尝试采用超体聚类+LCCP分割的方式,但由于上层点云模型一侧有空洞,导致分割效果不理想。
这里采用pcl::ExtractPolygonalPrismData类,实现平面上物体的提取。
pcl::ExtractPolygonalPrismData类是通过设定处于同一平面模型上的点索引向量,并指定一定高度,利用指定的点形成二维凸包,再结合指定高度生成多边形棱柱模型,分割出该棱柱的内部点集。
1、生成凸包
- pcl::ConvexHull<pcl::PointXYZ> hull;
- hull.setInputCloud(plane);
- //重构的维度,若设置为2,则表示计算平面上的凸包
- hull.setDimension(2);
- //执行凸包重构,将结果存储在convexHull指向的点云中
- hull.reconstruct(*convexHull);
2、构建棱柱模型及分割
- // 该类用于分割出棱柱模型内部的点集
- pcl::ExtractPolygonalPrismData<pcl::PointXYZ> prism;
- prism.setInputCloud(cloud);
- // 设置平面模型的点集
- prism.setInputPlanarHull(convexHull);
- // 设置高度范围
- prism.setHeightLimits(35.0f, 500.0f);
- pcl::PointIndices::Ptr objectIndices(new pcl::PointIndices);
- prism.segment(*objectIndices);
- // 分割目标物
- extract.setIndices(objectIndices);
- extract.filter(*objects);
完整示例代码:
- #include <pcl/io/pcd_io.h>
- #include <pcl/sample_consensus/method_types.h>
- #include <pcl/sample_consensus/model_types.h>
- #include <pcl/segmentation/sac_segmentation.h>
- #include <pcl/filters/extract_indices.h>
- #include <pcl/surface/convex_hull.h>
- #include <pcl/segmentation/extract_polygonal_prism_data.h>
- #include <pcl/visualization/cloud_viewer.h>
-
- #include <iostream>
-
- int
- main(int argc, char** argv)
- {
- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
- pcl::PointCloud<pcl::PointXYZ>::Ptr plane(new pcl::PointCloud<pcl::PointXYZ>);
- pcl::PointCloud<pcl::PointXYZ>::Ptr convexHull(new pcl::PointCloud<pcl::PointXYZ>);
- pcl::PointCloud<pcl::PointXYZ>::Ptr objects(new pcl::PointCloud<pcl::PointXYZ>);
-
- if (pcl::io::loadPCDFile<pcl::PointXYZ>("..\\testdata\\result\\cylinder.pcd", *cloud) != 0)
- {
- return -1;
- }
-
- pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
-
- // 采样一致性算法实现的分割类
- pcl::SACSegmentation<pcl::PointXYZ> segmentation;
- segmentation.setInputCloud(cloud);
- // 设置构造的几何模型类型
- segmentation.setModelType(pcl::SACMODEL_PLANE);
- // 设置采样一致性方法类型
- segmentation.setMethodType(pcl::SAC_RANSAC);
- // 设置点到模型的距离阈值
- segmentation.setDistanceThreshold(10);
- // 设置模型参数优化
- segmentation.setOptimizeCoefficients(true);
- pcl::PointIndices::Ptr planeIndices(new pcl::PointIndices);
- segmentation.segment(*planeIndices, *coefficients);
-
- if (planeIndices->indices.size() == 0)
- std::cout << "Could not find a plane in the scene." << std::endl;
- else
- {
- // Copy the points of the plane to a new cloud.
- pcl::ExtractIndices<pcl::PointXYZ> extract;
- extract.setInputCloud(cloud);
- extract.setIndices(planeIndices);
- extract.filter(*plane);
-
- // 保存平面点云
- pcl::io::savePCDFile("..\\testdata\\result\\data\\plane.pcd", *plane);
-
- // 生成凸包
- pcl::ConvexHull<pcl::PointXYZ> hull;
- hull.setInputCloud(plane);
- //重构的维度,若设置为2,则表示计算平面上的凸包
- hull.setDimension(2);
- hull.reconstruct(*convexHull);
-
- //pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler(cloud, 255, 188, 255);
- //pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
- viewer.addPointCloud<pcl::PointXYZ>(cloud, "原始点云");
- //viewer.addPointCloud<pcl::PointXYZ>(convexHull, color_handler, "凸包点云");
- //viewer.spin();
-
- // 冗余检查.检查凸包的维度是否为2
- if (hull.getDimension() == 2)
- {
- // 该类用于分割出棱柱模型内部的点集
- pcl::ExtractPolygonalPrismData<pcl::PointXYZ> prism;
- prism.setInputCloud(cloud);
- // 设置平面模型的点集
- prism.setInputPlanarHull(convexHull);
-
- // 设置高度范围
- prism.setHeightLimits(35.0f, 500.0f);
- pcl::PointIndices::Ptr objectIndices(new pcl::PointIndices);
-
- prism.segment(*objectIndices);
-
- // Get and show all points retrieved by the hull.
- extract.setIndices(objectIndices);
- extract.filter(*objects);
- //可视化
- pcl::visualization::CloudViewer viewerObjects("Objects on table");
- viewerObjects.showCloud(objects);
- pcl::io::savePCDFile("..\\testdata\\result\\data\\objects.pcd", *objects);
- while (!viewerObjects.wasStopped())
- {
- // Do nothing but wait.
- }
- }
- else std::cout << "The chosen hull is not planar." << std::endl;
- }
- return 0;
- }
运行效果:

构建三维凸包,分割效果不理想,更换二维凸包后,效果较好。
