aikhs
aikhs

Reputation: 979

How can I view Point Clouds?

I am using open source code called ORB SLAM 2. As far as I know, ORB SLAM 2 does not save the map. So in order to save the points (point clouds), I have included a small code inside System.cc:

void System::CreatePCD(const string &filename){
    cout << endl << "Saving map points to " << filename << endl;

    vector<MapPoint*> vMPs = mpMap->GetAllMapPoints();

    // Create PCD init string
    std::string begin = std::string("# .PCD v.7 - Point Cloud Data file format\nVERSON .7\n");
    begin += "FIELDS x y z\n";
    begin += "SIZE 4 4 4\n";
    begin += "TYPE F F F\n";
    begin += "COUNT 1 1 1\n";

    int width = vMPs.size();
    begin += "WIDTH ";
    begin += std::to_string(width);
    begin += "\nHEIGHT 1\n";
    begin += "VIEWPOINT 0 0 0 1 0 0 0\n";
    begin += "POINTS ";
    begin += std::to_string(width);
    begin += "\nDATA ascii\n";

    // File Opening:
    ofstream f;
    f.open(filename.c_str());
    f << begin;

    // Write the point clouds:
    for(size_t i= 0; i < vMPs.size(); ++i){
        MapPoint *pMP = vMPs[i];
        if (pMP->isBad()) continue;
        cv::Mat MPPositions = pMP->GetWorldPos();
        f << setprecision(7) << MPPositions.at<float>(0) << " " << 
        MPPositions.at<float>(1) << " " << MPPositions.at<float>(2) << endl;
    }

    f.close();
    cout << endl << "Map Points saved!" << endl;

  }
}

As you can see, I have included all the necessary things for the PCL version 7. My newly created point cloud file looks like this:

# .PCD v.7 - Point Cloud Data file format
VERSON .7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 1287
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 1287
DATA ascii
0.1549043 -0.3846602 0.8497394
0.01127081 -0.2949406 0.9007485
0.6072361 -0.3651089 1.833479
…

But whenever I try to visualize the file by running pcl_viewer pointclouds.pcd I get an error:

> Loading pointcloud.pcd [pcl::PCDReader::readHeader] No points to read

What am I doing wrong?

Upvotes: 1

Views: 1271

Answers (2)

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

The PCD (Point Cloud Data) file format is well documented. At a first look your file seems to be correct, but there is a small typo (missing I) in your code:

std::string begin = std::string("# .PCD v.7 - Point Cloud Data file format\nVERSON .7\n");

The code writes:

VERSON .7

Correct would be:

VERSION .7

A quick look in the source code shows that the typo leads to an early exit because it matches no valid parameter. This means all following parameters, including POINTS, are ignored. The result will be the No points to read error.

Fix the typo and your file works like expected.

Upvotes: 3

Dexter
Dexter

Reputation: 2480

It looks like you have a typo in your format:

// Create PCD init string
std::string begin = std::string("# .PCD v.7 - Point Cloud Data file format\nVERSON .7\n");

VERSON its being written instead of VERSION (it's missing an I).

Upvotes: 0

Related Questions