点云数据bin格式转换为pcd格式的方法

申明:再找到一款点云标注工具(https://blog.csdn.net/r1141207831/article/details/103788891)的时候发现只能输入pcd文件,那么现有的bin文件无法使用,于是找到了bin文件转换为pcd文件的方法,现在分享出来和大家一起讨论学习,如有问题可以留言评论。也感谢那些默默支持和帮助的人。


程序运行环境:

  运行系统:Ubuntu16.04


一、新建如下目录结构的文件夹以及文件

PointCloud
    └── bin2pcd
        ├── bin2pcd.cpp
        ├── build
        ├── CMakeLists.txt
        ├── run.sh
        └── velodyne
            ├── bin
            └── pcd

二、填充文件代码

 1 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);
}

 2 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)

 3 run.sh代码如下所示:

i=1; for x in $(cd "$(dirname "$0")"; pwd)/velodyne/bin/*bin; do $(cd "$(dirname "$0")"; pwd)/build/bin2pcd --infile $x --outfile $(cd "$(dirname "$0")"; pwd)/velodyne/pcd/$i.pcd; let i=i+1; done

三、编译程序

 启动终端切换目录到build文件夹下,输入以下命令进行编译

cd build
cmake ..
make

四、执行转换程序

 1  将需要转换的bin文件复制到velodyne目录下的bin文件夹下

 2 使用下面命令为run.sh文件赋予可执行权限

sudo chmod a+x run.sh

 3  切换到run.sh文件目录下,执行以下命令开始bin转换pcd程序

sh run.sh

至此该过程已经全部走完,现在可以到velodyne目录下的pcd文件夹下查看已经转换好的pcd文件了。


附录:

bash 中sh脚本自身路径的获取 https://blog.csdn.net/zhml8951/article/details/51669401

ubuntu16.04下用pcl库将点云bin文件转成pcd文件 https://blog.csdn.net/qq_35491306/article/details/82903371

kitti2pcd.cpp https://github.com/yanii/kitti-pcl/blob/master/src/kitti2pcd.cpp

如果需要在C++代码中实现lvx点云文件加载和换为pcd格式,可以使用以下步骤: 1. 下载并安装Livox Viewer软件,并添加Livox Viewer安装路径下的SDK目录到系统环境变量中。 2. 在代码中引入livox_sdk.h头文件,并使用LivoxSDK类中的GetLvxFilePaths函数获取lvx文件路径列表。 ```cpp #include <iostream> #include <vector> #include "livox_sdk.h" int main() { // 初始化SDK livox::SdkInit(); // 获取lvx文件路径列表 std::vector<std::string> lvx_file_paths; livox::LivoxSdk::GetLvxFilePaths(lvx_file_paths); // 遍历lvx文件路径列表,逐个换为pcd格式 for (auto& lvx_file_path : lvx_file_paths) { std::string pcd_file_path = lvx_file_path + ".pcd"; // 调用Livox Viewer的命令行工具livox_tool进行换 std::string cmd = "livox_tool export -f pcd -i " + lvx_file_path + " -o " + pcd_file_path; int ret = system(cmd.c_str()); if (ret == 0) std::cout << "Convert " << lvx_file_path << " to " << pcd_file_path << " success!" << std::endl; else std::cout << "Convert " << lvx_file_path << " to " << pcd_file_path << " failed!" << std::endl; } // 反初始化SDK livox::SdkDeinit(); return 0; } ``` 需要注意的是,Livox SDK提供了GetLvxFilePaths函数可以获取lvx文件路径列表,但是SDK本身并不提供将lvx文件换为pcd格式的功能,因此需要调用Livox Viewer的命令行工具livox_tool进行换。在Windows平台下,livox_tool位于Livox Viewer安装目录下的bin目录中。在Linux平台下,livox_tool位于Livox Viewer安装目录下的bin/linux目录中。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值