PCL study notes-data input and output

PCL study notes-data input and output

1. Read and save point cloud files from PCD files

pcl::io::loadPCDFile
#include <iostream> //标准C++库输入输出相关头文件
#include <pcl/io/pcl_io.h> //pcd读写相关头文件
#include <pcl/point_types.h> //pcl中支持的点类型头文件

//点云格式的宏定义,即使用PointT代替pcl::PointXYZ
typedef pcl::PointXYZ PointT;
int main(int argc, char** argv)
{
    
    
	//定义点云指针
	pcl::PointCloud<PointT>::Ptr cloud(new pcl::PointCloud<PointT>);
	//pcl::io::loadPCDFile<PointT>函数导入pcd文件,并将点云存储在cloud中,如果未读取到则为-1
	if(pcl::io::loadPCDFile<PointT>("abc.pcd", *cloud) == -1)
	{
    
    
		PCL_ERROR("couldn't read file\n");//PCL_ERROR是pcl中输出错误信息的函数
		return(-1);
	}
	std::cout<<"点云大小"<<cloud->size()<<std::endl;
	//pcl::io::savePCDFile函数将cloud点云存储在save.pcd文件中
	pcl::io::savePCDFile("save.pcd",*cloud);

	sysrem("pause");
	return(0);
pcl::PCDReader reader
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>

int
main (int argc, char** argv)
{
    
    
  pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2 ());
  pcl::PCLPointCloud2::Ptr cloud_filtered (new pcl::PCLPointCloud2 ());

  // 从文件读取点云图
  // Fill in the cloud data
  pcl::PCDReader reader;
  // Replace the path below with the path where you saved your file
  reader.read ("./data/table_scene_lms400.pcd", *cloud); // Remember to download the file first!

  std::cerr << "PointCloud before filtering: " << cloud->width * cloud->height 
       << " data points (" << pcl::getFieldsList (*cloud) << ").";

  // 创建一个长宽高分别是1cm的体素过滤器,cloud作为输入数据,cloud_filtered作为输出数据
  float leftSize = 0.01f;
  // Create the filtering object
  pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
  sor.setInputCloud (cloud);
  sor.setLeafSize (leftSize, leftSize, leftSize);
  sor.filter (*cloud_filtered);

  std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height 
       << " data points (" << pcl::getFieldsList (*cloud_filtered) << ").";

  // 将结果输出到文件
  pcl::PCDWriter writer;
  writer.write ("./data/table_scene_lms400_downsampled.pcd", *cloud_filtered);

  return (0);
}

2. Read and save PLY files

The suffix is ​​named as .ply format file, commonly used point cloud data file. The ply file can store not only point data, but also grid data. Open a ply file with an editor and observe the header. If the value of element face in the header is 0, it means that the file is a point cloud file. If the value is a positive integer N, it means that the file is a grid file and contains N grids . So use pcl to read ply files, not just use pcl::PointCloud::Ptr cloud (new pcl::PointCloud) To read. When reading a ply file, you must first distinguish whether the file is a point cloud or a grid file. If it is a point cloud file, you can read it according to the general point cloud class. If the ply file is a grid class, you need:
Insert picture description here
#include <pcl/io/ply_io.h> //Introduce the header of ply file reading file

pcl::PolygonMesh mesh; //定义用于存储ply网格文件的变量
pcl::io::loadPLYFile("readName.ply", mesh); //读取ply网格文件
pcl::io::savePLYFile("saveName.ply", mesh); //保存ply网格文件

Show the complete code example of ply grid:

#include <pcl/common/io.h>
#include <pcl/io/io.h>
#include <pcl/point_cloud.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/ply/ply.h>
#include <pcl/console/parse.h>
#include <iostream>
#include <string>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/PolygonMesh.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_lib_io.h>
//#include <pcl/io/vtk_lib_io.hpp>
#include <pcl/io/vtk_io.h>

#include <stdlib.h>
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;


using namespace std;

int
main(int argc, char** argv)
{
//pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	PolygonMesh cloud;
//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	pcl::visualization::PCLVisualizer viewer("PCL PolygonMesh");

	string filename = "Output_.ply";
	pcl::io::loadPolygonFilePLY(filename, cloud);

	viewer.addPolygonMesh(cloud);
	viewer.setBackgroundColor(0.2, 0.0, 0.0);
	while (!viewer.wasStopped())
	{
		viewer.spinOnce();
	}


	return 0;
}

Example of reading PLY point cloud file:

int readcloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, string filename) {
	// PointXYZ表示三维坐标信息 
	// 读取ply文件
	if (pcl::io::loadPLYFile<pcl::PointXYZ>(filename, *cloud) == -1) {
		//PCL_ERROR("Couldnot read file.\n");
		cout << "Couldnot read file." << endl;
		system("pause");
		return -1;
	}
	cout << "已读取并显示"<<filename<<"原始点云数据" << endl;
	cout << filename<<"原始点云数据点数:" << cloud->points.size() << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/109964933