点云库PCL学习笔记 -- 输入输出IO -- 1.从PCD文件中读取点云数据
PCD文件test_pcd.pcd
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z
SIZE 4 4 4
TYPE F F F
COUNT 1 1 1
WIDTH 5
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 5
DATA ascii
1.28125 577.09375 197.9375
828.125 599.03125 491.375
358.6875 917.4375 842.5625
764.5 178.28125 879.53125
727.53125 525.84375 311.28125
使用pcl_viewer test_pcd.pcd
查看该PCD文件,5个点的图像数据
读取PCD文件代码pcd_read.cpp
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main(int argc,char** argv)
{
//创建了一个名为cloud的指针,储存XYZ类型的点云数据
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
//打开test_pcd.pcd的PCD文件
if(pcl::io::loadPCDFile<pcl::PointXYZ>("test_pcd.pcd",*cloud) == -1)
{
PCL_ERROR("Couldn't read file test_pcd.pcd\n");
return(-1);
}
//PCD文件中: 点云数量 = width * height
std::cout<<"Loaded "
<<cloud->width*cloud->height
<<" data points from test_pcd.pcd with the following fields: "
<<std::endl;
//打印PCD文件中的点云数据
for(size_t i=0;i<cloud->points.size();++i)
std::cout<<" "<<cloud->points[i].x
<<" "<<cloud->points[i].y
<<" "<<cloud->points[i].z
<<std::endl;
return(0);
}
设置编译文件CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(pcd_read)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS}) # 添加PCL头文件
link_directories(${PCL_LIBRARY_DIRS}) # 添加PCL库链接
add_definitions(${PCL_DEFINITIONS})
# 添加一个可执行程序
# 语法:add_executable( 程序名 源代码文件 )
# 添加可执行程序调用hello库中函数
# 将库文件链接到可执行程序上
add_executable(pcd_read pcd_read.cpp)
target_link_libraries(pcd_read ${PCL_LIBRARIES})
编译
mkdir build
cd build/
cmake ..
make
执行程序
cd ..
./build/pcd_read
结果如下