【PCL学习】将pcd格式点云与txt格式文本互转

PCL中将pcd格式点云与txt转换

本文参考了之前有大神写的数据格式转换txt2pcd一文,在进行简单修改之后实现了pcd转txt的代码,欢迎大家指教。


格式转换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 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 cloud;
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    // 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;
}

格式转换pcd2txt

#include 
#include   
#include   

using namespace std;

int main(int argc, char *argv[])
{
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    // Fill in the cloud data  
    if (pcl::io::loadPCDFile("chuli.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("chuli.txt");
    for (int i = 0; i" " << Y[i] << " " << Z[i] << endl;
    }
    cout << "trans has done!!!" << endl;
    cin.get();
    return 0;
}

你可能感兴趣的:(PCL,C++)