首页 > 其他分享 >点云txt文件转pcd文件

点云txt文件转pcd文件

时间:2024-07-22 08:57:22浏览次数:8  
标签:size 文件 include cout pointCloud myPoint 点云 txt points

基于C++ 和pcl实现以下格式的点云txt文档转pcd格式。

使用qt 的console实现:

#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QDirIterator>

#include <iostream>
#include <fstream>
#include <strstream>
#include <vector>
#include <pcl/io/pcd_io.h>  //文件输入输出
#include <pcl/point_types.h>  //点类型相关定义
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;


bool readTxtFile(const string &fileName, const char tag, const PointCloudT::Ptr &pointCloud)
{
    cout << "reading file start..... " << endl;
    ifstream fin(fileName);
    string linestr;
    vector<PointT> myPoint;
    while (getline(fin, linestr))
    {
        vector<string> strvec;
        string s;
        stringstream ss(linestr);
        while (getline(ss, s, tag))
        {
            strvec.push_back(s);
        }
        if (strvec.size() < 3){
            cout << "格式不支持" << endl;
            return false;
        }
        PointT p;
        p.x = stod(strvec[0]);
        p.y = stod(strvec[1]);
        p.z = stod(strvec[2]);
        myPoint.push_back(p);
    }
    fin.close();

    //转换成pcd
    pointCloud->width = (int)myPoint.size();
    pointCloud->height = 1;
    pointCloud->is_dense = false;
    pointCloud->points.resize(pointCloud->width * pointCloud->height);
    for (int i = 0; i < myPoint.size(); i++)
    {
        pointCloud->points[i].x = myPoint[i].x;
        pointCloud->points[i].y = myPoint[i].y;
        pointCloud->points[i].z = myPoint[i].z;
    }
    cout << "reading file finished! " << endl;
    cout << "There are " << pointCloud->points.size() << " points!" << endl;
    return true;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString dirPath = "/home/csl/livox_txt/7.27textfile/end/txt";
    QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs, QDirIterator::Subdirectories);
    while(it.hasNext())
    {
          it.next();
          if(it.fileInfo().suffix() == "txt")
          {
              QString basename = it.fileInfo().baseName();
              QString strNew = QString("%1").arg(basename.toInt(), 4, 10, QLatin1Char('0'));
             
              QString fullname = it.fileInfo().absoluteFilePath();
            
              qDebug() << fullname;

              PointCloudT::Ptr cloud(new PointCloudT);
              readTxtFile(fullname.toStdString(),' ',cloud);

            

              //3.点云输出
              QString out_dir = "/home/csl/livox_txt/7.27textfile/end/pcd/";
              QString out_ex =".pcd";
              QString pcd_name = out_dir + strNew+out_ex;
              pcl::PCDWriter writer;
              writer.writeASCII<PointT>(pcd_name.toStdString(), *cloud);

          }
    }
    return a.exec();
}

标签:size,文件,include,cout,pointCloud,myPoint,点云,txt,points
From: https://blog.csdn.net/qq_33912182/article/details/140531559

相关文章

  • Selenium 没有打开我需要的配置文件
    fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServicefromselenium.webdriver.chrome.optionsimportOptionsimporttimechrome_options=Options()chrome_options.add_argument("user-data-dir=C:\\Users\\MyName\\A......
  • 目标文件的对称拟合问题
    我正在构建一个包含6列和6700行的数据集。该数据是从不同研究中的各种蒙特卡罗模拟中提取的光子剂量转换系数获得的。数据中的列包括能量、器官名称、器官质量、器官密度、AP剂量、PA剂量和侧向剂量。能量行代表从1keV到20MeV的每个能量仓的计算剂量,分为20个仓。每......
  • 安装程序运行所需的二进制文件的最用户友好的方式是什么?
    我正在创建一个使用curses模块的python程序,该模块本身不能在Windows上运行。值得庆幸的是,您可以在Windows上安装一个现有的二进制文件以便能够使用Curses,但这对用户来说很不方便。我的问题是为用户安装Curses-for-Windowss的最简单方法是什么?计划的一部分?我应该编写自己......
  • HDF5 与包含文件的文件夹有何不同?
    我正在开发一个开源项目处理向文件夹添加元数据。提供的(Python)API允许您浏览和访问元数据,就像它只是另一个文件夹一样。因为它只是另一个文件夹。\folder\.meta\folder\somedata.json然后我遇到了HDF5及其派生Alembic在书中阅读HDF5......
  • 使用案例显示时在“.txt”文件中打印现有待办事项时出现名称错误
    我正在尝试使用以下代码中给出的案例显示将现有的待办事项保存在我的txt文件中:whileTrue:user_action=input("Typeadd,show:")user_action=user_action.strip()matchuser_action:case'add':todo=input("Enteranytodo:")+......
  • NBT 解析器 Minecraft mca 文件不是 gzip 压缩文件错误
    我尝试使用NBT1.4.1模块(命名二进制标签读取器/写入器)从文件系统和.mca区域/anvil文件中使用Python读取Minecraft世界,该模块应该读取NBT格式在《我的世界》中使用。它对于level.dat等文件工作正常,但对于r.0.0.mca等区域文件会抛出错误编辑:我指的是Min......
  • 如何更快地比较同一 csv 文件中的行
    我有一个csv文件,包含720,000行和10列,与问题相关的列是['timestamp_utc','looted_by__name','item_id','quantity']此文件是人们在游戏中掠夺地面的物品的日志,问题是有时地面虫子和类型在两个不同的行中两次掠夺相同物品的人(这两行最多可以相隔5行),在'tim......
  • 文件操作(几乎最全)
    文章目录1.为什么使用文件2.什么是文件2.1程序文件2.2数据文件2.3文件名3.二进制文件和文本文件4.文件的打开和关闭4.1流和标准流4.1.1流4.1.2标准流4.2文件指针4.3文件的打开和关闭5.文件的顺序读写5.1顺序读写函数介绍5.2对比一组函数6.文件的随机......
  • Response下载文件
    1.首先在maven项目下创建javaclass类,然后继承HttpServlet接口。重写doGet方法。2.代码如下点击查看代码importjavax.servlet.ServletException;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServl......
  • 内核模块高级-多文件编译、加载、参数传递、模块依赖
    多文件编译    对于比较复杂的驱动程序,常常会把它的功能进行拆分,由不同的文件实现,这样也能进行并行开发,缩短开发周期。多文件编译的简单例子如下:mod.c://mod.c#include<linux/init.h>#include<linux/module.h>#include"ext.h"//其他文件的头文件ext.hstatic......