首页 > 解决方案 > 在 CImg 中加载点云

问题描述

我正在尝试从点云创建一个 3D CImg 对象以使用 CImg 的编辑功能。

我有一个点云,我以 ascii 格式保存

pcl::io::savePCDFileASCII("/path/data.txt", *cloud);

然后我初始化一个 3d CImg 对象并尝试加载 asci 文件

CImg<float> image(cloud->width, cloud->height, 5);
image.load_ascii("/path/data.txt");

这是错误

[CImg] *** CImgIOException *** [instance(512,432,5,1,0x55555668f800,non-shared)] CImg<float>::load_ascii(): Invalid ascii header in file '/path/data.txt', image dimensions are set to (0,1,1,1).
terminate called after throwing an instance of 'cimg_library::CImgIOException'
  what():  [instance(512,432,5,1,0x55555668f800,non-shared)] CImg<float>::load_ascii(): Invalid ascii header in file '/path/data.txt', image dimensions are set to (0,1,1,1).

这是我生成的 ascii 文件

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F U
COUNT 1 1 1 1
WIDTH 512
HEIGHT 432
VIEWPOINT 0 0 0 1 0 0 0
POINTS 221184
DATA ascii
-0.2196694 -0.1688118 0.55800003 4285428082
-0.21879199 -0.1688118 0.55800003 4285559668
...

我是 c++ 和 CImg 的新手,所以我不确定将点云加载到 CImg 中的正确或最佳方式是什么。我在互联网上也找不到任何有用的东西,CImg github 造成的混乱多于它的帮助。

我正在使用 Ubuntu 20、c++ 11 并加载任何类型的 2D 图像都可以正常工作。

标签: c++point-cloud-librarycimg

解决方案


CImg您的 PCL 文件与预期不符。它正在寻找一个以以下开头的文件:

WIDTH HEIGHT DEPTH NUMCHANNELS
float1
float2
float3
...

所以,我做了这个例子:

#include "CImg.h"
#include <iostream>

using namespace cimg_library;
using namespace std;

int main()
{
    // Load image 
    CImg<float> image;
    image.load_ascii("image.asc");
    cout << image.width() << endl;
    cout << image.height() << endl;

}

然后我制作了一个image.asc像这样尺寸为 4x8x3 的虚拟文件,它工作正常:

4 8 3 1
float1
float2
float3
...
float96

推荐阅读