c++point-cloud-librarycimg

Loading a point cloud in CImg


I am trying to create a 3D CImg object from a point cloud to use the editing capabilities of CImg.

I have a point cloud that I save in ascii format

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

Then i initialize a 3d CImg object and try to load the asci file

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

This is the error

[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).

here is my generated ascii file

# .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
...

I'm new to c++ and CImg so I'm not sure what is the correct or optimal way to load a point cloud into CImg. I also couldn't find anything helpful on the internet and the CImg github causes more confusion than it helps.

I'm using Ubuntu 20, c++ 11 and loading any kind of 2D image works fine.


Solution

  • Your PCL file doesn't match what CImg is expecting. It is looking for a file that starts with:

    WIDTH HEIGHT DEPTH NUMCHANNELS
    float1
    float2
    float3
    ...
    

    So, I made this example:

    #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;
    
    }
    

    And then I made a dummy image.asc file with dimensions 4x8x3 like this and it works fine:

    4 8 3 1
    float1
    float2
    float3
    ...
    float96