当前位置: 首页 > news >正文

PCL 计算法向量(MLS)

目录

一、概述

1.1原理

1.2实现步骤

1.3应用场景

二、代码实现

2.1关键函数

2.1.1 computeNormalsUsingMLS

2.1.2 visualizeMLSNormals

2.2完整代码

三、实现效果


PCL点云算法汇总及实战案例汇总的目录地址链接:

PCL点云算法与项目实战案例汇总(长期更新)


一、概述

        在点云处理领域,法向量是描述点云表面方向的关键属性之一。移动最小二乘(MLS, Moving Least Squares)法是一种常用的表面拟合算法,通过对局部邻域的平滑处理,得到点云的法向量。相比于其他方法,MLS法能在一定程度上提高点云法向量计算的精度和抗噪能力。

1.1原理

        移动最小二乘算法的基本思想是,在局部邻域内寻找最适合描述表面的函数,然后通过拟合得到该区域的法向量。MLS法会对点云进行表面平滑处理,这在噪声较大的点云中具有更好的表现。

法向量的计算步骤如下:

  1. 构建局部邻域:对给定点,找到其邻域内的点集。
  2. 拟合平面:在邻域内使用最小二乘法拟合平面。
  3. 法向量计算:平面的法向量即为该点的法向量。

1.2实现步骤

  1. 读取点云数据:从PCD文件中读取点云。
  2. 进行MLS重采样和法向量计算:使用pcl::MovingLeastSquares方法进行点云重采样并计算法向量。
  3. 可视化点云及其法向量:使用PCL的可视化工具显示原始点云和法向量。

1.3应用场景

  1. 表面重建:在3D表面重建中用于估计点云表面的法向量。
  2. 噪声点滤除:通过MLS重采样平滑点云,去除噪声。
  3. 精细化建模:在工业检测、3D扫描等领域,使用MLS可以得到更平滑的模型。

二、代码实现

2.1关键函数

2.1.1 computeNormalsUsingMLS

用于基于MLS算法计算点云的法向量。

// 基于MLS算法计算法向量
void computeNormalsUsingMLS(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals)
{// 创建 MLS 对象并设置参数pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> mls;mls.setInputCloud(cloud);mls.setComputeNormals(true);  // 设置为计算法线mls.setPolynomialOrder(2);    // 多项式拟合阶数mls.setSearchRadius(0.03);    // 搜索半径// 计算 MLS 重采样结果和法线pcl::PointCloud<pcl::PointNormal>::Ptr mls_points(new pcl::PointCloud<pcl::PointNormal>);mls.process(*mls_points);// 提取法向量for (size_t i = 0; i < mls_points->points.size(); ++i){pcl::Normal normal;normal.normal_x = mls_points->points[i].normal_x;normal.normal_y = mls_points->points[i].normal_y;normal.normal_z = mls_points->points[i].normal_z;normals->points.push_back(normal);}
}

2.1.2 visualizeMLSNormals

用于可视化MLS法向量。

// 可视化法向量
void visualizeMLSNormals(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals)
{// 创建可视化对象boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("MLS Normal Viewer"));// 创建第一个视口,显示原始点云int vp_1;viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1);viewer->setBackgroundColor(0.0, 0.0, 0.0, vp_1);viewer->addText("Original Point Cloud", 10, 10, "vp1_text", vp_1);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color(cloud, 255, 0, 0);viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color, "original_cloud", vp_1);// 创建第二个视口,显示法向量int vp_2;viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2);viewer->setBackgroundColor(0.08, 0.08, 0.08, vp_2);viewer->addText("MLS Normals", 10, 10, "vp2_text", vp_2);// 添加法向量,并设置为黑色viewer->addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, normals, 10, 0.02, "normals", vp_2);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler_2(cloud, 0, 255, 0);  // 绿色viewer->addPointCloud(cloud, cloud_color_handler_2, "cloud_with_curvature", vp_2);//viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 0.0, 0.0, "normals", vp_2);  // 设置法向量为黑色// 显示点云while (!viewer->wasStopped()){viewer->spinOnce(100);boost::this_thread::sleep(boost::posix_time::microseconds(100000));}
}

2.2完整代码

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/surface/mls.h>  // 使用 MLS 进行重采样和法向量估计
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/thread/thread.hpp>// 基于MLS算法计算法向量
void computeNormalsUsingMLS(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals)
{// 创建 MLS 对象并设置参数pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> mls;mls.setInputCloud(cloud);mls.setComputeNormals(true);  // 设置为计算法线mls.setPolynomialOrder(2);    // 多项式拟合阶数mls.setSearchRadius(0.03);    // 搜索半径// 计算 MLS 重采样结果和法线pcl::PointCloud<pcl::PointNormal>::Ptr mls_points(new pcl::PointCloud<pcl::PointNormal>);mls.process(*mls_points);// 提取法向量for (size_t i = 0; i < mls_points->points.size(); ++i){pcl::Normal normal;normal.normal_x = mls_points->points[i].normal_x;normal.normal_y = mls_points->points[i].normal_y;normal.normal_z = mls_points->points[i].normal_z;normals->points.push_back(normal);}
}// 可视化法向量
void visualizeMLSNormals(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::Normal>::Ptr normals)
{// 创建可视化对象boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("MLS Normal Viewer"));// 创建第一个视口,显示原始点云int vp_1;viewer->createViewPort(0.0, 0.0, 0.5, 1.0, vp_1);viewer->setBackgroundColor(0.0, 0.0, 0.0, vp_1);viewer->addText("Original Point Cloud", 10, 10, "vp1_text", vp_1);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color(cloud, 255, 0, 0);viewer->addPointCloud<pcl::PointXYZ>(cloud, cloud_color, "original_cloud", vp_1);// 创建第二个视口,显示法向量int vp_2;viewer->createViewPort(0.5, 0.0, 1.0, 1.0, vp_2);viewer->setBackgroundColor(0.08, 0.08, 0.08, vp_2);viewer->addText("MLS Normals", 10, 10, "vp2_text", vp_2);// 添加法向量,并设置为黑色viewer->addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, normals, 10, 0.02, "normals", vp_2);pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler_2(cloud, 0, 255, 0);  // 绿色viewer->addPointCloud(cloud, cloud_color_handler_2, "cloud_with_curvature", vp_2);//viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 0.0, 0.0, 0.0, "normals", vp_2);  // 设置法向量为黑色// 显示点云while (!viewer->wasStopped()){viewer->spinOnce(100);boost::this_thread::sleep(boost::posix_time::microseconds(100000));}
}int main(int argc, char** argv)
{// -------------------------------读取点云--------------------------------------pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);if (pcl::io::loadPCDFile<pcl::PointXYZ>("bunny.pcd", *cloud) == -1){PCL_ERROR("Couldn't read the PCD file\n");return -1;}// ------------------------------计算法向量-------------------------------------pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);computeNormalsUsingMLS(cloud, normals);// -------------------------------可视化---------------------------------------visualizeMLSNormals(cloud, normals);return 0;
}

三、实现效果


http://www.mrgr.cn/news/45500.html

相关文章:

  • git commit失败整理
  • FLINK SQL
  • 修改Docker的默认存储路径
  • 个人常用的正则表达式匹配,以及实际应用
  • 【react】开发常用hooks统计
  • 分析和解决js运算精度问题,出现多位小数
  • 算法闭关修炼百题计划(四)
  • 如何下载和安装CLion,图文详解
  • ZCC40036 过压保护芯片
  • 如何优化电源模块自动化测试的硬件设计?-纳米软件
  • C++ 游戏开发中内存管理进阶
  • 云原生周刊:Docker大涨价|2024.10.8
  • 00.DAMA数据管理知识体系[CDGA及考试相关介绍]
  • Kubernetes v1.31“Elli”:云原生领域的十年里程碑式革新!
  • 【C++】模拟实现hash_table(哈希表)
  • 洛谷新题 U487375 棕兔舞步旋 |【模版】快速输出
  • sql按照指定数组顺序查询数据
  • 编译gcc-8.3
  • 缓存系统的三大挑战:缓存击穿、缓存穿透和缓存雪崩
  • 清华系“仓颉”来袭:图形起源:用AI颠覆字体设计,推动大模型商业化落地