用python读取数据并生成点云文件

头文件

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()
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值