Two ways to read point cloud
PCL provides two ways to read and write pcd point clouds. The file format corresponding to PCD (Point Cloud Date, point cloud data) is (*.pcd), which is the official designated format of PCL and has two data storage types , ASCII and Binary .
The point cloud in ASCII format can be viewed directly with Notepad;
Point clouds in Binary format cannot be viewed with Notepad, but it is faster.
Mode 1 PCDReader; PCDWriter
Method 2 loadPCDFile; savePCDFile
1. Read the point cloud from the hard disk
1.1 loadPCDFile
#include <pcl/io/pcd_io.h>
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //创建存放读取点云的对象
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 <pcl/io/pcd_io.h>
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //创建存放读取点云的对象
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. Save the point cloud to the hard disk
2.1 savePCDFile
savePCDFile
There are two commonly used storage formats, one is savePCDFileASCII
and the other is savePCDFileBinary
, and you can choose one of the two methods according to your needs.
#include <pcl/io/pcd_io.h>
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //存放读取点云的对象
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<pcl::PointXYZ>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZ>); //存放投影后的点云
//定义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 <pcl/io/pcd_io.h>
using namespace std;
int main()
{
//-------------------------------- 从硬盘中读取点云 -----------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in(new pcl::PointCloud<pcl::PointXYZ>); //存放读取点云的对象
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<pcl::PointXYZ>::Ptr cloud_out(new pcl::PointCloud<pcl::PointXYZ>); //存放投影后的点云
//定义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. The difference between the two methods of reading and writing point clouds
The difference between PCDReader and loadPCDFile
//PCDReader
pcl::PCDReader reader;
reader.read("1.pcd",*cloud);
//loadPCDFile
pcl::io::loadPCDFile("1.pcd",*cloud);
- The difference between PCDWriter and savePCDFile
//PCDWriter
pcl::PCDWriter writer;
writer.write("1.pcd",*cloud);
//savePCDFile
pcl::io::savePCDFileBinary("1.pcd",*cloud);
Similarly, the savePCDFile function is also implemented by calling PCDWriter
//savePCDFile
template<typename PointT> inline int
savePCDFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud, bool binary_mode = false)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, binary_mode));
}
//savePCDFileASCII
template<typename PointT> inline int
savePCDFileASCII (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, false));
}
//savePCDFileBinary
template<typename PointT> inline int
savePCDFileBinary (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
{
PCDWriter w;
return (w.write<PointT> (file_name, cloud, true));
}