#include<iostream>#include<pcl/io/pcd_io.h>#include<pcl/point_types.h>#include<pcl/filters/voxel_grid.h>usingnamespace std;voidpclDownsize(pcl::PointCloud<pcl::PointXYZ>::Ptr in, pcl::PointCloud<pcl::PointXYZ>::Ptr out){
pcl::VoxelGrid<pcl::PointXYZ> down_filter;float leaf =0.1;
down_filter.setLeafSize(leaf, leaf, leaf);
down_filter.setInputCloud(in);
down_filter.filter(*out);}//遍历文件夹获取文件夹下文件名voidgetFileNames(string path, vector<string>& files){
intptr_t hFile =0;struct_finddata_t fileinfo;
string p;if((hFile =_findfirst(p.assign(path).append("/*").c_str(),&fileinfo))!=-1){do{//如果是目录,递归查找//如果不是,把文件绝对路径存入vector中if((fileinfo.attrib & _A_SUBDIR)){if(strcmp(fileinfo.name,".")!=0&&strcmp(fileinfo.name,"..")!=0)getFileNames(p.assign(path).append("/").append(fileinfo.name), files);}else{
files.push_back(p.assign(path).append("/").append(fileinfo.name));}}while(_findnext(hFile,&fileinfo)==0);_findclose(hFile);}}voidpcd2txt(string in_file, string out_file){
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);// pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZ>);// Fill in the cloud data if(pcl::io::loadPCDFile<pcl::PointXYZ>(in_file,*cloud)==-1){PCL_ERROR("Couldn't read file \n");//return (0);}// pclDownsize(cloud,cloud_out);
cout <<"points cloud is successfully loaded! "<< endl;//for (size_t i = 0; i < cloud->points.size(); i++)// std::cout << " " << cloud->points[i].x << " " << cloud->points[i].y// <<" "<< cloud->points[i].z << std::endl;int Num = cloud->points.size();double*X =newdouble[Num]{0};double*Y =newdouble[Num]{0};double*Z =newdouble[Num]{0};
cout <<"size is : "<< cloud->points.size()<< endl;for(size_t i =0; i < cloud->points.size();++i){
X[i]= cloud->points[i].x;
Y[i]= cloud->points[i].y;
Z[i]= cloud->points[i].z;//cout << "first " << i << " of " << Num << endl;}
ofstream zos(out_file);for(int i =0; i < cloud->points.size(); i++){
zos << X[i]<<" "<< Y[i]<<" "<< Z[i]<< endl;//cout << "second " << i << " of " << Num << endl;}
cout <<"trans has done!!!"<< endl;//cin.get();}intmain(int argc,char*argv[]){
vector<string> fileNames;
string path("E:/DataSet/points");getFileNames(path, fileNames);for(constauto&ph : fileNames){
std::cout <<"ph: "<< ph <<"\n";//不带路径的文件名
string::size_type iPos = ph.find_last_of("/")+1;
string filename = ph.substr(iPos, path.length()- iPos);
cout <<"filename: "<< filename << endl;//不带后缀的文件名
string name = filename.substr(0, filename.rfind("."));
cout <<"name: "<< name << endl;pcd2txt(ph,"E:/DataSet/bin/"+ name +".txt");}return0;}
3. python PCD2TXT
import os
from os import listdir, path
path_str ='E:/desktop/PointCloud/OpenPCDet/data/carton/testing/points'# your directory path
txts =[f for f in listdir(path_str)if f.endswith('.pcd')and path.isfile(path.join(path_str, f))]for txt in txts:withopen(os.path.join(path_str, txt),'rb')as f:
lines = f.readlines()withopen(os.path.join(path_str,os.path.splitext(txt)[0]+".txt"),'w')as f:
line =str(line)
f.write(''.join(lines[11:]))"""
import os
#定义一个三维点类
class Point(object):
def __init__(self,x,y,z):
self.x = x
self.y = y
self.z = z
points = []
path = 'E:/desktop/PointCloud/OpenPCDet/data/carton/testing/points/cloudnorm_000002'
#读取pcd文件,从pcd的第12行开始是三维点
with open(path+'.pcd') as f:
for line in f.readlines()[11:len(f.readlines())-1]:
strs = line.split(' ')
points.append(Point(strs[0],strs[1],strs[2].strip()))
##strip()是用来去除换行符
##把三维点写入txt文件
fw = open(path+'.txt','w')
for i in range(len(points)):
linev = points[i].x+" "+points[i].y+" "+points[i].z+"\n"
fw.writelines(linev)
fw.close()
import math
import os
import pcl
def txt2pcd(filename):
xlist = []
ylist = []
zlist = []
with open(filename, 'r') as file_to_read:
while True:
lines = file_to_read.readline()
if not lines:
break
pass
x, y, z = [float(i) for i in lines.split(' ')]
print(str(x) + ' ' + str(y) + ' ' + str(z))
xlist.append(x)
ylist.append(y)
zlist.append(z)
savefilename = './test_1.pcd'
if not os.path.exists(savefilename):
f = open(savefilename, 'w')
f.close()
with open(savefilename, 'w') as file_to_write:
file_to_write.writelines("# .PCD v0.7 - Point Cloud Data file format\n")
file_to_write.writelines("VERSION 0.7\n")
file_to_write.writelines("FIELDS x y z\n")
file_to_write.writelines("SIZE 4 4 4\n")
file_to_write.writelines("TYPE F F F\n")
file_to_write.writelines("COUNT 1 1 1\n")
file_to_write.writelines("WIDTH " + str(len(xlist)) + "\n")
file_to_write.writelines("HEIGHT 1\n")
file_to_write.writelines("VIEWPOINT 0 0 0 1 0 0 0\n")
file_to_write.writelines("POINTS " + str(len(xlist)) + "\n")
file_to_write.writelines("DATA ascii\n")
for i in range(len(xlist)):
file_to_write.writelines(str(xlist[i]) + " " + str(ylist[i]) + " " + str(zlist[i]) + "\n")
def pcd2txt(pcdfile):
p = pcl.load(pcdfile)
savetxtfile = './test_1_inliers.txt'
with open(savetxtfile, 'w') as f:
f.write(str(p.size) + 'points in total' + '\n')
# print(p[i])
for i in range(p.size):
x = str(p[i][0])
y = str(p[i][1])
z = str(p[i][2])
f.write(x + ' ' + y + ' ' + z + '\n')
if __name__ == '__main__':
txtfile = './3dposition.txt'
pcdfile = './pcldata/tutorials/test_1.pcd'
txt2pcd(txtfile)
pcd2txt(pcdfile)
"""