In 2D, use PCL to obtain the two-dimensional coordinates of the point cloud in all pcd files in the folder

The overall implementation idea is: first get all the file names under the folder, and then traverse and open the file to get the coordinates from it with PCL

The header files required by the program:

#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>
#include <cstring>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <ros/ros.h>
// function:获取路径下所有文件名,存在filenames中
void getFiles(string path, vector<string>& filenames)
{
    
    
	DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str()))){
    
    
        cout<<"Folder doesn't Exist!"<<endl;
        return;
    }
    while((ptr = readdir(pDir))!=0) {
    
    
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
    
    
            filenames.push_back(path + "/" + ptr->d_name);
    	}
    }
    closedir(pDir);
}
// function:读取整个文件夹下pcd文件,通过PCL将其中的坐标存储在position_x和position_y中
void Get_position_data(float *position_x, float *position_y, int &position_num) {
    
    
    string filePath = "/home/user/pcd";   // 待读取文件夹路径
    vector<string> files;
    //获取该路径下的所有文件
    getFiles(filePath, files);
    for (int i=0; i<(int)files.size(); i++) {
    
    
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);

	    if(pcl::io::loadPCDFile<pcl::PointXYZ>(files[i], *cloud)==-1) {
    
      //*打开点云文件 
		    PCL_ERROR("Couldn't read file test_pcd.pcd\n");
		    return ;
	    }
        for(size_t t=0; t<cloud->points.size(); ++t) {
    
    
            position_x[position_num] = cloud->points[t].x;
            position_y[position_num] = cloud->points[t].y;
            position_num++;
        }
    } 
    return ;
}

How to use: just call Get_position_data()

Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/130791598