如何计算云中每个点的法线

How can I compute a normal for each point in cloud

我的问题是:我有 3D 点云。我想将每个法线归因于每个点。来自 PCL 教程:

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

// 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);

我只能找到所有的云法线,我想为每个点指定其精确的法线。

我假设您有 pcl::PointCloud<pcl::PointXYZRGB> 类型的点云,并且您想将估计的表面法线分配给点云的每个点。

为输入点云的每个点估计表面法线。所以表面法线点云的大小等于输入点云的大小。

您可以创建另一个 pcl::PointCloud<pcl::PointXYZRGBNormal> 类型的点云,它可以保存相应法线的信息以及点的位置和颜色。然后写一个for循环赋值。

下面是代码片段:

pcl::PointCloud<pcl::PointXYZRGB>& src; // Already generated
pcl::PointCloud<pcl::PointXYZRGBNormal> dst; // To be created

// Initialization part
dst.width = src.width;
dst.height = src.height;
dst.is_dense = true;
dst.points.resize(dst.width * dst.height);

// Assignment part
for (int i = 0; i < cloud_normals->points.size(); i++)
{
    dst.points[i].x = src.points[i].x;
    dst.points[i].y = src.points[i].y;
    dst.points[i].z = src.points[i].z;

    dst.points[i].r = src.points[i].r;
    dst.points[i].g = src.points[i].g;
    dst.points[i].b = src.points[i].b;

   // cloud_normals -> Which you have already have; generated using pcl example code 

    dst.points[i].curvature = cloud_normals->points[i].curvature;

    dst.points[i].normal_x = cloud_normals->points[i].normal_x;
    dst.points[i].normal_y = cloud_normals->points[i].normal_y;
    dst.points[i].normal_z = cloud_normals->points[i].normal_z;
}

希望对您有所帮助。

您可以使用 pcl::concatenateFields:

而不是使用 for 循环
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals (new pcl::PointCloud<pcl::PointNormal>); 
    pcl::PointCloud<pcl::Normal>::Ptr normals (new pcl::PointCloud<pcl::Normal>);
    // Use all neighbors in a sphere of radius 3cm
    ne.setRadiusSearch (0.03);
    // Compute the features
    ne.compute (*normals);
    pcl::concatenateFields(*cloud, *normals, *cloud_with_normals);