kitti彩色地图拼接<一>、点云bin格式转为pcd格式

发布时间 2023-09-30 09:37:57作者: FeiYull

 

 下面是bin格式转pcd格式批量处理代码,其中品红色是需要改成你的实际情况的地方。

cpp:【note:代码中,pcd文件的路径改为你自己的】

 1 #include <boost/program_options.hpp>
 2 #include <pcl/point_types.h>
 3 #include <pcl/io/pcd_io.h>
 4 #include <pcl/common/point_operators.h>
 5 #include <pcl/common/io.h>
 6 #include <pcl/search/organized.h>
 7 #include <pcl/search/octree.h>
 8 #include <iostream>
 9 #include <string>
10 #include <vector>
11 #include <string.h>
12 #include <dirent.h>
13 using namespace pcl;
14 using namespace std;
15 
16 
17 /** @brief 获取path路径下所有指定格式文件名
18  *  @in    path:      eg:"/home/ros_proj/bin2pcd/input/"
19  *  @in    extendStr: eg:".bin"格式
20  *  @out   files:     “0000000000.bin” “0000000001.bin” “0000000002.bin”...
21  *  note:(具体哪种格式,需要在函数中修改,我这里默认.bin格式)
22  * */
23 int getFiles(const string path, vector<string>& files, const string& extendStr){
24 
25     int iFileCnt = 0;
26     DIR *dirptr = NULL;
27     struct dirent *dirp;
28 
29     if((dirptr = opendir(path.c_str())) == NULL)//打开一个目录
30     {
31         std::cout << "errors occurs in open the input file!" <<std::endl;
32         return 0;
33     }
34     while ((dirp = readdir(dirptr)) != NULL)
35     {
36         if ((dirp->d_type == DT_REG) && 0 ==(strcmp(strchr(dirp->d_name, '.'), extendStr.c_str())))//判断是否为文件以及文件后缀名
37         {
38             files.push_back(dirp->d_name);
39         }
40         iFileCnt++;
41     }
42     closedir(dirptr);
43 
44     return iFileCnt;
45 }
46 
47 int main(int argc, char **argv)
48 {
49     // bin输出文件夹
50     string pathIn = "/home/ros_proj/bin2pcd/input/"; // 记得修改getFiles()函数中文件格式,千万别写错,写错就JJ
51     // pcd输出文件夹
52     string pathOut = "/home/ros_proj/bin2pcd/output/";
53     string pcdStr = ".pcd";
54     vector<string> files;
55     // <一>、获取文件路径
56     getFiles(pathIn,files, ".bin");
57 //    int i = 0;
58 //    for(const auto &x: files)
59 //    {
60 //        cout << x << endl;
61 //        i++;
62 //        cout << i <<endl;
63 //    }
64     // <二>、遍历每一个bin文件
65     for (int i = 0; i < files.size(); ++i)
66     {
67         // 加载bin格式点云
68         fstream input(pathIn + files[i].c_str(), ios::in | ios::binary);
69         cout << "***********************************************" << endl;
70         cout << "processing " << (i + 1) << "st" << " bin file" <<endl;
71         cout <<"read the bin file: " << pathIn + files[i].c_str() << endl;
72         if(!input.good()){
73             cerr << "Could not read file: " << files[i] << endl;
74             exit(EXIT_FAILURE);
75         }
76         input.seekg(0, ios::beg);
77         // bin -> pcd
78         pcl::PointCloud<PointXYZI>::Ptr points (new pcl::PointCloud<PointXYZI>);
79         int j;
80         for (j=0; input.good() && !input.eof(); j++) {
81             PointXYZI point;
82             input.read((char *) &point.x, 3*sizeof(float));
83             input.read((char *) &point.intensity, sizeof(float));
84             points->push_back(point);
85         }
86         //pcd点云写入硬盘
87         string pcd = ".pcd";
88         string tmp = files[i].substr(0, 10);
89         pcl::io::savePCDFileBinary(pathOut + tmp + pcd, *points);
90         cout <<"write the pcd file: " << pathOut + tmp + pcd<< endl;
91         cout << "***********************************************" << endl;
92     }
93     return 1;
94 }

 

 

Cmakelists:

cmake_minimum_required(VERSION 2.6)
project(bin2pcd)
 
find_package(PCL 1.2 REQUIRED)
 
# 加入Boost setting
find_package(Boost COMPONENTS program_options REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable(bin2pcd bin2pcd.cpp)
 
target_link_libraries (bin2pcd ${PCL_LIBRARIES} ${Boost_LIBRARIES}) #此处也有修改
 
install(TARGETS bin2pcd RUNTIME DESTINATION bin)

  下面左边是bin格式,右边是pcd格式。