Convert PLY to VTK Using PCL 1.6.0 or PCL 1.8.0 使用PCL库将PLY格式转为VTK格式

PLY格式是比较流行的保存点云Point Cloud的格式,可以用MeshLab等软件打开,而VTK是医学图像处理中比较常用的格式,可以使用VTK库和ITK库进行更加复杂的运算处理。我们可以使用ParaView软件对VTK格式文件进行预览和简单处理,ParaView也可以打开PLY格式,但是就没有texture了,而且我们如果直接用ParaView导出VTK格式也没有texture的,这不是我们想要的结果。MeshLab虽然可以打开有texture的PLY文件,但是却不支持导出VTK格式,那么我们如何将PLY转为VTK格式并且保留texture呢?我们可以用PCL库来转换,PCL全称是Point Cloud Library,是专门处理点云的库,功能十分强大,提供saveVTKFile函数可以保存vtk,就是要注意参数的类型,做一些类型转换即可。

Using PCL 1.6.0:

// PCL 1.6.0
#include <iostream>
#include <string>

#include <pcl/io/vtk_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/ros/conversions.h>
#include <sensor_msgs/PointCloud2.h>

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main() {

    std::string filename = "in.ply";
    PointCloudT::Ptr pc(new PointCloudT);
    if (pcl::io::loadPLYFile (filename, *pc) == -1) {
        PCL_ERROR("Error reading point cloud %s
", filename.c_str());
        return 0;
    }

    sensor_msgs::PointCloud2 cloud2;
    pcl::toROSMsg(*pc, cloud2)
    pcl::io::saveVTKFile("out.vtk", cloud2);

    return 0;
}

Using PCL 1.8.0:

// PCL 1.8.0
#include <iostream>
#include <string>

#include <pcl/io/vtk_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

int main() {

    std::string filename = "in.ply";
    PointCloudT::Ptr pc(new PointCloudT);
    if (pcl::io::loadPLYFile (filename, *pc) == -1) {
        PCL_ERROR("Error reading point cloud %s
", filename.c_str());
        return 0;
    }

    pcl::PCLPointCloud2 cloud2;
    pcl::toPCLPointCloud2(*pc, cloud2);
    pcl::io::saveVTKFile("out.vtk", cloud2);

    return 0;
}

VTK格式的点云关于texture的存储方式有两种,第一种是需要有texture的图片,然后每个点存储上该点在图片中的x,y坐标,一般会normalize到[0,1]之间。第二种方法是直接存每个点的rgb值,上面的方法用的是第二种,因为导入的PLY格式就直接存储的texture的rgb值,并没有额外提供texture图片,这种方法导出的vtk格式,在VTK库的viewer中显示是有texture的,大功告成!

原文地址:https://www.cnblogs.com/grandyang/p/6242139.html