B站|公众号:啥都会一点的研究生
- 新建文件夹datasets
- datasets文件将下新建两个文件夹
bin
与pcd
,bin
中存放要转换的二进制文件,pcd
存放转换后的点云文件 - datasets下新建CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(bin2pcd)
find_package(PCL 1.2 REQUIRED)
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)
- 新建bin2pcd.cpp
#include <boost/program_options.hpp>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/io.h>
#include <pcl/io/vtk_io.h>
#include <vector>
#include <iostream>
#include <fstream>
using namespace pcl;
using namespace std;
void topcd(string name);
void topcd(string infile,string outfile) {
// load point cloud
fstream input(infile.c_str(), ios::in | ios::binary);
if (!input.good()) {
cerr << "Could not read file: " << infile << endl;
exit(EXIT_FAILURE);
}
input.seekg(0, ios::beg);
pcl::PointCloud<PointXYZI>::Ptr points(new pcl::PointCloud<PointXYZI>);
int i;
for (i = 0; input.good() && !input.eof(); i++) {
PointXYZI point;
input.read((char*)&point.x, 3 * sizeof(float));
input.read((char*)&point.intensity, sizeof(float));
points->push_back(point);
}
input.close();
cout << "Read point cloud with " << i << " points, writing to " << outfile << endl;
pcl::PCDWriter writer;
// Save DoN features
writer.write<PointXYZI>(outfile, *points, false);
}
int main(int argc, char** argv) {
string binpath = "/home/xxxx/datasets/bin/";
string pcdpath = "/home/xxxx/datasets/pcd/";
vector<boost::filesystem::path> stream(boost::filesystem::directory_iterator{binpath}, boost::filesystem::directory_iterator{});
sort(stream.begin(), stream.end());
auto streamIterator = stream.begin();
int j = 0;
while(streamIterator != stream.end()){
string binfile((*streamIterator).string());
stringstream ss;
string str;
ss << setw(10) << setfill('0') << j; // 补零
ss >> str;
string surfix = ".pcd";
string pcdfile = pcdpath + str + surfix;
topcd(binfile,pcdfile);
streamIterator++;
j++;
}
return 0;
}
- 注意仅需修改
binpath
与pcdpath
为你的路径 - 新建build文件夹,所以现在的文件层级为这样
├── bin2pcd.cpp
├── build
├── CMakeLists.txt
└── datasets
├── bin
├── 0000000000.bin
├── 0000000001.bin
...
└── pcd
├── 0000000000.bin
├── 0000000001.bin
...
- 剩下的步骤就是正常编译,
进入build文件夹
cmake .. && make -j12
- 然后运行文件
./bin2pcd