欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

txt格式、Pcd格式、ply格式互转

程序员文章站 2022-03-30 22:08:39
...

1、TXT转PCD

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

#include <pcl/visualization/pcl_visualizer.h> 

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("za.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("txt2pcd_bunny1.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;
}

2、pcd转txt

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

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>("F:\\2020.11.9\\体积检测图片\\pointcloud_pefect.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("F:\\2020.11.9\\体积检测图片\\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、pcd转ply-matlab

ptCloud = pcread('F:\\2020.11.9\\体积检测图片\好的点云\\sgbm_self\\point_final.pcd');
pcwrite(ptCloud,'F:\\2020.11.9\\体积检测图片\好的点云\\sgbm_self\\point_final.ply');
ptCloud=pcread('F:\\2020.11.9\\体积检测图片\好的点云\\sgbm_self\\point_final.ply');
pcshow('F:\\2020.11.9\\体积检测图片\好的点云\\sgbm_self\\point_final.ply');