背景:现有的pcd文件大小为2G,里面包含了多个不同时间的点云数据,现在需要将不同时间戳的点云数据单独抽取出来,作为一个独立的pcd文件。
方法:采用python逐行读取,并利用读取到的点云数据,修改原始点云头信息,输出一个标准的点云数据格式
def segment_pcd(pcd_src_path, pcd_dst_path):
header = []
body = []
pcd_file_name = 0
points = 0
with open(pcd_src_path, 'r', encoding='utf-8') as f:
i = -1
for line in f:
i += 1
# print(i)
if i < 11:
# if 'TYPE' in line:
# line = line.replace('I', 'F')
header.append(line)
else:
if pcd_file_name == 0:
pcd_file_name = line[-14:-1]
elif pcd_file_name != line[-14:-1]:
# 保存一份文件数据
for k in range(len(header)):
if 'WIDTH' in header[k]:
header[k] = header[k].replace(header[k][6:], str(len(body)) + '\n')
if 'POINTS' in header[k]:
header[k] = header[k].replace(header[k][7:], str(len(body)) + '\n')
pcd_file = header + body
with open(pcd_dst_path + pcd_file_name + '.pcd', 'w') as f2:
f2.writelines(pcd_file)
f2.close()
# 清空body数据
body = []
# 写入新的数据
pcd_file_name = line[-14:-1]
print(pcd_file_name)
body.append(line)
# 保存最后一份文件数据
for k in range(len(header)):
if 'WIDTH' in header[k]:
header[k] = header[k].replace(header[k][6:], str(len(body))+'\n')
if 'POINTS' in header[k]:
header[k] = header[k].replace(header[k][7:], str(len(body)) + '\n')
pcd_file = header + body
# with open(pcd_dst_path + pcd_file_name + '.pcd', 'w') as f2:
with open(pcd_dst_path, 'w') as f2:
f2.writelines(pcd_file)
f2.close()
f.close()
if __name__ == "__main__":
segment_pcd(r'points.pcd', './point_cloud/')