PCL三维重建

贪婪三角投影法

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>  
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/surface/mls.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>


int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::io::loadPCDFile("bunny.pcd", *cloud);

	// 统计滤波
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::StatisticalOutlierRemoval<pcl::PointXYZ> statisOutlierRemoval;
	statisOutlierRemoval.setInputCloud(cloud);
	statisOutlierRemoval.setMeanK(10);
	statisOutlierRemoval.setStddevMulThresh(3.0);
	statisOutlierRemoval.filter(*cloud_filtered);
	pcl::io::savePCDFile("cloud_filtered.pcd", *cloud_filtered);

	// 对点云重采样  
	pcl::search::KdTree<pcl::PointXYZ>::Ptr treeSampling(new pcl::search::KdTree<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_mls(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointXYZ> mls;
	mls.setComputeNormals(true);
	mls.setInputCloud(cloud_filtered);
	mls.setPolynomialOrder(2);
	mls.setSearchMethod(treeSampling);
	mls.setSearchRadius(0.01f);
	mls.process(*cloud_mls);
	pcl::io::savePCDFile("cloud_mls.pcd", *cloud_mls);

	// 法线估计
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
	pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
	tree->setInputCloud(cloud_mls);
	n.setInputCloud(cloud_mls);
	n.setSearchMethod(tree);
	n.setKSearch(10);
	n.compute(*normals);

	//连接字段
	pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
	pcl::concatenateFields(*cloud_mls, *normals, *cloud_with_normals);
	pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
	tree2->setInputCloud(cloud_with_normals);

	// 贪心投影三角化
	pcl::GreedyProjectionTriangulation<pcl::PointNormal> gp3;   // 定义三角化对象
	pcl::PolygonMesh triangles; //存储最终三角化的网络模型
	gp3.setSearchRadius(0.01f);  //设置搜索时的半径,也就是KNN的球半径
	gp3.setMu(3);  //设置样本点搜索其近邻点的最远距离为2.5倍(典型值2.5-3),这样使得算法自适应点云密度的变化
	gp3.setMaximumNearestNeighbors(100);    //设置样本点最多可搜索的邻域个数,典型值是50-100
	gp3.setMinimumAngle(0); //设置三角化后得到的三角形内角的最小的角度
	gp3.setMaximumAngle(M_PI); //设置三角化后得到的三角形内角的最大角度
	gp3.setMaximumSurfaceAngle(M_PI); //设置某点法线方向偏离样本点法线的最大角度,如果超过,连接时不考虑该点
	gp3.setNormalConsistency(false);  //设置该参数为true保证法线朝向一致,设置为false的话不会进行法线一致性检查
	gp3.setInputCloud(cloud_with_normals);     //设置输入点云为有向点云
	gp3.setSearchMethod(tree2);   //设置搜索方式
	gp3.reconstruct(triangles);  //重建提取三角化

	pcl::io::savePLYFile("bunny.ply", triangles);

	return 0;
}

在这里插入图片描述

移动立方体法

#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/marching_cubes_hoppe.h>
#include <pcl/surface/marching_cubes_rbf.h>


int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::io::loadPCDFile("bunny.pcd", *cloud);

	//法线估计
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
	pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
	tree->setInputCloud(cloud);
	n.setInputCloud(cloud);
	n.setSearchMethod(tree);
	n.setKSearch(10);
	n.compute(*normals);

	//连接字段
	pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
	pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);

	//初始化MarchingCubes对象,设置参数
	pcl::MarchingCubes<pcl::PointNormal>* mc = new pcl::MarchingCubesHoppe<pcl::PointNormal>();

	//创建搜索树
	pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
	tree2->setInputCloud(cloud_with_normals);
	mc->setInputCloud(cloud_with_normals);

	//设置MarchingCubes对象的参数
	mc->setIsoLevel(0.001f); //该方法设置要提取表面的iso级别
	mc->setGridResolution(100, 100, 100); //用于设置行进立方体网格分辨率
	mc->setPercentageExtendGrid(0.001f); //该参数定义在点云的边框和网格限制之间的网格内应该保留多少自由空间

	//创建多变形网格,用于存储结果
	pcl::PolygonMesh mesh;
	mc->reconstruct(mesh); //执行重构,结果保存在mesh中
	pcl::io::savePLYFile("bunny.ply", mesh); //保存网格图

	return 0;
}

在这里插入图片描述

泊松重建

#include <iostream>
#include <string>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/surface/poisson.h>
#include <pcl/visualization/pcl_visualizer.h>


int main(int argc, char** argv)
{
	pcl::PointCloud <pcl::PointXYZ> ::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 
	pcl::io::loadPCDFile("bunny.pcd", *cloud);

	// 计算法向量
	pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> n;
	pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
	pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
	tree->setInputCloud(cloud);
	n.setInputCloud(cloud);
	n.setSearchMethod(tree);
	n.setKSearch(10);
	n.compute(*normals); 

	//连接字段
	pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
	pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);

	//创建搜索树
	pcl::search::KdTree<pcl::PointNormal>::Ptr tree2(new pcl::search::KdTree<pcl::PointNormal>);
	tree2->setInputCloud(cloud_with_normals);

	//创建Poisson对象,并设置参数
	pcl::Poisson<pcl::PointNormal> pn;
	pn.setConfidence(true); //是否使用法向量的大小作为置信信息。如果false,所有法向量均归一化。
	pn.setDegree(2); //设置参数degree[1,5],值越大越精细,耗时越久。
	pn.setDepth(8); //树的最大深度,求解2^d x 2^d x 2^d立方体元。由于八叉树自适应采样密度,指定值仅为最大深度。
	pn.setIsoDivide(8); //用于提取ISO等值面的算法的深度
	pn.setManifold(true); //是否添加多边形的重心,当多边形三角化时。 设置流行标志,如果设置为true,则对多边形进行细分三角话时添加重心,设置false则不添加
	pn.setOutputPolygons(true); //是否输出多边形网格(而不是三角化移动立方体的结果)
	pn.setSamplesPerNode(1.0f); //设置落入一个八叉树结点中的样本点的最小数量。无噪声,[1.0-5.0],有噪声[15.-20.]平滑
	pn.setScale(1.0f); //设置用于重构的立方体直径和样本边界立方体直径的比率。
	pn.setSolverDivide(8); //设置求解线性方程组的Gauss-Seidel迭代方法的深度
	pn.setSearchMethod(tree2); //设置搜索方法
	pn.setInputCloud(cloud_with_normals); //设置输入点云

	pcl::PolygonMesh mesh; 	//创建多变形网格,用于存储结果
	pn.performReconstruction(mesh); //执行重构
	pcl::io::savePLYFile("bunny.ply", mesh); //保存网格图

	return 0;
}

在这里插入图片描述

### PCL三维重建方法概述 PCL(Point Cloud Library)是一个开源项目,专注于提供高效、易用的点云处理功能。它支持多种三维重建技术,包括但不限于表面重建、网格化以及可视化等功能。 #### 圆柱体点云生成与泊松表面重建 通过PCL可以轻松生成一个圆柱形状的密集点云,并利用泊松表面重建算法完成高质量的表面重建过程[^1]。以下是具体的实现方式: - **生成圆柱形点云**: 使用随机采样或者基于几何模型的方式创建一组表示圆柱结构的数据点。 - **泊松表面重建**: 调用PCL内置的`pcl::SurfaceReconstruction<PoinT>`类及其子类来执行泊松重建逻辑。此方法能够有效填补点云间的空隙并构建平滑连续的三角网格。 ```cpp #include <pcl/point_cloud.h> #include <pcl/io/pcd_io.h> #include <pcl/surface/poisson.h> int main() { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>()); // 假设已生成圆柱点云存储于cloud变量中 pcl::Poisson<pcl::PointXYZ> poisson; poisson.setDepth(8); // 设置树深度影响细节程度 poisson.setInputCloud(cloud); std::vector<std::uint32_t> indices; std::vector<float> coefficients; poisson.reconstruct(indices, coefficients); } ``` 上述代码片段展示了如何设置参数并通过调用`reconstruct()`函数启动泊松重建流程。 #### 可视化点云计算结果 为了直观查看最终成果,可借助PCL提供的渲染器组件呈现原始点云及经过重建后的网格形态[^4]。下面是一段简单的演示脚本: ```cpp #include <pcl/visualization/cloud_viewer.h> void showCloud(pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud){ pcl::visualization::CloudViewer viewer("Cloud Viewer"); viewer.showCloud(cloud); while (!viewer.wasStopped()) {} } // 将上面得到的结果传递给showCloud函数即可展现出来 ``` 此外,在某些场景下可能还需要考虑法向量估计以便增强视觉表现力[^2]^。这一步骤通常涉及先估算每个顶点处的方向信息再将其附加至目标对象之上。 #### 参数调节建议 针对不同类型的输入数据集,合理配置诸如体素尺寸之类的选项有助于提升整体性能指标[^3]。例如增大分辨率可能会带来更精细的效果但也伴随计算成本上升;反之缩小则能加快速度却牺牲部分精度。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值