Ubuntu16.04下將點雲bin文件轉成pcd文件
不多說,直接上。首先要先安裝好點雲庫(pcl),可以參考我之前的博客。
安裝好pcl後,會發現home下有一個pcl文件。
之後正式操作具體如下:
在home下,新建文件夾PointCloud(我建在這裏,大家隨意),在PointCloud文件裏繼續新建文件夾bin2pcd,在bin2pcd文件裏繼續新建文件夾velodyne和build,同時新建文檔bin2pcd.cpp和CMakeLists.txt,進入新建等velodyne文件裏,繼續新建文件夾bin和pcd,到此,新建操作結束。
然後將測試的bin文件放入velodyne裏的bin文件夾裏,我這裏只放了7個bin文件。接着把空文檔bin2pcd.cpp和CMakeLists.tx的代碼補上,代碼以及上述操作,如下列圖示:
bin2pcd.cpp代碼如下:
#include <boost/program_options.hpp>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/point_operators.h>
#include <pcl/common/io.h>
#include <pcl/search/organized.h>
#include <pcl/search/octree.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/surface/gp3.h>
#include <pcl/io/vtk_io.h>
#include <pcl/filters/voxel_grid.h>
#include <iostream>
#include <fstream>
using namespace pcl;
using namespace std;
namespace po = boost::program_options;
int main(int argc, char **argv){
///The file to read from.
string infile;
///The file to output to.
string outfile;
// Declare the supported options.
po::options_description desc("Program options");
desc.add_options()
//Options
("infile", po::value<string>(&infile)->required(), "the file to read a point cloud from")
("outfile", po::value<string>(&outfile)->required(), "the file to write the DoN point cloud & normals to")
;
// Parse the command line
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
// Print help
if (vm.count("help"))
{
cout << desc << "\n";
return false;
}
// Process options.
po::notify(vm);
// 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 KTTI point cloud with " << i << " points, writing to " << outfile << endl;
pcl::PCDWriter writer;
// Save DoN features
writer.write<PointXYZI> (outfile, *points, false);
}
CMakeLists.txt代碼如下:
cmake_minimum_required(VERSION 3.5)
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)
現在開始編譯
直接上圖,代碼最後給出可以複製的。
代碼如下:
cd PointCloud/bin2pcd/build
cmake ..
make
i=1;for x in /home/terry/PointCloud/bin2pcd/velodyne/bin/*.bin; do /home/terry/PointCloud/bin2pcd/build/bin2pcd --infile $x --outfile /home/terry/PointCloud/bin2pcd/velodyne/pcd/$i.pcd; let i=i+1; done
其中,最後一行代碼需要自行改動,提示:
/home/terry/PointCloud/bin2pcd/velodyne/bin/*.bin 是儲存 .bin文件的路徑;
/home/terry/PointCloud/bin2pcd/build/bin2pcd 是可執行程序的路徑;
/home/terry/PointCloud/bin2pcd/velodyne/pcd/$i.pcd 是放轉換後的pcd文件的路徑。
所有代碼成功執行後,查看velodyne文件下的pcd文件夾,會發現生成裏pcd文件,如下圖:
參考鏈接
(1)https://blog.csdn.net/qq_35491306/article/details/82903371
(2)https://blog.csdn.net/tuyim7124/article/details/88615937