PCL 由点云生成深度图像

前言:在电脑上的pcl1.8.0版本可能是由于版本问题,无法在窗口显示深度图像,但是深度图像确实是生成了的,可以通过一个API将深度图像保存为一个png格式的图片然后查看。

该函数如下:

//save rangeImage
//相关的头文件
#include //保存深度图像
#include //保存深度图像

    float *ranges = rangeImage.getRangesArray();
    unsigned char* rgb_image = pcl::visualization::FloatImageUtils::getVisualImage(ranges,rangeImage.width,rangeImage.height);
    pcl::io::saveRgbPNGFile("saveRangeImageRGB.png",rgb_image,rangeImage.width,rangeImage.height);//保存至该png文件
    std::cerr << "Picture Saved!" << std::endl;

完整代码:

#include 
#include //深度图有关文件
#include //点云可视化头文件
#include //pcd文件输入/输出
#include //rangeImage可视化头文件
#include 
#include //命令行参数解析
#include //多线程文件
#include //保存深度图像
#include //保存深度图像

int main (int argc, char** argv) {

    //读入点云数据
   // pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud);
   // pcl::io::loadPCDFile("room_scan1.pcd",*cloud_in);

  pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud);
 //生成数据
  for (float y=-0.5f; y<=0.5f; y+=0.01f) {
    for (float z=-0.5f; z<=0.5f; z+=0.01f) {
      pcl::PointXYZ point;
      point.x = 2.0f - y;
      point.y = y;
      point.z = z;
      cloud_in->points.push_back(point);
    }
  }
  cloud_in->width = (uint32_t) cloud_in->points.size();
  cloud_in->height = 1;



//以1度为角分辨率,从上面创建的点云创建深度图像。
  float angularResolution = (float) (  1.0f * (M_PI/180.0f));  
// 1度转弧度
  float maxAngleWidth     = (float) (360.0f * (M_PI/180.0f));  
// 360.0度转弧度
  float maxAngleHeight    = (float) (180.0f * (M_PI/180.0f)); 
// 180.0度转弧度
  Eigen::Affine3f sensorPose = (Eigen::Affine3f)Eigen::Translation3f(0.0f, 0.0f, 0.0f);
  pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
  float noiseLevel=0.00;
  float minRange = 0.0f;
  int borderSize = 1;
  pcl::RangeImage rangeImage;
  rangeImage.createFromPointCloud(*cloud_in, angularResolution, maxAngleWidth, maxAngleHeight, sensorPose, coordinate_frame, noiseLevel, minRange, borderSize);
std::cout << rangeImage << "\n";//重载运算符<<

pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("CloudPointView"));
viewer->initCameraParameters();

int v1(0);
//viewer->createViewPort(0,0,0.5,1,v1);
viewer->setBackgroundColor(255,255,255,v1);
pcl::visualization::PointCloudColorHandlerCustom color1(cloud_in->makeShared(),255,0,0);
viewer->addPointCloud(cloud_in->makeShared(),color1,"pointCloud",v1);

viewer->addCoordinateSystem();
//viewer->spin();//这句话不注释掉会导致只显示点云图而不显示深度图的窗口



//save depth picture
    float *ranges = rangeImage.getRangesArray();
    unsigned char* rgb_image = pcl::visualization::FloatImageUtils::getVisualImage(ranges,rangeImage.width,rangeImage.height);
    pcl::io::saveRgbPNGFile("saveRangeImageRGB.png",rgb_image,rangeImage.width,rangeImage.height);
    std::cerr << "Picture Saved!" << std::endl;

//可视化深度图像rangeImage
pcl::visualization::RangeImageVisualizer range_image_widget("Range image");//创建Range image显示的对象
range_image_widget.setWindowTitle("RangeImage");
range_image_widget.showRangeImage(rangeImage);
    while (!range_image_widget.wasStopped ()&& !viewer->wasStopped())
    {
        range_image_widget.spinOnce ();
        viewer->spinOnce();

        pcl_sleep (0.01);


    }

    return  0 ;
}

可视化:左下为深度图

PCL 由点云生成深度图像_第1张图片

你可能感兴趣的:(PointCloud,点云生成深度图像)