PCL提供了两种pcd点云读写方式,其中PCD(Point Cloud Date,点云数据)对应的文件格式为 (*.pcd),是 PCL官方指定格式,具有 ASCII 和 Binary 两种数据存储类型。
其中 ASCII 格式的点云可以直接用记事本查看;
Binary 格式的点云无法用记事本查看,但速度更快。
方式1 PCDReader;PCDWriter
方式2 loadPCDFile;savePCDFile
1、从硬盘中读取点云
1.1 loadPCDFile
#include
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud); //创建存放读取点云的对象
if (pcl::io::loadPCDFile("test.pcd", *cloud_in) < 0)
{
PCL_ERROR("\a->点云文件不存在!\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud_in->points.size() << " 个数据点" << endl;
//==============================================================================
return 0;
}
1.2 PCDReader
#include
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud); //创建存放读取点云的对象
pcl::PCDReader reader; //定义点云读取对象
if (reader.read("test.pcd", *cloud_in) < 0)
{
PCL_ERROR("\a->点云文件不存在!\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud_in->points.size() << " 个数据点" << endl;
//==============================================================================
return 0;
}
2、将点云保存到硬盘中
2.1 savePCDFile
savePCDFile
有两种常用的存储格式,一种是 savePCDFileASCII
,另一种是 savePCDFileBinary
,两种方式按照需求选择其中一种即可。
#include
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud); //存放读取点云的对象
pcl::PCDReader reader; //定义点云读取对象
if (reader.read("test.pcd", *cloud_in) < 0)
{
PCL_ERROR("\a->点云文件不存在!\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud_in->points.size() << " 个数据点" << endl;
//==============================================================================
//------------------------------- 将点云投影至XOY平面 ----------------------------
pcl::PointCloud::Ptr cloud_out(new pcl::PointCloud); //存放投影后的点云
//定义cloud_out的大小等参数,防止下标越界
cloud_out->width = cloud_in->width;
cloud_out->height = 1;
cloud_out->is_dense = true;
cloud_out->points.resize(cloud_out->width*cloud_out->height);
for (size_t i = 0; i < cloud_in->points.size(); i++)
{
cloud_out->points[i].x = cloud_in->points[i].x;
cloud_out->points[i].y = cloud_in->points[i].y;
cloud_out->points[i].z = 0;
}
//-------------------------------- 将点云保存到硬盘 -----------------------------
///保存为ASICC格式,可以直接用记事本打开
if (!cloud_out->empty())
{
pcl::io::savePCDFileASCII("ASICC_1.pcd", *cloud_out);
cout << "->(ASICC)保存点数为:" << cloud_out->points.size() << endl;
}
else
{
PCL_ERROR("\a->保存点云为空!\n");
system("pause");
return -1;
}
///保存为Binary格式,不可用记事本打开,但更快速。
if (!cloud_out->empty())
{
pcl::io::savePCDFileBinary("Binary_1.pcd", *cloud_out);
cout << "->(Binary)保存点数为:" << cloud_out->points.size() << endl;
}
else
{
PCL_ERROR("\a->保存点云为空!\n");
system("pause");
return -1;
}
//==============================================================================
return 0;
}
2.2 PCDWriter
#include
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud::Ptr cloud_in(new pcl::PointCloud); //存放读取点云的对象
pcl::PCDReader reader; //定义点云读取对象
if (reader.read("test.pcd", *cloud_in) < 0)
{
PCL_ERROR("\a->点云文件不存在!\n");
system("pause");
return -1;
}
cout << "->加载了 " << cloud_in->points.size() << " 个数据点" << endl;
//==============================================================================
//------------------------------- 将点云投影至XOY平面 ----------------------------
pcl::PointCloud::Ptr cloud_out(new pcl::PointCloud); //存放投影后的点云
//定义cloud_out的大小等参数,防止下标越界
cloud_out->width = cloud_in->width;
cloud_out->height = 1;
cloud_out->is_dense = true;
cloud_out->points.resize(cloud_out->width*cloud_out->height);
for (size_t i = 0; i < cloud_in->points.size(); i++)
{
cloud_out->points[i].x = cloud_in->points[i].x;
cloud_out->points[i].y = cloud_in->points[i].y;
cloud_out->points[i].z = 0;
}
//-------------------------------- 将点云保存到硬盘 -----------------------------
pcl::PCDWriter writer;
///保存为ASICC格式,可以直接用记事本打开
if (!cloud_out->empty())
{
writer.write("ASICC_2.pcd", *cloud_out,false); //默认false(可以不写),保存为ASICC格式
cout << "->(ASICC)保存点数为:" << cloud_out->points.size() << endl;
}
else
{
PCL_ERROR("\a->保存点云为空!\n");
system("pause");
return -1;
}
///保存为Binary格式,不可用记事本打开,但更快速。
if (!cloud_out->empty())
{
writer.write("Binary_2.pcd", *cloud_out, true); //true,保存为Binary格式
cout << "->(Binary)保存点数为:" << cloud_out->points.size() << endl;
}
else
{
PCL_ERROR("\a->保存点云为空!\n");
system("pause");
return -1;
}
//==============================================================================
return 0;
}
3、 两种读写点云方式的区别
PCDReader和loadPCDFile的区别
//PCDReader
pcl::PCDReader reader;
reader.read("1.pcd",*cloud);
//loadPCDFile
pcl::io::loadPCDFile("1.pcd",*cloud);
//PCDWriter
pcl::PCDWriter writer;
writer.write("1.pcd",*cloud);
//savePCDFile
pcl::io::savePCDFileBinary("1.pcd",*cloud);
同样的,savePCDFile函数也是调用PCDWriter实现
//savePCDFile
template inline int
savePCDFile (const std::string &file_name, const pcl::PointCloud &cloud, bool binary_mode = false)
{
PCDWriter w;
return (w.write (file_name, cloud, binary_mode));
}
//savePCDFileASCII
template inline int
savePCDFileASCII (const std::string &file_name, const pcl::PointCloud &cloud)
{
PCDWriter w;
return (w.write (file_name, cloud, false));
}
//savePCDFileBinary
template inline int
savePCDFileBinary (const std::string &file_name, const pcl::PointCloud &cloud)
{
PCDWriter w;
return (w.write (file_name, cloud, true));
}