PCL学习记录--点云数据的获取与可视化

1、获取点云数据

(1)点云定义

pointCloud = std::make_shared>();

(2)获取相机数据(camera.c和camera.h略)

frame = camera.getFrame();

(3)利用深度信息获取点云数据

void infer_base::getXYZPointCloud(const cv::Mat &cv_depth) {
    int width = camera.k4aCalibration.color_camera_calibration.resolution_width;
    int height = camera.k4aCalibration.color_camera_calibration.resolution_height;

    depthImage = k4a::image::create_from_buffer(K4A_IMAGE_FORMAT_DEPTH16, width, height,
                                                width * (int) sizeof(uint16_t), cv_depth.data,
                                                width * height * 2, nullptr, nullptr);
    xyzImage = camera.k4aTransformation.depth_image_to_point_cloud(depthImage, K4A_CALIBRATION_TYPE_COLOR);

    auto *xyzImageData = (signed short *) xyzImage.get_buffer();

    pointCloud->clear();

    pcl::PointXYZ point;
    for (int i = 0; i < width * height; i++) {
        if (xyzImageData[3 * i + 2] == 0) continue;
        point.x = xyzImageData[3 * i + 0];
        point.y = xyzImageData[3 * i + 1];
        point.z = xyzImageData[3 * i + 2];
        pointCloud->points.emplace_back(point);
    }
}
getXYZPointCloud(frame.depthImage);  

2、将点云数据保存为.ply文件

pcl::io::savePLYFile ("one.ply", *pointCloud);
pcl::PCLPointCloud2 clod;

3、将.ply文件转为.pcd文件

pcl::io::loadPLYFile("one.ply", clod);
pcl::io::savePCDFile("one.pcd", clod);

4、.ply或.pcd文件可视化

安装Cloudcompare软件:Flathub—An app store and build service for Linux

PCL学习记录--点云数据的获取与可视化_第1张图片

5、一些问题

(1)如果直接将点云数据保存为.pcd文件则会报错:

[pcl::PCDWriter::writeASCII] Number of points different than width * height!

利用其他博客所描述的方法增加如下代码:

pointCloud->width = 1;
pointCloud->height = pointCloud->points.size();

成功保存.pcd文件但是可视化之后发现不正常。

(2)读错.ply文件时会报错,大概是保存的ply文件格式有问题:

[pcl::PLYReader::read] problem parsing header!

(3)想做到实时显示pcl点云,利用如下代码:

pcl::visualization::CloudViewer viewer("Cloud Viewer");      //创建viewer对象
viewer.showCloud(cloud);

放进循环里会报segmentation fault的错误:段错误 (核心已转储)

你可能感兴趣的:(学习,人工智能,opencv,c++,计算机视觉)