使用pcl将bin文件转化为pcd文件

使用pcl将bin文件转化为pcd文件

环境搭载:ubuntu16.04

之后正式操作具体如下:
在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)

现在开始编译
直接上图,代码最后给出可以复制的

执行完cmake…
make
然后输入i=1;for x in /home/wzf/PointCloud/bin2pcd/velodyne/bin/*.bin; do /home/wzf/PointCloud/bin2pcd/build/bin2pcd --infile $x --outfile /home/wzf/PointCloud/bin2pcd/velodyne/pcd/$i.pcd; let i=i+1; done

/home/wzf/PointCloud/bin2pcd/velodyne/bin/*.bin 是储存 .bin文件的路径;
/home/wzf/PointCloud/bin2pcd/build/bin2pcd 是可执行程序的路径;
/home/wzf/PointCloud/bin2pcd/velodyne/pcd/$i.pcd 是放转换后的pcd文件的路径。

所有代码成功执行后,查看velodyne文件下的pcd文件夹,会发现生成里pcd文件,如下图:
在这里插入图片描述

源文件下载:https://download.csdn.net/download/RNG_uzi_/12665944

使用Open3D将bin文件换为pcd文件的方法如下: 1. 导入必要的Open3D库: ```python import open3d as o3d ``` 2. 读取bin文件换为Open3D点云格式: ```python # 读取bin文件 pcl = o3d.io.read_point_cloud("input_file.bin", format="xyzrgb") # 可选:根据需要进行点云预处理,例如下采样 # pcl_downsampled = pcl.voxel_down_sample(voxel_size=0.01) # 可选:对点云进行滤波处理,例如移除离群点 # pcl_filtered = pcl_downsampled.remove_radius_outlier(nb_points=30, radius=0.01) # 可选:对点云进行平滑处理,例如使用高斯滤波器 # pcl_smoothed = pcl_filtered.filter_smoothed_gaussian(std_dev=0.02) # 可选:对点云进行配准处理,例如使用ICP算法 # pcl_registered = pcl_smoothed.estimate_normals( # search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30)) # transformation = o3d.pipelines.registration.registration_icp( # pcl_registered, target_pcl, max_correspondence_distance=0.05, criteria=o3d.pipelines.registration.ICPConvergenceCriteria()) # 如果有进行点云预处理、滤波、平滑、配准等操作,请使用相应的点云数据 ``` 3. 将Open3D点云对象保存为pcd文件: ```python o3d.io.write_point_cloud("output_file.pcd", pcl) ``` 4. 完整的代码示例: ```python import open3d as o3d # 读取bin文件 pcl = o3d.io.read_point_cloud("input_file.bin", format="xyzrgb") # 对点云进行处理 # 保存为pcd文件 o3d.io.write_point_cloud("output_file.pcd", pcl) ``` 请注意,根据您的需求,您可能需要根据具体情况进行预处理、滤波、平滑、配准等操作。在示例代码中的注释部分,您可以根据需要选择使用这些操作。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RNG_uzi_

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值