首页 > 解决方案 > 以 xyzrgb 格式读取 ascii 点云

问题描述

我在 txt 文件中有一个点云,其中每一行的形式为:

x y z r g b

我应该如何将其读入 PCL 中的 XYZRGB 点云?PCL 中的 ascii 阅读器或 pcdreader 期望格式为 xyz rgb 形式,其中 rgb 是一个代表 rgb 通道的值。

有什么方法可以读取我上面提到的格式的点云,而不必自己修改点云?

编辑:添加我当前的代码和点云中的一些行以响应评论

    pcl::ASCIIReader ptsReader;
    ptsReader.setSepChars(" ");
    ptsReader.read(m_pointCloudFilePath,*m_pointCloudRef);

如果m_pointCloudRef是类型:pcl::PointCloud<pcl::PointXYZRGB>::Ptr 此代码不适用于运行时错误消息:Failed to find match for field 'rgb'.。如果m_pointCloudRef是类型,则相同的代码有效pcl::PointCloud<pcl::PointXYZ>::Ptr(这也意味着我正在使用每行都是 ASCII 文件x y z

以下是我正在使用的点云的前几行:

0.792 9.978 12.769 234 220 209
0.792 9.978 12.768 242 228 217
0.794 9.978 12.771 241 227 214
0.794 9.978 12.770 247 231 218
0.793 9.979 12.769 234 217 207
0.793 9.979 12.768 238 224 213
0.794 9.979 12.767 239 227 215
0.795 9.978 12.772 230 221 206
0.795 9.978 12.771 243 229 216
0.795 9.979 12.770 242 226 213
0.795 9.979 12.769 235 218 208
0.795 9.979 12.768 235 221 210
0.795 9.979 12.767 240 228 216
0.795 9.979 12.766 240 230 218
0.795 9.979 12.765 240 230 218
0.795 9.978 12.763 244 234 222

标签: c++point-cloud-librarypoint-clouds

解决方案


如果您不想更改云在磁盘上的保存方式并且只需要使用很少的云类型,则可以手动读取它们。

 bool loadAsciCloud(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
{
    std::cout << "Begin Loading Model" << std::endl;
    FILE* f = fopen(filename.c_str(), "r");

    if (NULL == f)
    {
        std::cout << "ERROR: failed to open file: " << filename << endl;
        return false;
    }

    float x, y, z;
    char r, g, b;

    while (!feof(f))
    {
        int n_args = fscanf_s(f, "%f %f %f %c %c %c", &x, &y, &z, &r, &g, &b);
        if (n_args != 6)
            continue;

        pcl::PointXYZRGB point;
        point.x = x; 
        point.y = y; 
        point.z = z;
        point.r = r;
        point.g = g;
        point.b = b;

        cloud->push_back(p);
    }

    fclose(f);

    std::cout << "Loaded cloud with " << cloud->size() << " points." << std::endl;

    return cloud->size() > 0;
}

推荐阅读