点云的pcd、txt、ply之间的格式互转

点云的pcd、txt、ply之间的格式互转

1.新建一个文件夹,自命名
2.在新建文件夹中分别新建一个CMakeLists.txt文本文档,scr文件夹
3.在scr文件夹里新建pcd2txt.cpp、txt2pcd.cpp、pcd2ply.cpp、ply2pcd.cpp等cpp文件
各文档内容如下
(1)CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(CMAKE_BUILD_TYPE "Release")
# 添加c++ 11标准支持
set(CMAKE_CXX_FLAGS "-std=c++11 -O2")

find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})

add_executable (pcd2txt src/pcd2txt.cpp) #
target_link_libraries(pcd2txt ${PCL_LIBRARIES})

add_executable (txt2pcd src/txt2pcd.cpp) #
target_link_libraries(txt2pcd ${PCL_LIBRARIES})


add_executable (pcd2ply src/pcd2ply.cpp) #
target_link_libraries(pcd2ply ${PCL_LIBRARIES})

add_executable (ply2pcd src/ply2pcd.cpp) #
target_link_libraries(ply2pcd ${PCL_LIBRARIES})

(2).pcd2txt.cpp

#include 
#include   
#include   

using namespace std;

int main(int argc, char *argv[])
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	// Fill in the cloud data  
	if (pcl::io::loadPCDFile<pcl::PointXYZ>("map.pcd", *cloud) == -1)
	{
		PCL_ERROR("Couldn't read file chuli.pcd\n");
		return (-1);
	}
	//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 = new double[Num] {0};
	double *Y = new double[Num] {0};
	double *Z = new double[Num] {0};
	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;
	}

	ofstream zos("pcd2txt.txt");
	for (int i = 0; i<Num; i++)
	{
		zos << X[i] << " " << Y[i] << " " << Z[i] << endl;
	}
	cout << "trans has done!!!" << endl;
	cin.get();
	return 0;
}


(3)txt2pcd**

#include 
#include  
#include   
#include   
#include   
#include   

#include  

using namespace std;
int main()
{
    typedef struct tagPOINT_3D
    {
        double x;  //mm world coordinate x  
        double y;  //mm world coordinate y  
        double z;  //mm world coordinate z  
        double r;
    }POINT_WORLD;


    //加载txt数据  
    int number_Txt;
    FILE *fp_txt;
    tagPOINT_3D TxtPoint;
    vector<tagPOINT_3D> m_vTxtPoints;
    fp_txt = fopen("pcd2txt.txt", "r");
    if (fp_txt)
    {
        while (fscanf(fp_txt, "%lf %lf %lf", &TxtPoint.x, &TxtPoint.y, &TxtPoint.z) != EOF)
        {
            m_vTxtPoints.push_back(TxtPoint);
        }
    }
    else
        cout << "txt数据加载失败!" << endl;
    number_Txt = m_vTxtPoints.size();
    //pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    // Fill in the cloud data  
    cloud->width = number_Txt;
    cloud->height = 1;
    cloud->is_dense = false;
    cloud->points.resize(cloud->width * cloud->height);
    for (size_t i = 0; i < cloud->points.size(); ++i)
    {
        cloud->points[i].x = m_vTxtPoints[i].x;
        cloud->points[i].y = m_vTxtPoints[i].y;
        cloud->points[i].z = m_vTxtPoints[i].z;
    }
    pcl::io::savePCDFileASCII("map1.pcd", *cloud);
    std::cerr << "Saved " << cloud->points.size() << " data points to txt2pcd.pcd." << std::endl;

    //for (size_t i = 0; i < cloud.points.size(); ++i)
    //  std::cerr << "    " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;

    //PCL Visualizer
    // Viewer  
    pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
    viewer.addPointCloud(cloud);
    viewer.setBackgroundColor(1, 0.5, 1);

    viewer.spin();
    system("pause");
    return 0;
}

(4)pcd2ply**

#include             
#include  
#include    
#include 

int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	// 读取点云
	if (pcl::io::loadPCDFile<pcl::PointXYZ>("map.pcd", *cloud) == -1) 
	{
		PCL_ERROR("Are you sure you are right?\n");
		return (-1);
	}
	pcl::PLYWriter writer;
	writer.write("map.ply", *cloud); 

	return (0);
}

(5)ply2pcd**

#include              
#include     // pcd相关头文件
#include   // 所有点类型定义头文件
#include     // ply类相关头文件

int main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	//加载文件
	if (pcl::io::loadPLYFile<pcl::PointXYZ>("map.ply", *cloud) == -1)
	{
		PCL_ERROR("就你输入的那些渣渣,你TM确定能读取成功? \n");
		return (-1);
	}
	// 输出点的个数
	std::cout << "point number: " << cloud->width * cloud->height << std::endl;
	pcl::PCDWriter writer;
	writer.write("map.pcd", *cloud);  //保存文件

	return (0);
}

最后新建build文件,编译运行
点云的pcd、txt、ply之间的格式互转_第1张图片
点云的pcd、txt、ply之间的格式互转_第2张图片

你可能感兴趣的:(c#)