txt转pcd并可视化

91 篇文章 106 订阅
本文介绍了一种将TXT格式的点云数据转换为PCD格式,并使用PCL库进行可视化的C++实现方法。通过定义TXT_Point_XYZ结构体读取TXT文件中的三维坐标,然后将这些坐标转换为PCL的PointCloud类型,最后保存为PCD文件并进行可视化展示。
摘要由CSDN通过智能技术生成

txt文件转为pcd文件并可视化

#include<iostream>

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

#include <pcl/visualization/pcl_visualizer.h>//可视化
using namespace std;

int main()
{
    //定义一种类型表示TXT中xyz
    typedef struct TXT_Point_XYZ
    {
        double x;
        double y;
        double z;
    }TOPOINT_XYZ;

    //读取txt文件
    int num_txt;
    FILE *fp_txt;
    TXT_Point_XYZ txt_points;
    vector<TXT_Point_XYZ> my_vTxtPoints;
    fp_txt = fopen("in.txt","r");

    if (fp_txt)
    {
        while (fscanf(fp_txt, "%lf %lf %lf", &txt_points.x, &txt_points.y, &txt_points.z) != EOF)
        {//将点存入容器尾部
            my_vTxtPoints.push_back(txt_points);
        }
    }
    else
        cout << "读取txt文件失败"<<endl;

    num_txt = my_vTxtPoints.size();

    //写入点云数据
    pcl::PointCloud<pcl::PointXYZ> ::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud->width = num_txt;
    cloud->height = 1;
    cloud->is_dense = false;
    cloud->points.resize(cloud->width*cloud->height);
    for (int i = 0; i < cloud->points.size(); ++i)
    {
        cloud->points[i].x = my_vTxtPoints[i].x;
        cloud->points[i].y = my_vTxtPoints[i].y;
        cloud->points[i].z = my_vTxtPoints[i].z;
    }
    pcl::io::savePCDFileASCII("out.pcd", *cloud);
    cout<< "从 txt_pcd.txt读取" << cloud->points.size() << "点写入txt_pcd.pcd" << endl;

    //打印出写入的点
    cout << "_________________________________" << endl;
    for (size_t i = 0; i < cloud->points.size(); ++i)
        cout << "    " << cloud->points[i].x
             << " " << cloud->points[i].y
             << " " << cloud->points[i].z << endl;
	cout<<"\n-------------------\n"<<"PointCloud Size: "<<cloud->size()<<endl;

    //可视化
    pcl::visualization::PCLVisualizer viewer("3D Viewer");


    int v2(0);
//    viewer.createViewPort(0,0,0.5,1,v2);
    viewer.setBackgroundColor(1,200,200);
    pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color2(cloud, 0,0,0);
    viewer.addPointCloud(cloud,color2,"cloud",v2);
    viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 9, "cloud");
    viewer.addText("cloud_normalized",0,0,"cloud",v2);

    viewer.addCoordinateSystem();
    while(!viewer.wasStopped())
    {
        viewer.spinOnce();
    }



    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值