3.PLY文件读写

目录

1.利用读写类读写ply格式点云

1.1使用步骤

1.1.1.读取

1.1.2.保存

2.利用读写函数读写ply的格式点云

2.1使用步骤

2.1.1.读取

2.2.2.保存

1.利用读写类读写ply格式点云

        pcl::PLYReader是PCL库中用于读取PLY格式文件的类。 pcl::PLYWriter 是 PCL 库中用于将点云数据写入 PLY 文件格式的类。主要作用就是将 PCL 中的点云数据写入 PLY 文件格式中。使用时要加入头文件#include<pcl/io/ply_io.h>

1.1使用步骤

1.1.1.读取
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
 
int main(int argc, char **argv) {
    // 准备pcl::PointXYZ类型的点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    // 将pcd中的数据加载到cloud中
		pcl::PLYReader plyRead;
		
    if (plyRead.read("bunny.ply", *cloud) == -1) //* load the file
    {
        PCL_ERROR ("Couldn't read file bunny.ply \n");
        return (-1);
    }
    std::cout << "点云数量"
              << cloud->width * cloud->height
              << std::endl;
    return (0);
}
1.1.2.保存
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
 
int
main(int argc, char **argv) {
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new  pcl::PointCloud<pcl::PointXYZ>);
 
    // 设置点云参数
    cloud->width = 500;
    cloud->height = 1;
    cloud->is_dense = false;
    cloud->points.resize(cloud.width * cloud.height);
 
 
    // 随机生成500个点
    for (size_t i = 0; i < cloud.points.size(); ++i) {
        cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }
 
    pcl::PLYWriter plyWriter;
	plyWriter.writeASCII("test.ply",*cloud);
    std::cerr << "保存了: " << cloud->points.size() << " 点" << std::endl;
 
    return (0);
}

2.利用读写函数读写ply的格式点云

        loadPLYFile是 PCL 库中用于读取 .ply 格式点云数据文件的函数。 savePLYFile将点云数据保存为PLY格式文件的函数。使用时要加入头文件#include<pcl/io/ply_io.h>。

2.1使用步骤

2.1.1.读取
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
 
int main(int argc, char **argv) {
    // 准备pcl::PointXYZ类型的点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    // 将pcd中的数据加载到cloud中
    if (pcl::io::loadPLYFile<pcl::PointXYZ>("bunny.ply", *cloud) == -1) //* load the file
    {
        PCL_ERROR ("Couldn't read file bunny.ply \n");
        return (-1);
    }
    std::cout << "点云数量"
              << cloud->width * cloud->height
              << std::endl;
    return (0);
}
2.2.2.保存
#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
 
int
main(int argc, char **argv) {
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new  pcl::PointCloud<pcl::PointXYZ>);
 
    // 设置点云参数
    cloud->width = 500;
    cloud->height = 1;
    cloud->is_dense = false;
    cloud->points.resize(cloud.width * cloud.height);
 
 
    // 随机生成500个点
    for (size_t i = 0; i < cloud.points.size(); ++i) {
        cloud->points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud->points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }
 
    pcl::io::savePLYFileASCII("test_ply.ply", cloud);
    std::cerr << "保存了: " << cloud->points.size() << " 点" << std::endl;
 
    return (0);
}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

别叭叭儿—好好学

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值