Point cloud processing: format conversion and visualization from pcd to bin
pcd to bin format conversion
In the Kitti dataset, the point cloud is stored in bin format. In the actual operation of the project, it is often necessary to convert the collected point cloud in pcd format into bin format to facilitate subsequent model training or testing. The following code implements the format conversion of the point cloud using the python language:
By default, the point cloud in pcd format is saved in the ./pcd folder, and the generated point cloud in bin format is saved in the ./bin folder.
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()
Copy the code and write it into the pcd2bin.py file, and run the code with the following command:
# []表示可省略
python pcd2bin.py [--pcd_path=./pcd] [--bin_path=./bin]
visualization
Use the following code to visualize the point cloud in pcd format:
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()
Use the following code to visualize the generated bin format point cloud:
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()**加粗样式**