如何计算点云中点的平均距离

how to calculate mean distance of points in point cloud

我试图在 PointCloudLibrary 的帮助下找到点云的法线,以下是我正在使用的代码

        pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;

        ne.setInputCloud (test1.cloud);

        pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());

        ne.setSearchMethod (tree);

        ne.setKSearch (150);

        ne.setRadiusSearch (1.5);

        ne.compute (*Normalcloud);

我正在使用 KDsearch 和球形搜索两种方法,但我必须手动决定或在它们之间切换,还必须手动输入搜索 and/or 个相邻点。

为了避免所有这些麻烦,我正在考虑使用点云的平均点距离来完成所有这些工作 像这样

ne.setKSearch (0.8*Avg_point_Distance);

ne.setRadiusSearch (1.5*Avg_point_Distance);

但是我不知道如何得到整个点云的平均距离?

注意:如果有人能以更易于理解的方式编辑问题,我不会介意:)

我浏览了 PCL 文档并找到了 Kd 搜索方法,从中我计算了最近的相邻点并累积了所有距离并将其除以点云中存在的点数。

该方法的代码片段如下:

    totalcount = inputCloud->width * inputCloud->height ;

    EuclidianDistance = new float [totalcount];

    kdtree.setInputCloud (inputCloud);

    int K = 2; //first will be the distance with point it self and second will the nearest point that's why "2"

    std::vector<int> pointIdxNKNSearch(K);
    std::vector<float> pointNKNSquaredDistance(K);

    for (int i = 0; i < totalcount; ++i)
    {
        std::cout << "\nK nearest neighbor search at (" << inputCloud->points[i].x 
            << " " << inputCloud->points[i].y 
            << " " << inputCloud->points[i].z
            << ") with K=" << K << std::endl;

        if ( kdtree.nearestKSearch (inputCloud->points[i], K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
            {
                for (size_t j = 0; j < pointIdxNKNSearch.size (); ++j)
                {
                   //saving all the distance in Vector
                    EuclidianDistance[i] =  pointNKNSquaredDistance[j];

                }
            }
        }

    for(int i = 0; i < totalcount; i++)
    {
        //accumulating all distances
        totalDistance = totalDistance + EuclidianDistance[i];
    }

    //calculating the mean distance
    meanDistance = totalDistance/totalcount;

    //freeing the allocated memory      
    delete  [] EuclidianDistance;