如何查看点云?

时间:2018-10-02 09:00:59

标签: ros point-cloud-library point-clouds

我正在使用称为ORB SLAM 2的开源代码。据我所知,ORB SLAM 2不会保存地图。因此,为了保存点(点云),我在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;

  }
}

如您所见,我已经包含了PCL 7版的所有必要内容。我新创建的点云文件如下所示:

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

但是每当我尝试通过运行pcl_viewer pointclouds.pcd来可视化文件时,都会出现错误:

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

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

PCD(点云数据)文件格式为well documented。乍一看,您的文件似乎是正确的,但是您的代码中有一个小的错字(缺少 I ):

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

代码写道:

  

VERSON .7

正确的是:

  

VERS 开启.7

快速浏览source code会发现错字会导致提前退出,因为它与有效参数都不匹配。这意味着所有以下参数,包括 POINTS ,都将被忽略。结果将是没有要读取的点错误。

修正打字错误,您的文件将按预期工作。

答案 1 :(得分:0)

您似乎在输入格式有误:

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

VERSON而不是VERSION(它缺少I)被写入。