1 程序执行结果
实现效果:白色的为提取的点云边界(体素下采样叶节点参数setLeafSize(0.1f,0.1f,0.1f),投影至Z=0平面,concave hull参数为0.3)
实现效果:白色的为提取的点云边界(体素下采样叶节点参数setLeafSize(0.1f,0.1f,0.1f),投影至Z=-1.0平面,concave hull参数为0.7)
2 代码实现
// alpha-concave hull
#include <pcl/io/pcd_io.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/console/time.h>
using namespace std;
typedef pcl::PointXYZ PointT; //C++类型定义,用于简化类型名称
typedef pcl::PointCloud<PointT> PointCloudT;
int main()
{
cout << "Start..." << endl;
PointCloudT::Ptr cloud(new PointCloudT);
string path = ".\\input\\Kuangshan_long_R.pcd";
if (pcl::io::loadPCDFile(path, *cloud) < 0)
{
PCL_ERROR("The point cloud file does not exist.\n");
return -1;
}
cout << "There are " << cloud->points.size() << " points of data." << endl;
//--------------------------点云数据下采样 --------------------------
PointCloudT::Ptr cloud_sub(new PointCloudT); //下采样点云
pcl::VoxelGrid<PointT> vg; //创建体素下采样对象
vg.setInputCloud(cloud); //设置下采样输入点云
vg.setLeafSize(0.1f, 0.1f, 0.1f);//设置体素栅格边长
vg.filter(*cloud_sub); //执行体素下采样
//利用遍历修改某维度值的方法进行投影
for (size_t i = 0; i < cloud_sub->size(); ++i)
{
cloud_sub->points[i].z = 0.0f;//投影至Z=0平面
}
//-------------------- AlphaShapes提取投影边界 凹包算法(ConcaveHull)--------------------
pcl::console::TicToc time;
time.tic();
PointCloudT::Ptr cloud_boundary(new PointCloudT);
pcl::ConcaveHull<PointT> ch;
ch.setInputCloud(cloud_sub);
ch.setAlpha(0.3);
ch.reconstruct(*cloud_boundary);
cout << "This operation takes :" << time.toc() / 1000 << " s(second)" << endl;
//---------------------------- 保存边界点云 --------------------------
if (!cloud_boundary->empty())
{
pcl::io::savePCDFileBinary(".\\output\\0911\\boundary_points.pcd", *cloud_boundary);
cout << "There are a total of " << cloud_boundary->points.size() << " points in the boundary point cloud." << endl;
}
else
{
PCL_ERROR("The boundary point cloud is empty. \a\n");
system("pause");
return -1;
}
cout << "Done!" << endl;
return 0;
}