头文件
import struct
创建一个pcd文件,并填写好注释
row=125
column=18
file_name1 = 'C:/Users/26943/Desktop/pointcloud/pointcloud.pcd'
file1=open(file_name1,'wb+')
data=['# .PCD v0.7 - Point Cloud Data file format\n',
'VERSION 0.7\n',
'FIELDS x y z dyn_prop id rcs vx vy vx_comp vy_comp is_quality_valid ambig_state x_rms y_rms invalid_state pdh0 vx_rms vy_rms\n',
'SIZE 4 4 4 1 2 4 4 4 4 4 1 1 1 1 1 1 1 1\n',
'TYPE F F F I I F F F F F I I I I I I I I\n',
'COUNT 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n',
'WIDTH 125\n',
'HEIGHT 1\n',
'VIEWPOINT 0 0 0 1 0 0 0\n',
'POINTS 125\n',
'DATA binary\n']
for i in data:
a=i.encode('utf-8')
b=file1.write(a)
注:注释可根据需求进行更改
读取txt中的的数据
file_name2 = 'C:/Users/26943/Desktop/pointcloud/TI.txt'
file2=open(file_name2,'r+')
point=[]
w=''
n=k=0
data=file2.read()
while 1:
if k<len(data)-1:
if data[k]!='\t'and data[k]!='\n' :
w+=data[k]
if (data[k-1]!='\t' and data[k-1]!='\n')and( data[k+1]=='\t'or data[k+1]=='\n'):
point.append(w)
w=''
elif (data[k-1]!='\t' or data[k-1]!='\n')and( data[k+1]=='\t'or data[k+1]=='\n'):
point.append(w)
w=''
else:
break
k+=1
print(k)
注:数据必须以回车或空格结束,假如以数字结束,需对代码进行修改。
对数据进行编码并写入pcd文件
unpacking_lut = {'F': {2: 'e', 4: 'f', 8: 'd'},
'I': {1: 'b', 2: 'h', 4: 'i', 8: 'q'},
'U': {1: 'B', 2: 'H', 4: 'I', 8: 'Q'}}
types=['F','F','F','I','I','F','F','F','F','F','I','I','I','I','I','I','I','I']
sizes=['4','4','4','1','2','4','4','4','4','4','1','1','1','1','1','1','1','1']
types_str = ''.join([unpacking_lut[t][int(s)] for t, s in zip(types, sizes)])
k=i=0
while i<row:
p=0
while p<column:
if k<len(point):
if types_str[p]=='f':
points=float(point[k])
elif types_str[p]=='b':
points=int(point[k])
elif types_str[p]=='h':
points=int(point[k])
point_binary = struct.pack(types_str[p], points)
file1.write(point_binary)
else:
break
k+=1
p+=1
i+=1
print(k)
l=0
while l<200:
x='\t'
x=x.encode('utf-8')
file1.write(x)
l+=1
file1.close()
file2.close()