点云库PCL学习笔记 -- 点云滤波Filtering -- 7.CropHull 任意多边形内部点云提取
1. CropHull 任意多边形内部点云提取代码
CropHull 任意多边形内部点云提取代码crophull.cpp
#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <vector>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/filters/crop_hull.h>
#include <pcl/surface/concave_hull.h>
int main(int argc, char** argv)
{
//定义并实例化一个PointCloud指针对象,并用输入参数文件进行相关赋值
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PCDReader reader;
reader.read(argv[1],*cloud); //输入滤波对象点云
//输入 2D 平面点云
pcl::PointCloud<pcl::PointXYZ>::Ptr boundingbox_ptr (new pcl::PointCloud<pcl::PointXYZ>);
boundingbox_ptr->push_back(pcl::PointXYZ(0.1, 0.1, 0));
boundingbox_ptr->push_back(pcl::PointXYZ(0.1, -0.1,0 ));
boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, 0.1,0 ));
boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, -0.1,0 ));
boundingbox_ptr->push_back(pcl::PointXYZ(0.15, 0.1,0 ));
pcl::ConvexHull<pcl::PointXYZ> hull; //创建凸包对象
hull.setInputCloud(boundingbox_ptr); //设置输入点云
hull.setDimension(2); //设置凸包维度
std::vector<pcl::Vertices> polygons; //设置 pcl::Vertice 类型的向量,用于保存凸包顶点
//该点云用于描述凸包形状
pcl::PointCloud<pcl::PointXYZ>::Ptr surface_hull (new pcl::PointCloud<pcl::PointXYZ>);
hull.reconstruct(*surface_hull, polygons); //计算 2D 凸包结果
pcl::PointCloud<pcl::PointXYZ>::Ptr objects (new pcl::PointCloud<pcl::PointXYZ>);
pcl::CropHull<pcl::PointXYZ> bb_filter; //创建 CropHull 对象
bb_filter.setDim(2); //设置维度
bb_filter.setInputCloud(cloud); //设置输入需要滤波的点云
bb_filter.setHullIndices(polygons); //输入封闭多边形的顶点
bb_filter.setHullCloud(surface_hull); //输入封闭多边形的形状
bb_filter.filter(*objects); //执行 CropHull 滤波,结果存储到 objects
std::cout << objects->size() << std::endl; //打印输出滤波后的点云数量信息
//可视化视窗界面
boost::shared_ptr<pcl::visualization::PCLVisualizer> for_visualizer_v (new pcl::visualization::PCLVisualizer ("crophull display"));
for_visualizer_v->setBackgroundColor(255,255,255);
//界面视窗1:用于显示输入的点云图像 cloud
int v1(0);
for_visualizer_v->createViewPort (0.0, 0.0, 0.33, 1, v1);
for_visualizer_v->setBackgroundColor (255, 255, 255, v1);
for_visualizer_v->addPointCloud (cloud,"cloud",v1);
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,255,0,0,"cloud");
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,3,"cloud");
for_visualizer_v->addPolygon<pcl::PointXYZ>(surface_hull,0,.069*255,0.2*255,"backview_hull_polyline1",v1);
//界面视窗2:用于封闭多边形的点云 surface_hull
int v2(0);
for_visualizer_v->createViewPort (0.33, 0.0, 0.66, 1, v2);
for_visualizer_v->setBackgroundColor (255, 255, 255, v2);
for_visualizer_v->addPointCloud (surface_hull,"surface_hull",v2);
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,255,0,0,"surface_hull");
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,8,"surface_hull");
for_visualizer_v->addPolygon<pcl::PointXYZ>(surface_hull,0,.069*255,0.2*255,"backview_hull_polyline",v2);
//界面视窗3:用于经过滤波后的点云 objects
int v3(0);
for_visualizer_v->createViewPort (0.66, 0.0, 1, 1, v3);
for_visualizer_v->setBackgroundColor (255, 255, 255, v3);
for_visualizer_v->addPointCloud (objects,"objects",v3);
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,255,0,0,"objects");
for_visualizer_v->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,3,"objects");
while (!for_visualizer_v->wasStopped())
{
for_visualizer_v->spinOnce(1000);
}
system("pause");
}
2. 编译文件
设置编译文件CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(crophull)
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (crophull crophull.cpp)
target_link_libraries (crophull ${PCL_LIBRARIES})
编译
mkdir build
cd build/
cmake ..
make
3. 测试
执行程序
cd ..
./build/crophull pig.pcd
结果如下:
使用pcl_viewer -fc 0,200,200 pig.pcd
查看pig.pcd
文件,如下
(-fc 0,200,200)是设置点云颜色的相关参数:颜色参数为(0,200,200)
- 开始执行程序后
可以清楚的看到多边形对原始点云进行相关的点云信息的提取。