点云不同数据格式的转化方法:ASC,PCD,PLY

点云不同数据格式的转化方法:ASC,PCD,PLY

最近在做PCL环境下的ICP配准,遇到了点云数据的格式问题,我的ICP代码是使用PLY格式的点云,其他格式运行会报错,所以做了一下转换点云格式的方法总结。
1.ASC转PCD
以下代码基于Python环境。步骤:
(1)将ASC文件中的数据复制到一个txt文件中;
(2)将txt点云数据和python程序文件放入文件夹;
(3)双击python程序文件,启动,会自动在文件夹下生成你需要的PCD 文件。(注意:代码中的文件路径要改)
如图:
在这里插入图片描述

import time
from sys import argv
#script,filename = argv
filename = "22.txt"
print ("the input file name is:%r." %filename)

start = time.time()
print("open the file.....")
file = open(filename,"r+")
count = 0

for line in file:
    count=count+1
print("size is %d" %count)
file.close()

#output = open("out.pcd","w+")
f_prefix = filename.split('.')[0]
output_filename = '{prefix}.pcd'.format(prefix=f_prefix)
output = open(output_filename,"w+")

list = ['# .PCD v.5 - Point Cloud Data file format\n','VERSION .5\n','FIELDS x y z\n','SIZE 4 4 4\n','TYPE F F F\n','COUNT 1 1 1\n']
output.writelines(list)
output.write('WIDTH ')
output.write(str(count))
output.write('\nHEIGHT ')
output.write(str(1))
output.write('\nPOINTS ')
output.write(str(count))
output.write('\nDATA ascii\n')

file1 = open(filename,"r")
all = file1.read()
output.write(all)
output.close()
file1.close()

end = time.time()
print("run time is:",end-start)

2.PCD转PLY
转换代码基于C++

#include<pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include<pcl/PCLPointCloud2.h>
#include<iostream>
#include<string>
using namespace pcl;
using namespace pcl::io;
using namespace std;
int PCDtoPLYconvertor(string & input_filename, string& output_filename)
{
 pcl::PCLPointCloud2 cloud;
 if (loadPCDFile(input_filename, cloud) < 0)
 {
  cout << "Error: cannot load the PCD file!!!" << endl;
  return -1;
 }
 PLYWriter writer;
 writer.write(output_filename, cloud, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), true, true);
 return 0;
}
int main()
{
 string input_filename = "22.pcd";
 string output_filename = "222.ply";
 PCDtoPLYconvertor(input_filename, output_filename);
   std::cout << "成功" << std::endl;
 return 0;
}

3.PLY转PCD
代码基于C++:

#include <iostream>
#include <pcl/common/io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/PolygonMesh.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_lib_io.h>

using namespace std;
int main()
{
 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
 pcl::PolygonMesh mesh;
 vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 pcl::io::loadPolygonFilePLY("curve29.ply", mesh);
 pcl::io::mesh2vtk(mesh, polydata);
 pcl::io::vtkPolyDataToPointCloud(polydata, *cloud);
 pcl::io::savePCDFileASCII("zhply.pcd", *cloud);
 return 0;
}


参考链接:

https://blog.csdn.net/ganguowa/article/details/53965304

https://blog.csdn.net/peach_blossom/article/details/78354641?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://blog.csdn.net/weixin_43712770/article/details/89402181

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值