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');
推荐阅读
-
XPath 结合 requests 爬取网站 存储格式可选 有json、csv、txt
-
python实现txt文件格式转换为arff格式
-
python将txt文件(ASCII码格式),转化为16进制文件
-
Python使用pydub库对mp3与wav格式进行互转的方法
-
曦力音视频转换专家怎么用如何进行音视频格式互转
-
[20180918]文件格式与sql_id.txt
-
XPath 结合 requests 爬取网站 存储格式可选 有json、csv、txt
-
阅读电子书感觉到眼睛累平板电脑如何语音播放TXT格式的文本
-
Python读取txt内容写入xls格式excel中的方法
-
网页文字保存成Txt电子书不带有任何格式的方法