PCL:将点云投影到参数模型上

这个程序是将点云投影到一个参数模型上(例如:球形, 或者是平面上),假如是平面的话,通过给定coefficients,就可以了,代码具体如下:

#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char **argv) {
    std::cout << "Hello, world!" << std::endl;


    pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
    pcl::PointCloud::Ptr cloud_projected (new pcl::PointCloud);

    cloud->width = 500;
    cloud->height = 1;
    cloud->points.resize(cloud->width * cloud->height);

    for(size_t  i = 0; i < cloud->points.size(); ++i )
    {
     cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
     cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
     cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }


    std::cerr << "cloud before projection: " << std::endl;
    for(size_t i = 0; i < cloud->points.size(); ++i)
    {
      std::cerr << "   " << cloud->points[i].x << " "
            << cloud->points[i].y << " "<< cloud->points[i].y << " \n";
    }

    //create a set of planar coefficients with X=Y=0, Z = 1
    pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
    coefficients->values.resize (4);
    coefficients->values[0] = coefficients->values[1] = 0;
    coefficients->values[2] = 1.0;
    coefficients->values[3] = 0;

    //create the filtering object
    pcl::ProjectInliers proj;
    proj.setModelType(pcl::SACMODEL_PLANE);
    proj.setInputCloud(cloud);
    proj.setModelCoefficients(coefficients);
    proj.filter(*cloud_projected);


    std::cerr << "cloud after projection: " << std::endl;
    for (size_t i = 0; i < cloud_projected->points.size(); ++i)
    {
      std::cerr << "   " << cloud_projected->points[i].x << " "
          << cloud_projected->points[i].y << " "<< cloud_projected->points[i].z << std::endl;;
    }



      pcl::PCDWriter writer;
      writer.write("original_points.pcd", *cloud, false);
      writer.write("project_inliers.pcd", *cloud_projected, false);




    return 0;
}

最后的结果图

PCL:将点云投影到参数模型上_第1张图片

你可能感兴趣的:(SLAM)