Python txt转pcd(带RGB值,点云)

1、有numpy就能运行,假如需要转换的TXT文件有header,需要删除header才能运行;
2、直接运行代码,按运行提示输入TXT文件路径和PCD文件保存路径即可,不需要修改代码。

import os
import sys
import numpy as np

def creat_pcd(input_path, output_path):
    
    #Lodaing txt
    Full_Data = np.loadtxt(input_path)
    
    #Creating pcd
    if os.path.exists(output_path):
        os.remove(output_path)
    Output_Data = open(output_path, 'a')
    Output_Data.write('# .PCD v0.7 - Point Cloud Data file format\nVERSION 0.7\nFIELDS x y z rgba\nSIZE 4 4 4 4\nTYPE F F F U\nCOUNT 1 1 1 1')
    string = '\nWIDTH ' + str(Full_Data.shape[0])
    Output_Data.write(string)
    Output_Data.write('\nHEIGHT 1\nVIEWPOINT 0 0 0 1 0 0 0')
    string = '\nPOINTS ' + str(Full_Data.shape[0])
    Output_Data.write(string)
    Output_Data.write('\nDATA ascii')
    for j in range(Full_Data.shape[0]):
        R=Full_Data[j,3]
        G=Full_Data[j,4]
        B=Full_Data[j,5]
        value = (int(R) << 16 | int(G) << 8 | int(B))
        string = ('\n' + str(Full_Data[j,0]) + ' ' + str(Full_Data[j, 1]) + ' ' +str(Full_Data[j, 2]) + ' ' + str(value))
        Output_Data.write(string)
    Output_Data.close()
    
    print('--------------Completed--------------')

a = input("请输入TXT文件路径:")#文件路径中斜杆使用"/",比如:D:/pcl/points.txt
b = input("请输入PCD文件保存路径:")#比如:D:/pcl/points.pcd

creat_pcd(a, b)

参考链接

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值