#include<iostream>
#include<pcl/io/pcd_io.h>
#include<pcl/console/print.h>
#include<pcl/console/parse.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <boost/algorithm/string.hpp>
#include <boost/thread/thread.hpp>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;
bool loadCloud(const string& filename, PointCloud<PointXYZ>& cloud)
{
ifstream fs;
fs.open(filename.c_str(), ios::binary);
if (!fs.is_open() || fs.fail())
{
PCL_ERROR("Could not open file '%s'! Error : %s\n", filename.c_str(), strerror(errno));
fs.close();
return(false);
}
string line;
vector<string> st;
while (!fs.eof())
{
getline(fs, line);
if (line.empty())
{
continue;
}
boost::trim(line);
// #include <boost/algorithm/string.hpp>包含头文件,才可以使用split分割。
boost::split(st, line, boost::is_any_of(" "), boost::token_compress_on);
// 分割符是空格。
//cout << "st.size() = " << st.size() << endl;
if (st.size() != 3)
continue;
pcl::PointXYZ point;
point.x = float(atof(st[0].c_str()));
point.y = float(atof(st[1].c_str()));
point.z = float(atof(st[2].c_str()));
cloud.push_back(point);
}
fs.close();
cloud.width = uint32_t(cloud.size());
cloud.height = 1;
cloud.is_dense = true;
return (true);
}
PointCloud<PointXYZ> cloud;
if (!loadCloud(XYZfileName, cloud))
return (-1);
cout << cloud.size() << endl;
PointCloud<PointXYZ>::Ptr cloud_ptr(new PointCloud<PointXYZ>());
savePCDFileASCII(PCDfileName, cloud);
使用pcl库读取.xyz文件并生成点云数据
于 2022-12-02 16:37:41 首次发布