PCL点云重建源代码详解:从去噪到重建的完整流程
创作时间:
作者:
@小白创作中心
PCL点云重建源代码详解:从去噪到重建的完整流程
引用
CSDN
1.
https://m.blog.csdn.net/mfc5158/article/details/145856929
本文将介绍如何使用PCL(Point Cloud Library)库实现点云的去噪滤波、平滑、采样和重建。通过本示例,读者可以学习到完整的点云处理流程,包括数据读取、预处理、平滑处理、法线估计、三角化重建等关键步骤。
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/surface/mls.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/gp3.h>
#include <pcl/visualization/pcl_visualizer.h>
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
typedef pcl::PointNormal NormalT;
typedef pcl::PointCloud<NormalT> PointCloudWithNormals;
int main(int argc, char** argv) {
// 1. 读取点云数据
PointCloudT::Ptr cloud(new PointCloudT);
if (pcl::io::loadPLYFile<PointT>("path_to_your_pointcloud.ply", *cloud) == -1) {
std::cerr << "Error loading point cloud" << std::endl;
return -1;
}
// 2. 预处理 - 去噪
pcl::StatisticalOutlierRemoval<PointT> sor;
sor.setInputCloud(cloud);
sor.setMeanK(50); // 考虑邻近50个点
sor.setStddevMulThresh(1.0); // 标准差倍数阈值
sor.filter(*cloud);
// 3. 下采样
pcl::VoxelGrid<PointT> voxel;
voxel.setInputCloud(cloud);
voxel.setLeafSize(0.5f, 0.5f, 0.5f); // 根据点云密度调整
voxel.filter(*cloud);
// 4. 平滑处理 (移动最小二乘法)
pcl::MovingLeastSquares<PointT, NormalT> mls;
pcl::search::KdTree<PointT>::Ptr tree(new pcl::search::KdTree<PointT>);
mls.setInputCloud(cloud);
mls.setSearchMethod(tree);
mls.setSearchRadius(2.0); // 根据点间距调整
mls.setComputeNormals(true);
PointCloudWithNormals::Ptr cloud_smoothed(new PointCloudWithNormals);
mls.process(*cloud_smoothed);
// 5. 法线估计(如果MLS未计算)
pcl::NormalEstimation<PointT, NormalT> ne;
ne.setInputCloud(cloud);
ne.setSearchMethod(tree);
ne.setRadiusSearch(2.0); // 与MLS半径一致
ne.compute(*cloud_smoothed);
// 6. 贪心投影三角化(适合开放边缘)
pcl::GreedyProjectionTriangulation<NormalT> gp3;
pcl::PolygonMesh triangles;
// 设置重建参数
gp3.setSearchRadius(3.0); // 搜索半径(点间距的3-5倍)
gp3.setMu(2.5); // 最大允许距离乘数
gp3.setMaximumNearestNeighbors(100); // 最大邻近点数
gp3.setMaximumSurfaceAngle(M_PI / 2); // 45度
gp3.setMinimumAngle(M_PI / 18); // 10度
gp3.setMaximumAngle(2 * M_PI / 3); // 120度
gp3.setNormalConsistency(true); // 保持法线一致性
gp3.setInputCloud(cloud_smoothed);
gp3.reconstruct(triangles);
// 7. 后处理 - 网格平滑
//pcl::MeshSmoothingLaplacianVTK smoother;
//smoother.setInputMesh(triangles);
//smoother.setNumIterations(50); // 平滑迭代次数
//smoother.process(triangles);
// 8. 保存结果
pcl::io::savePLYFile("output_mesh.ply", triangles);
// 可视化 (可选)
pcl::visualization::PCLVisualizer viewer("Mesh Viewer");
viewer.addPolygonMesh(triangles, "mesh");
while (!viewer.wasStopped()) {
viewer.spinOnce();
}
return 0;
}
热门推荐
用Minecraft还原故宫,你敢挑战吗?
Minecraft玩家热议:高效资源管理新玩法!
春节红包回赠指南:60元的智慧
春节抢红包的心理学解析:为什么我们乐此不疲?
易烊千玺加盟重庆元宵晚会,携手演绎《一礼成人》展现非遗魅力
基于有限元法的平面弹性问题MATLAB求解
舌尖上的非遗:重庆元宵晚会美食大赏
央视元宵晚会:重庆非遗文化的高光时刻
吴文携重庆非遗亮相元宵晚会:20秒“言子”惊艳全国
揭秘二战日本煤炭车:从煤炭到动力的创新与局限
深度解析:剧情背后的心理动机与情感逻辑
日韩如何应对低生育率?政策大比拼!
社交焦虑:你也有这些症状吗?
社交焦虑?这三招让你秒变社交达人!
小学英语微课新玩法:让形容词学习更有趣
《爱情公寓》教你如何在恋爱中正确表达情感
柠檬水美白大法,你get了吗?
徽州卖花渔村:世外桃源里的梅花仙境
黄山市至卖花渔村两日游:秋冬打卡新宠!
秋冬美白神器:蓝莓燕麦饼干&番茄红烧牛肉
吃出好气色!维生素C和A的秘密
吃柠檬真的能变白吗?真相揭秘!
2025年普通人如何快速入门AI?这条路径居然被90%的人忽略了
青团:一颗糯米团子里的千年传承
清明团子DIY:艾草青团超详细教程
类比与比喻:语言魔法师的双重魔法
掌握引用比喻法,让你的文案像大师一样精彩!
《红楼梦》里的比喻艺术大揭秘
二审败诉怎么申诉
DeepSeek描绘青白江:历史与现代交融的华章