三维点云PCD转换成bin格式
在网上看了一些用PCL直接读取PCD点云,然后直接写入文件,但再通过代码打开显示不了,因为其中很多Nan数据,找到一个大佬写的PCD转成bin格式的代码,是python版本的,要求是提供的PCD文件要包含intensity、(ring,time)等,以下例子仅供参考。
1. 安装依赖
pip install numpy
pip install argparse
pip install pypcd
pip install tqdm
注意 pypcd是python2.7的库
2. 运行代码
git clone https://github.com/Yuseung-Na/pcd2bin.git
cd pcd2bin
python pcd2bin.py --pcd_path=../123/pcd --bin_path=../123/bin --file_name=test
Notes:
这个仓库也有转换方法 https://github.com/Qjizhi/kitti-velodyne-viewer
3. 简易版
def save_lidar_files(src_pth,out_pth,file_count): # save as bin format
'''
Use this function fist to convert the timestamp-name pcd into counter-sorted bin file
:param src_pth: .pcd files
:param out_pth: .bin files
:param file_count : The initial file number you want to save
:return: None
'''
files=[]
for idx,filename in enumerate(os.listdir(src_pth)):
name,ext = os.path.splitext(filename)
if ext == '.pcd':
files.append(name)
files.sort()
for pcd_file in tqdm(files):
src_lidar_file = src_pth + '/' + str(pcd_file) + '.pcd'
out_lidar_file = out_pth + '/' + str(file_count).zfill(6) + '.bin'
# Load pcd from pypcd
pc = pypcd.PointCloud.from_path(src_lidar_file)
np_x = (np.array(pc.pc_data['x'], dtype=np.float32)).astype(np.float32)
np_y = (np.array(pc.pc_data['y'], dtype=np.float32)).astype(np.float32)
np_z = (np.array(pc.pc_data['z'], dtype=np.float32)).astype(np.float32)
np_i = (np.array(pc.pc_data['intensity'], dtype=np.float32)).astype(np.float32) / 256
points_32 = np.transpose(np.vstack((np_x, np_y, np_z, np_i)))
with open(out_lidar_file, 'w') as f: # Save as bin format
points_32.tofile(f)
file_count += 1