本文主要是介绍点云库(PCL)学习——I/O,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.PCD(Point Cloud Data)文件格式
1.PCD并不是第一个支持三维点云的格式。特别是计算机图形学和计算几何界,已经创造了许多格式来描述使用激光扫描仪获得的任意多边形和点云。比如:PLY,STL,OBJ,X3D等。在当今的传感器技术以及算法被发明之前,这些格式就因为不同的目的在不同的时间相继被创造出来,它们也存在着多多少少的缺陷。
2.文件格式头文件:每个PCD文件都包含了一个头文件来定义和声明点云数据的某些属性(properties)并存储在文件里。PCD的头文件必须用ASCII编写。
NOTE:每个头文件条目(header entry)以及ASCII点云数据都在PCD文件中明确,并以行隔开。
0.7版本的PCD头文件包含以下条目:VERSION、FIELDS、SIZE、TYPE、COUNT、WIDTH、HEIGHT、VIEWPOINT、POINTS、D,头文件条目必须严格按照上述顺序指定标题条目。
2.从PCD文件中读取点云数据
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>int
main (int argc, char** argv)
{pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);//创建一个PointCloud<PointXYZ>boost共享指针并初始化if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file{PCL_ERROR ("Couldn't read file test_pcd.pcd \n");//从磁盘加载PointCloud数据return (-1);}std::cout << "Loaded "<< cloud->width * cloud->height<< " data points from test_pcd.pcd with the following fields: "<< std::endl;for (const auto& point: *cloud)std::cout << " " << point.x<< " " << point.y<< " " << point.z << std::endl;//显示加载的数据return (0);
}
3.向PCD文件中写点云数据
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>intmain (int argc, char** argv)
{pcl::PointCloud<pcl::PointXYZ> cloud;// Fill in the cloud datacloud.width = 5;cloud.height = 1;cloud.is_dense = false;cloud.points.resize (cloud.width * cloud.height);for (auto& point: cloud){point.x = 1024 * rand () / (RAND_MAX + 1.0f);point.y = 1024 * rand () / (RAND_MAX + 1.0f);point.z = 1024 * rand () / (RAND_MAX + 1.0f);}pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);std::cerr << "Saved " << cloud.size () << " data points to test_pcd.pcd." << std::endl;for (const auto& point: cloud)std::cerr << " " << point.x << " " << point.y << " " << point.z << std::endl;return (0);
}
4.连接(concatenate)两个点云的点
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>int main (int argc, char** argv)
{if (argc !=2) //限制参数为2个{std::cerr << "please specify command line arg '-f' or '-p'" << std::endl;exit(0);}pcl::PointCloud<pcl::PointXYZ> cloud_a, cloud_b, cloud_c;plc::PointCloud<pcl::Normal> n_cloud_b;pcl::PointCloud<pcl::PointNormal> p_n_cloud_c;//Fill in the cloud datacloud_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){cloud_b.width = 3;cloud_b.points.resize(cloud_b.width*cloud_b.height);}else{n_cloud_b.width = 5;n_cloud_b.points.resize(n_cloud_b.width*n_cloud_b.height);}for (std::size_t i = 0; i<cloud_a.size(); ++i){cloud_a[i].x = 1024*rand()/(RAND_MAX + 1.0f);cloud_a[i].y = 1024*rand()/(RAND_MAX + 1.0f);cloud_a[i].z = 1024*rand()/(RAND_MAX + 1.0f);}if (strcmp(argv[1], "-p") == 0)for (std::size_t i = 0; i<cloud_b.size(); ++i){cloud_b[i].x = 1024*rand()/(RAND_MAX + 1.0f);cloud_b[i].y = 1024*rand()/(RAND_MAX + 1.0f);cloud_b[i].z = 1024*rand()/(RAND_MAX + 1.0f);}elsefor (std::size_t i = 0; i<n_cloud_b.size(); ++i){n_cloud_b[i].normal[0] = 1024*rand()/ (RAND_MAX + 1.0f);n_cloud_b[i].normal[1] = 1024*rand()/ (RAND_MAX + 1.0f);n_cloud_b[i].normal[2] = 1024*rand()/ (RAND_MAX + 1.0f);}std::cerr<<"Cloud A : " << std::endl;for (std::size_t i = 0; i<cloud_a.size(); ++i)std::cerr<< " " << cloud_a[i].x << " " << cloud_a[i].y << " " << cloud_a[i].z << std::endl;std::cerr<<"Cloud B: " << std::endl;if (strcmp(argv[1], "-p") == 0)for (std::size_t i = 0; i<cloud_b.size (); ++i)std::cerr<<" " << cloud_b[i].x << " " << cloud_b[i].y << " " << cloud_b[i].z << std::endl;elsefor (std::size_t i = 0; i < n_cloud_b.size (); ++i)std::cerr << " " << n_cloud_b[i].normal[0] << " " << n_cloud_b[i].normal[1] << " " << n_cloud_b[i].normal[2] << std::endl;if (strcmp(argv[1], "-p") == 0){cloud_c = cloud_a;cloud_c += cloud_b;std::cerr << "Cloud C: " << std::endl;for (std::size_t i = 0; i < cloud_c.size (); ++i)std::cerr << " " << cloud_c[i].x << " " << cloud_c[i].y << " " << cloud_c[i].z << " " << std::endl;}else{pcl::concatenateFields (cloud_a, n_cloud_b, p_n_cloud_c);std::cerr << "Cloud C: " << std::endl;for (std::size_t i = 0; i < p_n_cloud_c.size (); ++i)std::cerr << " " <<p_n_cloud_c[i].x << " " << p_n_cloud_c[i].y << " " << p_n_cloud_c[i].z << " " << p_n_cloud_c[i].normal[0] << " " << p_n_cloud_c[i].normal[1] << " " << p_n_cloud_c[i].normal[2] << std::endl;}reture (0);
}
这篇关于点云库(PCL)学习——I/O的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!