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

时间:2016-10-12 06:19:41

标签: 3d computer-vision point-cloud-library

我试图在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和Spherical Searching方法两种方法,但我必须手动决定或在它们之间切换,还必须手动输入搜索和/或相邻点的数量。

避免所有麻烦我正在考虑使用点云的平均点距离来做所有这些事情 像这样的东西

ne.setKSearch (0.8*Avg_point_Distance);

ne.setRadiusSearch (1.5*Avg_point_Distance);

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

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

1 个答案:

答案 0 :(得分:0)

我浏览了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;