三维点云数据pcd与ply格式相互转换/点云合并

PCL配置与下载参考:https://blog.csdn.net/stq054188/article/details/106408641

① ply转pcd

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/io/vtk_io.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <pcl/visualization/cloud_viewer.h>  
#include <pcl/conversions.h>
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;

int main()
{
	pcl::PCLPointCloud2 point_cloud2;
	pcl::PLYReader reader;
	reader.read("reconstructed_2_1.ply", point_cloud2);
	pcl::PointCloud<pcl::PointXYZ> point_cloud;
	pcl::fromPCLPointCloud2(point_cloud2, point_cloud);
	pcl::PCDWriter writer;
	writer.writeASCII("reconstructed_2_1.pcd", point_cloud);
	cout << "Done!" << endl;
	return 0;
}

② pcd转ply

#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 = "./reconstructed_1_reconstructed_2_1.pcd";
	string output_filename = "./reconstructed_1_reconstructed_2_1.ply";
	PCDtoPLYconvertor(input_filename, output_filename);
	cout << "Done!" << endl;
	return 0;
}
 

③ 点云合并

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

using namespace std;  // 可以加入 std 的命名空间

int main2(int argc, char** argv)
{
	string ReviseName;
	cout << "是否已经修改输出文件的名称和K值?请输入Y或N。" << endl;
	cin >> ReviseName;
	if (ReviseName != "Y")
	{
		return (-1);//跳出整个程序
	}
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);	// 总点

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);	// 点云1
	pcl::PCDReader reader;
	reader.read<pcl::PointXYZ>("reconstructed_1.pcd", *cloud1);//读取pcd文件,用指针传递给cloud。

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>);	// 点云2
	reader.read<pcl::PointXYZ>("reconstructed_2_1.pcd", *cloud2);//读取pcd文件,用指针传递给cloud。


	//拷贝点云数据

	*cloud = *cloud1;
	*cloud += *cloud2;

	//输出时所用离群点的名字
	string name_out1 = "reconstructed_1_";   //因为string变量自身就带着隐含的双引号了,所以不用特意加双引号
	string name_out2 = "reconstructed_2_1.pcd";
	string name_out = name_out1; name_out += name_out2; 
	cout << name_out << endl;

	pcl::PCDWriter writer;
	writer.write<pcl::PointXYZ>(name_out, *cloud, false);//滤波后内点(主体点)
	cout << "点云合并完成!" << endl;

	return(0);
}

 

上一篇:基于PCL的程序报0Xc000007b或0Xc0000142错


下一篇:Ubuntu16.04安装PCL