采用ubuntu18.04+PCL
代码如下:
#include <omp.h>
#include <ctime>
#include <vector>
#include <string>
#include <algorithm>
#include <pcl/io/pcd_io.h>
#include <pcl/common/common_headers.h>
#include <sys/stat.h>
#include <pcl/io/boost.h>
#include <boost/program_options.hpp>
#include <fstream>
void convertPCDtoBin(std::string &in_file, std::string& out_file)
{
std::cout << "2 "<< std::endl;
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
if (pcl::io::loadPCDFile<pcl::PointXYZI>(in_file, *cloud) == -1) //* load the file
{
std::string err = "Couldn't read file " + in_file;
std::cout << "4 "<< std::endl;
PCL_ERROR(err.c_str());
std::cout << "5 "<< std::endl;
return;// (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from "
<< in_file
<< " with the following fields: "
<< std::endl;
std::string out_file1 = out_file + ".bin";
std::ofstream myFile(out_file1.c_str(), std::ios::out | std::ios::binary);
for (int j = 0; j < cloud->size(); j++) {
std::cout << "3 "<< std::endl;
myFile.write((char*)& cloud->at(j).x, sizeof(cloud->at(j).x));
myFile.write((char*)& cloud->at(j).y, sizeof(cloud->at(j).y));
myFile.write((char*)& cloud->at(j).z, sizeof(cloud->at(j).z));
myFile.write((char *)& cloud->at(j).intensity, sizeof(cloud->at(j).intensity));
//myFile << cloud->at(j).x << " " << cloud->at(j).y << " " << cloud->at(j).z << endl;
std::cout << "4 "<< std::endl;
}
//myFile.write(reinterpret_cast<char*>(myFile),nume*7sizeof(float));
myFile.close();
std::cout << "5 "<< std::endl;
}
int main(int argc, char **argv)
{
std::string path_input = "/home/*****/pcd2bin/pcd/000005.pcd";
std::string path_output= "/home/*****/pcd2bin/bin/00.txt";#这里就是需要一个文件来存储
#二进制文件,等代码运行完成后就自动生成为.bin文件了
std::cout << "1 "<< std::endl;
convertPCDtoBin(path_input,path_output);
}
CMakeLists.txt就和常规使用的一样。
cd build
cmake ..
make
./pcd2bin
需要改的就是path_input和path_output了。