在Kitti数据集中,点云以bin的格式存储,在项目的实操中经常需要将采集得到的pcd格式点云转换成bin格式,以方便后续的模型训练或测试。以下代码使用python语言实现了点云的格式转换:
默认情况下,pcd格式的点云保存在./pcd文件夹下,生成的bin格式点云保存在./bin文件夹下。
import numpy as np
import os
import argparse
import pypcd
from tqdm import tqdm
def main():
## Add parser
parser = argparse.ArgumentParser(description="Convert .pcd to .bin")
parser.add_argument(
"--pcd_path",
help=".pcd file path.",
type=str,
default="./pcd"
)
parser.add_argument(
"--bin_path",
help=".bin file path.",
type=str,
default="./bin"
)
args = parser.parse_args()
## Find all pcd files
pcd_files = []
for (path, dir, files) in os.walk(args.pcd_path):
for filename in files:
# print(filename)
ext = os.path.splitext(filename)[-1]
if ext == '.pcd':
pcd_files.append(path + "/" + filename)
## Sort pcd files by file name
pcd_files.sort()
print("Finish to load point clouds!")
## Make bin_path directory
try:
if not (os.path.isdir(args.bin_path)):
os.makedirs(os.path.join(args.bin_path))
except OSError as e:
if e.errno != errno.EEXIST:
print ("Failed to create directory!!!!!")
raise
## Converting Process
print("Converting Start!")
for pcd_file in tqdm(pcd_files):
## Get pcd file
pc = pypcd.PointCloud.from_path(pcd_file)
## Generate bin file name
## bin_file_name = "{}_{:05d}.bin".format(args.file_name, seq)
pcd_name = pcd_file.split('/')[2]
bin_file_name = pcd_name.split('.')[0]+'.'+pcd_name.split('.')[1]+'.bin'
## print bin_file_name
bin_file_path = os.path.join(args.bin_path, bin_file_name)
## Get data from pcd (x, y, z, intensity, ring, time)
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
# np_r = (np.array(pc.pc_data['ring'], dtype=np.float32)).astype(np.float32)
# np_t = (np.array(pc.pc_data['time'], dtype=np.float32)).astype(np.float32)
## Stack all data
points_32 = np.transpose(np.vstack((np_x, np_y, np_z, np_i)))
## Save bin file
points_32.tofile(bin_file_path)
if __name__ == "__main__":
main()
拷贝代码写入到pcd2bin.py文件中,通过以下命令运行代码:
# []表示可省略
python pcd2bin.py [--pcd_path=./pcd] [--bin_path=./bin]
使用如下的代码对pcd格式的点云进行可视化:
import numpy as np
import open3d as o3d
from open3d import geometry
def main():
#创建窗口对象
vis = o3d.visualization.Visualizer()
#设置窗口标题
vis.create_window(window_name="kitti")
#设置点云大小
vis.get_render_option().point_size = 1
#设置颜色背景为黑色
opt = vis.get_render_option()
opt.background_color = np.asarray([0, 0, 0])
#################################################################################################
#读取点云文件,创建点云对象
pcd = o3d.io.read_point_cloud("./test.pcd")
#设置点的颜色为白色
pcd.paint_uniform_color([1,1,1])
#将点云加入到窗口中
vis.add_geometry(pcd)
vis.run()
vis.destroy_window()
if __name__=="__main__":
main()
使用如下代码对生成的bin格式点云进行可视化:
import numpy as np
import mayavi.mlab
# lidar_path换成自己的.bin文件路径
pointcloud = np.fromfile(str("./test.bin"), dtype=np.float32, count=-1).reshape([-1, 4])
x = pointcloud[:, 0] # x position of point
y = pointcloud[:, 1] # y position of point
z = pointcloud[:, 2] # z position of point
r = pointcloud[:, 3] # reflectance value of point
d = np.sqrt(x ** 2 + y ** 2) # Map Distance from sensor
degr = np.degrees(np.arctan(z / d))
vals = 'height'
if vals == "height":
col = z
else:
col = d
fig = mayavi.mlab.figure(bgcolor=(0, 0, 0), size=(640, 500))
mayavi.mlab.points3d(x, y, z,
col, # Values used for Color
mode="point",
colormap='spectral', # 'bone', 'copper', 'gnuplot'
# color=(0, 1, 0), # Used a fixed (r,g,b) instead
figure=fig,
)
mayavi.mlab.show()**加粗样式**