使用pcl库读取.xyz文件并生成点云数据

#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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值