PCL Save VTK File With Texture Coordinates 使用PCL库来保存带纹理坐标的VTK文件

简介:

我之前有一篇博客Convert PLY to VTK Using PCL 1.6.0 or PCL 1.8.0 使用PCL库将PLY格式转为VTK格式展示了如何将PLY格式文件转化为VTK格式的文件,在文章的最后提到了VTK文件保存纹理的两种方式,第一种是需要有texture的图片,然后每个点存储上该点在图片中的x,y坐标,一般会normalize到[0,1]之间。第二种方法是直接存每个点的rgb值,上面的方法用的是第二种,因为导入的PLY格式就直接存储的texture的rgb值,并没有额外提供texture图片。

对于一般的PLY或者PCD格式的点云,一般都是用第二种方式来保存纹理的,即直接存储rgb值,这样转换成的vtk文件自然也是第二种情况,而对于大多数的可视化软件,比如ParaView或者3D Slicer,貌似只支持第一种方式,即需要导入texture图片(如果大家知道直接显示rgb值的方法,请在下方留言告知博主)。这样就极大的不方便了,而且PCL库中的点云格式一般也是XYZRGBA,并没有带UV,纹理有专门的数据结构。我们的目标是生成带texture coordinates的VTK文件,那么可以通过修改pcl自带的saveVTKFile函数来实现目标。

这里我们把纹理坐标单独抽出来,用下面的数据结构来表示:

std::vector<Eigen::Vector2f> texcoord;

pcl自带的pcl::io::saveVTKFile函数所在的文件的地址是.../io/src/vtk_io.cpp。默认的是写入RGB的值,我们只需要注释掉写入RGB的部分,添加上写入纹理坐标的部分:

Using PCL 1.6.0

int save_vtk_file(const std::string &file_name, 
                  const sensor_msgs::PointCloud2 &cloud, 
                  const std::vector<Eigen::Vector2f>& texcoord,
                  unsigned precision) 
{
    if (cloud.data.empty ())
    {
        PCL_ERROR ("[pcl::io::saveVTKFile] Input point cloud has no data!\n");
        return (-1);
    }

    // Open file
    std::ofstream fs;
    fs.precision (precision);
    fs.open (file_name.c_str ());

    unsigned int nr_points  = cloud.width * cloud.height;
    unsigned int point_size = static_cast<unsigned int> (cloud.data.size () / nr_points);

    // Write the header information
    fs << "# vtk DataFile Version 3.0\nvtk output\nASCII\nDATASET POLYDATA\nPOINTS " << nr_points << " float" << std::endl;

    // Iterate through the points
    for (unsigned int i = 0; i < nr_points; ++i)
    {
        int xyz = 0;
        for (size_t d = 0; d < cloud.fields.size (); ++d)
        {
            int count = cloud.fields[d].count;
            if (count == 0)
                count = 1;          // we simply cannot tolerate 0 counts (coming from older converter code)
            int c = 0;
            if ((cloud.fields[d].datatype == sensor_msgs::PointField::FLOAT32) && (
                cloud.fields[d].name == "x" || 
                cloud.fields[d].name == "y" || 
                cloud.fields[d].name == "z"))
            {
                float value;
                memcpy (&value, &cloud.data[i * point_size + cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
                fs << value;
                if (++xyz == 3)
                    break;
            }
            fs << " ";
        }
        if (xyz != 3)
        {
            PCL_ERROR ("[pcl::io::saveVTKFile] Input point cloud has no XYZ data!\n");
            return (-2);
        }
        fs << std::endl;
    }

    // Write vertices
    fs << "\nVERTICES " << nr_points << " " << 2*nr_points << std::endl;
    for (unsigned int i = 0; i < nr_points; ++i)
        fs << "1 " << i << std::endl;

    // Write RGB values
//     int field_index = pcl::getFieldIndex (cloud, "rgb");
//     if (field_index != -1)
//     {
//         fs << "\nPOINT_DATA " << nr_points << "\nCOLOR_SCALARS scalars 3\n";
//         for (unsigned int i = 0; i < nr_points; ++i)
//         {
//             int count = cloud.fields[field_index].count;
//             if (count == 0)
//                 count = 1;          // we simply cannot tolerate 0 counts (coming from older converter code)
//             int c = 0;
//             if (cloud.fields[field_index].datatype == sensor_msgs::PointField::FLOAT32)
//             {
//                 pcl::RGB color;
//                 memcpy (&color, &cloud.data[i * point_size + cloud.fields[field_index].offset + c * sizeof (float)], sizeof (pcl::RGB));
//                 int r = color.r;
//                 int g = color.g;
//                 int b = color.b;
//                 fs << static_cast<float> (r) / 255.0f << " " << static_cast<float> (g) / 255.0f << " " << static_cast<float> (b) / 255.0f;
//             }
//             fs << std::endl;
//         }
//     }

    // Write texture coordinates
    fs << "\nPOINT_DATA " << nr_points << "\nTEXTURE_COORDINATES tcoords 2 float\n";
    for (unsigned int i = 0; i < nr_points; ++i) {
    
        //fs << texcoord[i][0] << " " << texcoord[i][1] << "\n";
        fs << texcoord[i][1] << " " << texcoord[i][0] << "\n";
    }
    fs << std::endl;


    // Close file
    fs.close ();
    return (0);

}

Using PCL 1.8.0

int save_vtk_file (const std::string &file_name, 
    const pcl::PCLPointCloud2 &cloud, 
    const std::vector<Eigen::Vector2f>& texcoord,
    unsigned precision)
{
    if (cloud.data.empty ())
    {
        PCL_ERROR ("[pcl::io::saveVTKFile] Input point cloud has no data!\n");
        return (-1);
    }

    // Open file
    std::ofstream fs;
    fs.precision (precision);
    fs.open (file_name.c_str ());

    unsigned int nr_points  = cloud.width * cloud.height;
    unsigned int point_size = static_cast<unsigned int> (cloud.data.size () / nr_points);

    // Write the header information
    fs << "# vtk DataFile Version 3.0\nvtk output\nASCII\nDATASET POLYDATA\nPOINTS " << nr_points << " float" << '\n';

    // Iterate through the points
    for (unsigned int i = 0; i < nr_points; ++i)
    {
        int xyz = 0;
        for (size_t d = 0; d < cloud.fields.size (); ++d)
        {
            int count = cloud.fields[d].count;
            if (count == 0)
                count = 1;          // we simply cannot tolerate 0 counts (coming from older converter code)
            int c = 0;
            if ((cloud.fields[d].datatype == pcl::PCLPointField::FLOAT32) && (
                cloud.fields[d].name == "x" || 
                cloud.fields[d].name == "y" || 
                cloud.fields[d].name == "z"))
            {
                float value;
                memcpy (&value, &cloud.data[i * point_size + cloud.fields[d].offset + c * sizeof (float)], sizeof (float));
                fs << value;
                if (++xyz == 3)
                    break;
            }
            fs << " ";
        }
        if (xyz != 3)
        {
            PCL_ERROR ("[pcl::io::saveVTKFile] Input point cloud has no XYZ data!\n");
            return (-2);
        }
        fs << '\n';
    }

    // Write vertices
    fs << "\nVERTICES " << nr_points << " " << 2*nr_points << '\n';
    for (unsigned int i = 0; i < nr_points; ++i)
        fs << "1 " << i << '\n';

//     // Write RGB values
//     int field_index = getFieldIndex (cloud, "rgb");
//     if (field_index != -1)
//     {
//         fs << "\nPOINT_DATA " << nr_points << "\nCOLOR_SCALARS scalars 3\n";
//         for (unsigned int i = 0; i < nr_points; ++i)
//         {
//             int count = cloud.fields[field_index].count;
//             if (count == 0)
//                 count = 1;          // we simply cannot tolerate 0 counts (coming from older converter code)
//             int c = 0;
//             if (cloud.fields[field_index].datatype == pcl::PCLPointField::FLOAT32)
//             {
//                 pcl::RGB color;
//                 memcpy (&color, &cloud.data[i * point_size + cloud.fields[field_index].offset + c * sizeof (float)], sizeof (RGB));
//                 int r = color.r;
//                 int g = color.g;
//                 int b = color.b;
//                 fs << static_cast<float> (r) / 255.0f << " " << static_cast<float> (g) / 255.0f << " " << static_cast<float> (b) / 255.0f;
//             }
//             fs << '\n';
//         }
//     }

    // Write texture coordinates
    fs << "\nPOINT_DATA " << nr_points << "\nTEXTURE_COORDINATES tcoords 2 float\n";
    for (unsigned int i = 0; i < nr_points; ++i) {

        //fs << texcoord[i][0] << " " << texcoord[i][1] << "\n";
        fs << texcoord[i][1] << " " << texcoord[i][0] << "\n";
    }
    fs << '\n';

    // Close file
    fs.close ();
    return (0);
}

注意上面纹理的x和y的值,根据贴图的情况来看是否需要调换位置。

本文转自博客园Grandyang,原文链接:PCL Save VTK File With Texture Coordinates 使用PCL库来保存带纹理坐标的VTK文件

,如需转载请自行联系原博主。

相关文章
|
计算机视觉 C#
知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
一直以来,我使用的方法都是shiqiyu在opencvchina上面提供的引入directshow,并且采用cvvimage和cameraDs的方法。这个方法虽然在xp/win7/win8下面都能够成果使用,但是一直以来我都没有动机去深入看一看这个方法。
1224 0
|
5月前
|
计算机视觉
[Qt&MFC] 各种方式的图像读取(OpenCv、Halcon)
[Qt&MFC] 各种方式的图像读取(OpenCv、Halcon)
40 0
|
6月前
[√]freetype2 bitmap纹理转换
[√]freetype2 bitmap纹理转换
39 0
|
8月前
|
C++ 计算机视觉 Python
VS+QT+PCL点云窗体程序显示编辑保存
VS+QT+PCL点云窗体程序显示编辑保存
100 0
VS+QT+PCL点云窗体程序显示编辑保存
|
计算机视觉
Qt+OpenCV小项目:灰度图转换
Qt+OpenCV小项目:灰度图转换
173 0
Qt+OpenCV小项目:灰度图转换
|
Python
Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)
Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)
561 0
PCL:点云保存遇到的问题及解决方法
之前已经完成kinect2实时获取点云,那么接下来准备将点云保存到本地,点云扩展名为pcd。在网上查找资料普遍都是这个方法。 我就按着这个步骤尝试,首先创建一个空点云(pcl::PointCloud cloud;),接着定义点云的大小和格式,然后把信息写入点云,再使用(pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);),保存为pcd文件。
2443 0
|
机器学习/深度学习 存储 算法
PCL中使用FLANN库(1)
FLANN库全称是Fast Library for Approximate Nearest Neighbors,它是目前最完整的(近似)最近邻开源库。不但实现了一系列查找算法,还包含了一种自动选取最快算法的机制,在一个度量空间X给定一组点P=p1,p2,…,pn,这些点必须通过以下方式进行预处理,给第一个新的查询点q属于X,快速在P中找到距离q最近的点,即最近邻搜索问题。
2148 0
|
传感器 数据挖掘 索引
PCL中使用FLANN库(2)
接着上一篇的介绍继续 关于在使用readHeader函数读取点云数据头的类型的代码(Read a point cloud data header from a PCD file.) pcl::PCLPointCloud2 cloud; int version; Eigen:...
1924 0