pcl::ExtractPolygonalPrismData类

来源:互联网 发布:淘宝上井江茶油造假 编辑:程序博客网 时间:2024/06/14 18:08

ExtractPolygonalPrismData通过设定处于同一平面模型上的点索引向量,并指定一定高度,利用指定的点形成二维凸包,再结合指定高度一起生成一个多边形棱柱模型,该类用于分割出该棱柱模型的内部点集,比如分割出平面上的物体。下面用一个例子演示其用法。

#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>intmain(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>("table_scene_mug_stereo_textured.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(0.01);    // 设置模型参数优化    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("plane.pcd",*plane);        // 生成凸包        pcl::ConvexHull<pcl::PointXYZ> hull;        hull.setInputCloud(plane);        hull.setDimension(2);        hull.reconstruct(*convexHull);        // 冗余检查.        if (hull.getDimension() == 2)        {            // 该类用于分割出棱柱模型内部的点集            pcl::ExtractPolygonalPrismData<pcl::PointXYZ> prism;            prism.setInputCloud(cloud);            // 设置平面模型的点集            prism.setInputPlanarHull(convexHull);            // 设置高度范围            prism.setHeightLimits(0.01f, 0.4f);            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("objects.pcd", *objects);            while (!viewerObjects.wasStopped())            {                // Do nothing but wait.            }        }        else std::cout << "The chosen hull is not planar." << std::endl;    }}

线面是分割的效果图
原始图
分割的平面效果图
这里写图片描述
分割出桌面上的杯子
这里写图片描述