PCL估计点云的表面法向量

估计点云表面法向量的方法

  1. 根据点云中点及其邻域信息,构建点云表面mesh网格,然后mesh网格计算曲面法线
  2. 使用近似值直接从点云数据集中推断出曲面法线
    PCL中点云表面法向量的实现是基于后者的,即在给定点云数据集的情况下,直接计算点云中每个点的表面法线。

理论基础

法线确定方法

确定表面上点的法线问题可以通过估计与表面相切的平面的法线问题来近似,从而又可以转化为最小二乘平面拟合估计问题。
因为点云中的点是三维的,要估计的平面是二维的,用三维数据估计二维数据,很容易想到的方法就是降维,而一提起降维,最容易想到的方法就是PCA,关于PCA的理论及代码实现,请参考我的文章PCA(主成分分析)降维原理及其在optdigits以及点云数据集上的python实现

因此,估计表面法线问题被简化为对由待估计点的最近邻生成的协方差矩阵的特征向量和特征值的分析。 更具体地说,对于每个点pi,我们如下构建它的协方差矩阵C:
在这里插入图片描述
其中k是点pi的k个相邻点,这些相邻点可以用最近邻方法确定,也可以指定半径确定,这在文章后面还会讨论到; p_bar表示临近点的3D质心; lamdaj表示协方差矩阵的第j个特征值,vector vj表示协方差矩阵的第j个特征向量。

可以很容易看出,pi和p_bar的shape都是(3,1),所以协方差矩阵C的shape是(3,3)
要从一组点中估计协方差矩阵,在PCL中实现方式为:

  // Placeholder for the 3x3 covariance matrix at each surface patch
  Eigen::Matrix3f covariance_matrix;
  // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
  Eigen::Vector4f xyz_centroid;

  // Estimate the XYZ centroid
  compute3DCentroid (cloud, xyz_centroid);

  // Compute the 3x3 covariance matrix
  computeCovarianceMatrix (cloud, xyz_centroid, covariance_matrix);

法线方向确定

PCA可以很好的实现降维,找到切平面,但是并不能确定切平面的法线方向。
解决这一问题的方法是引入一个视点约束,假设引入一个视点vp,为了使所有法线vector ni始终朝向视点的一面,它们需要满足如下不等式约束:
在这里插入图片描述
若要在PCL中手动重新定向给定法线,可以用如下代码:

flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z, Eigen::Vector4f &normal);

选择合适的邻域尺度

如前所述,某一点的法线估计离不开其邻域点的约束。但是,给定点云数据,要确定某一点的邻域点集,需要确定合理的k(通过pcl::Feature::setKSearch给出)或r(通过pcl::Feature:: setRadiusSearch)给出,从而使得法线估计取得理想的效果,这就是邻域尺度问题。

该问题极其重要,是默认参数点特征估计的限制因素。为了更好地说明这个问题,下图比较了合理的尺度与偏大尺度的效果。左图是合理尺度的表面法线估计结果,可以看到,算法估计的表面法线分别与两个平面大致垂直,并且在整个桌子上可以看到小边缘。右图是偏大尺度的表面法线估计结果,因为比例尺度偏大,邻近点集覆盖了更多来自相邻表面的点,估计的法线变得失真,在两个平坦表面的边缘处表面法线出现明显旋转,并且边缘出现模糊、很多细节被抑制。
在这里插入图片描述在这里插入图片描述
尺度因子的选择需要根据应用程序所需的详细程度来确定。 简单地说,如果杯子的手柄和圆柱形部分之间的边缘处的法线很重要,则比例因子需要足够小以捕获那些细节,否则设置成较大比例因子比较合适。

PCL估计表面法线代码实现

估算输入数据集中所有点的曲面法线:

#include <pcl/point_types.h>
#include <pcl/features/normal_3d.h>

{
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);

  ... read, pass in or create a point cloud ...

  // Create the normal estimation class, and pass the input dataset to it
  pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
  ne.setInputCloud (cloud);

  // Create an empty kdtree representation, and pass it to the normal estimation object.
  // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
  ne.setSearchMethod (tree);

  // Output datasets
  pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);

  // Use all neighbors in a sphere of radius 3cm
  ne.setRadiusSearch (0.03);

  // Compute the features
  ne.compute (*cloud_normals);

  // cloud_normals->points.size () should have the same size as the input cloud->points.size ()*
}

ne.compute()方法实际上完成了以下的工作:

for each point p in cloud P

  1. get the nearest neighbors of p

  2. compute the surface normal n of p

  3. check if n is consistently oriented towards the viewpoint and flip otherwise

默认的视点坐标是(0,0,0),可以通过如下代码指定:

setViewPoint (float vpx, float vpy, float vpz);

如果只需要计算某一个单点的法线,可以用下面的代码:

computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices, Eigen::Vector4f &plane_parameters, float &curvature);

其中cloud是包含点的输入点云,indices表示来自点云的k-最近邻集合,plane_parameters和curvature表示法线估计的输出,plane_parameters保存法线(nx,ny,nz)的前3个坐标,第四个坐标是D = nc = p_plane(这里是质心)+ p。 输出表面curvature被估计为协方差矩阵的特征值之间的关系,如下所示:
在这里插入图片描述

用OpenMP加速法线估计

PCL提供了表面法线估计的加速实现,基于OpenMP使用多核/多线程来加速计算。 该类的名称是pcl :: NormalEstimationOMP,其API与单线程pcl :: NormalEstimation 100%兼容。 在具有8个内核的系统上,一般计算时间可以加快6-8倍。
示例代码:

#include <pcl/point_types.h>
#include <pcl/features/normal_3d_omp.h>

{
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);

  ... read, pass in or create a point cloud ...

  // Create the normal estimation class, and pass the input dataset to it
  pcl::NormalEstimationOMP<pcl::PointXYZ, pcl::Normal> ne;
  ne.setNumberOfThreads(12);  // 手动设置线程数,否则提示错误
  ne.setInputCloud (cloud);

  // Create an empty kdtree representation, and pass it to the normal estimation object.
  // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
  ne.setSearchMethod (tree);

  // Output datasets
  pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);

  // Use all neighbors in a sphere of radius 3cm
  ne.setRadiusSearch (0.03);

  // Compute the features
  ne.compute (*cloud_normals);

  // cloud_normals->points.size () should have the same size as the input cloud->points.size ()*
}

参考资料

Estimating Surface Normals in a PointCloud

  • 17
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: pcl 中可以使用 NormalEstimation 和 Visualization 进行点云向量的估算和显示。 以下是一个简单的示例代码: ```cpp #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/features/normal_3d.h> #include <pcl/visualization/cloud_viewer.h> int main(int argc, char** argv) { // 读取点云数据 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("sample.pcd", *cloud); // 估算向量 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; ne.setInputCloud(cloud); pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>()); ne.setSearchMethod(tree); pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); ne.setRadiusSearch(0.03); ne.compute(*cloud_normals); // 将向量添加到原始点云pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>); pcl::concatenateFields(*cloud, *cloud_normals, *cloud_with_normals); // 可视化点云向量 pcl::visualization::PCLVisualizer viewer("Point Cloud with Normals"); viewer.addPointCloudNormals<pcl::PointNormal>(cloud_with_normals, 1, 0.01, "normals"); viewer.spin(); return 0; } ``` 其中,`sample.pcd` 是输入的点云数据文件名。在上述代码中,我们首先读取点云数据,然后使用 NormalEstimation 对点云进行向量估算,将估算得到的向量添加到原始点云中,最后使用 PCLVisualizer 对点云向量进行可视化。 ### 回答2: PCL(点云库)是一个专门用于处理点云数据的开源软件库。估算点云向量点云处理的一个重要任务,它可以帮助我们理解点云中的几何结构和表面形状。 在PCL中,要估算点云向量,可以使用Normal Estimation的功能模块。此模块可以从点云中计算出每个点的向量,并将其存储为新的点云数据结构。 具体步骤如下: 1. 创建一个PointCloud对象来存储点云数据。 2. 读取点云数据,可以通过读取.pcd文件或者直接通过代码创建点云数据。 3. 创建一个NormalEstimation对象,用于估算法线向量。 4. 为NormalEstimation对象设置输入点云数据。 5. 调用compute方进行法线向量的估算。 6. 获取估算后的法线向量数据。 7. 可以通过可视化工具(如PCL Visualizer)将法线向量显示出来。 例如,以下是使用PCL估算点云向量并显示的示例代码: ```cpp #include <pcl/point_cloud.h> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/features/normal_3d.h> #include <pcl/visualization/pcl_visualizer.h> #include <pcl/visualization/cloud_viewer.h> int main() { // 创建一个PointCloud对象 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); // 读取点云数据 pcl::PCDReader reader; reader.read<pcl::PointXYZ>("point_cloud.pcd", *cloud); // 创建一个NormalEstimation对象 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; // 设置输入点云数据 ne.setInputCloud(cloud); // 创建一个存储法线向量的对象 pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); // 估算法线向量 ne.compute(*cloud_normals); // 可视化法线向量 pcl::visualization::PCLVisualizer viewer("PCL Viewer"); viewer.setBackgroundColor(0, 0, 0); viewer.addPointCloudNormals<pcl::PointXYZ, pcl::Normal>(cloud, cloud_normals); // 显示点云法线向量 while (!viewer.wasStopped()) { viewer.spinOnce(); } return 0; } ``` 以上代码示例演示了如何使用PCL库中的Normal Estimation模块来估算点云法线向量并使用PCL Visualizer进行可视化展示。 ### 回答3: PCL(Point Cloud Library)是一个用于点云处理的开源库,支持估算点云向量并进行可视化显示。 在PCL中,估算点云向量的主要方有两种:基于平滑处理的方和基于表面法线计算的方。 基于平滑处理的方是一种常用的估算向量的方,它使用协方差矩阵来估计每个点的向量。这种方利用邻域点的空间关系来平滑噪声,并通过计算协方差矩阵的特征向量估计每个点的向量。 基于表面法线计算的方则是通过计算表面法线来估算点云向量。这种方通常使用最近邻搜索来寻找点云中的邻域点,并通过计算这些点在法线方向上的差异来估计向量。 在PCL中,可以使用pcl::NormalEstimation类来进行点云向量的估算。该类提供了不同的估算方,例如基于平滑处理的方(如pcl::IntegralImageNormalEstimation)和基于表面法线计算的方(如pcl::NormalEstimation)等。 估算出的向量可以通过PointCloud类的points[i].normal字段来访问。为了将点云向量可视化显示,可以使用PCL中的可视化模块,例如pcl::visualization::PCLVisualizer类。该类提供了丰富的可视化功能,可以根据点云向量点云进行颜色编码、绘制法线等操作。 总的来说,使用PCL可以方便地估算点云向量,并通过可视化模块将其显示出来,这对于点云处理和分析非常有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值