PCL学习笔记(五):PCD文件的IO操作
1、PCD文件读取点云数据
#include<iostream> #include<pcl/io/pcd_io.h> #include<pcl/point_types.h> int main(int argc,char **argv) { //声明点云对象指针(点云类型为XYZ点云结构),并初始化该对象,pointcloud是一个泛型类,所以每次使用的记得申明 //这一句代码的作用是创建一个真是参数为PointXYZ的PointCloud类型的boost共享指针并初始化 pcl::PiontCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); int flatMeathod = 1; if(flatMeathod ==0 ) { printf("采用pcl::io::loadPCDFile<pcl::PointXYZ>的方式读取\n"); //打开本地的点云文件test_pcd.pcd //PointCloud类中解析了pcd文件的各字段 //判断文件是否存在 if(pcl::io::loadPCDFile<pcl::PointXYZ>("/home/zll/source/test_pcd.pcd",*cloud)== -1) { PCL_ERROR("could not find test_pcd.pcd"); return -1; } } else { printf("采用pcl::PCLPointCloud2 的方式读取"); //整理需要先转换为PCLPointCloud2类型 pcl::PCLPointCloud2 cloud2; pcl::io::loadPCDFile("/home/zll/source/test_pcd.pcd",cloud2); if(pcl::io::loadPCDFile("",cloud2)==-1) { return -1; } else{ pcl::fromPCLPointCloud2(cloud2,*cloud); } } 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; } return 0; }
2、向PCD文件写入点云数据
#include<iostream> #include<pcl/io/pcd_io.h> #include<pcl/point_types.h> int main(int argc,char ** argv) { //实例化模板类PointCloud,每个点都被设置为pcl::PointXYZ,作为模板类实例化的参数 pcl::PointCloud<pcl::PointXYZ> cloud; //创建点云 cloud.width=5; cloud.height=1; cloud.is_dense=false; //Point字段=宽*高 cloud.points.resize(cloud.width*cloud.height); //用随机的值填充PointCloud点云对象 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); } //把PointCloud对象数据存储在test_pcd.pcd文件中 pcl::io::savePCDFileASCII("test_pcd.pcd",cloud); //打印输出存储的点云数据 std::cerr<<"Saved"<<cloud.points.size()<<"data points to test_pcd.pcd"<<std::endl; for(size_t i=0;i<cloud.points.size();++i) { std::cerr<<" "<<cloud.poitns[i].x<<cloud.points[i].y<<" "<<cloud.points[i].z<<std::endl; } return (0); }
3、两个点云中的字段或数据连接
进行操作前要确保两个数据集中字段的类型相同和维度相等
#include<iostream> #include<pcl/io/pcd_io.h> #include<pcl/point_types.h> int main(int argc,char **argv) { if(argc!=2) { std::cerr<<"please specifty Command line arg '-f' or '-p' "<<std::endl; exit(0); } pcl::PointCloud<pcl::PointXYZ>cloud_a,cloud_b,cloud_c; pcl::PointCloud<pcl::Normal>n_cloud_b; pcl::PointCloud<pcl::PointNormal>p_n_cloud_c; //三个输入(cloud_a,cloud_b和n_cloud_b),两个输出(cloud_c和p_n_cloud_c) cloud_a.width=5; cloud_a.height=cloud_b.height=n_cloud_b.height=1; cloud_a.points.resize(cloud_a.width*cloud_a.height); if(strcmp(argv[1],"-p")==0)//判断是否为连接a+b=c { cloud_b.width=3; cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height); } else{//如果是连接xyz与normal,则生成5个法线 n_cloud_b.width=5; n_cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height); } //循环生成无序点云,填充上面定义的两种类型点云对象 for(size_t i=0;i<cloud_a.points.size();++i) { cloud_a.points[i].x=1024*rand()/(RAND_MAX+1.0f); cloud_a.points[i].y=1024*rand()/(RAND_MAX+1.0f); cloud_a.points[i].z=1024*rand()/(RAND_MAX+1.0f); } if(strcmp(argv[1],"-p")==0) { //如果连接a+b=c,则cloud_b用两个点作为xyz数据 for(size_t i=0;i<cloud_b.points.size();++i) { cloud_b.points[i].x=1024*rand()/(RAND_MAX+1.0f); cloud_b.points[i].y=1024*rand()/(RAND_MAX+1.0f); cloud_b.points[i].z=1024*rand()/(RAND_MAX+1.0f); } } else { //如果连接xyz+normal=xyznormal,则n_cloud_b用5个点作为normal数据 for(size_t i=0;i<cloud_b.points.size();++i) { n_cloud_b.points[i].normal[0]=1024*rand()/(RAND_MAX+1.0f); n_cloud_b.points[i].normal[1]=1024*rand()/(RAND_MAX+1.0f); n_cloud_b.points[i].normal[2]=1024*rand()/(RAND_MAX+1.0f); } } //将cloud_a和cloud_b或n_cloud_b的数据输出 std::cerr<<"Cloud A:"<<std::endl; for(size_t i=0;i<cloud_a.points.size();++i) { std::cerr<<" "<<cloud_a.points[i].x<<" "<<cloud_a.points[i].y<<" "<<cloud_a.points[i].z<<std::endl; } std::cerr<<"Cloud B:"<<std::endl; for(size_t i=0;i<cloud_b.points.size();++i) { std::cerr<<" "<<cloud_b.points[i].x<<" "<<cloud_b.points[i].y<<" "<<cloud_b.points[i].z<<std::endl; } if(strcmp(argv[1],"-p")==0) { for(size_t i=0;i<cloud_b.points.size();++i) { std::cerr<<" "<<cloud_b.points[i]<<" "<<cloud_b.points[i]<<" "<<cloud_b.points[i]<<std::endl; } } else { for(size_t i=0;i<n_cloud_b.points.size();++i) { std::cerr<<" "<<n_cloud_b.points[i].normal[0]<<" "<<n_cloud_b.points[i].normal[1]<<" "<<n_cloud_b.points[i].normal[2]<<std::endl; } } if(strcmp(argv[1],"-p")==0) { //把cloud_a和cloud_b连接在一起创建了cloud_c cloud_c=cloud_a; cloud_c+=cloud_b; std::cerr<<"Cloud C: "<<std::endl; //输出 for(size_t i=0;i<cloud_c.points.size();++i) { std::cerr<<" "<<cloud_c.points[i].x<<" "<<cloud_c.points[i].y<<" "<<cloud_c.points[i].z<<std::endl; } } else { //把cloud_a和n_cloud_b字段连接在一起创建p_n_cloud_c pcl::concatenateFields(cloud_a,n_cloud_b,p_n_cloud_c); std::cerr<<"Cloud C: "<<std::endl; //输出 for(size_t i=0;i<p_n_cloud_c.points.size();++i) { std::cerr<< " "<<p_n_cloud_c.points[i].x<<" "<<p_n_cloud_c.points[i].y<<" "<<p_n_cloud_c.points[i].z<<" " <<p_n_cloud_c.points[i].normal[0]<<" "<<p_n_cloud_c.points[i].normal[1]<<" "<<p_n_cloud_c.points[i].normal[2]<<std::endl; } } return (0); }
参考:
https://www.cnblogs.com/codeAndlearn/p/11609364.html