PCL HelloWorld
PCL 安裝
Windows
下利用 vcpkg
安裝,環境 Visual Studio 14 Win64
,PCL1.9.1
。
同時vcpkg
會自動下載相應的依賴包。
.\vcpkg install pcl
HelloWorld
一個簡單PCL點雲的寫操作。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int main (int argc, char** argv){
// 初始化定義一個點雲對象,PointXYZ
pcl::PointCloud<pcl::PointXYZ> cloud;
// 設置pcd文件字段,將想要填的內容填進去
// Fill in the cloud data
cloud.width = 5;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize (cloud.width * cloud.height);
for (size_t i = 0; i < cloud.points.size (); ++i)
{
cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
}
// 寫入文件
pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
std::cerr << "Saved " << cloud.points.size () << " data points to test_pcd.pcd." << std::endl;
for (size_t i = 0; i < cloud.points.size (); ++i)
std::cerr << " " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
return 0;
}
CMakeLists.txt
#指定cmake最小版本
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
#給自己的程序工程命名
project(pcd_write)
#找PCL庫,指定最小版本
find_package(PCL 1.9 REQUIRED)
#引入相關PCL庫
include_directories(${PCL_INCLUDE_DIRS})
#鏈接PCL庫
link_directories(${PCL_LIBRARY_DIRS})
#加入PCL使用的宏定義
add_definitions(${PCL_DEFINITIONS})
#從單個文件pcd_write.cpp編譯生成名爲pcd_write的可執行文件
add_executable (pcd_write pcd_write.cpp)
#鏈接PCL庫
target_link_libraries (pcd_write ${PCL_LIBRARIES})
編譯
cmake
與 vcpkg
聯合使用構建項目。
mkdir build
cd build
:: 生成
cmake .. -G "Visual Studio 14 Win64" -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake
:: 構建
cmake --build .
.\Debug\pcd_write.exe
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
PCD文件裏有着不同的字段。常用的有以下幾種。
- VERSION - PCD文件的版本號(通常爲0.7);
- FIELDS - 每個點所包含的維度或字段(例如x y z);
- SIZE - 每個數據維度所佔據的字節(比如float爲4字節)
- TYPE -每個數據維度的數據類型(I= signed,U= unsigned,F=float)
- COUNT - 每個數據維度上包含的元素個數。(比如,x, y, or z的count爲1,但是hisogram的count包含N)
- WIDTH - 點雲的寬度
- HEIGHT - 點雲的高度
- VIEWPOINT - 點雲的視角 translation (tx ty tz) +quaternion (qw qx qy qz)
- POINTS -點雲的數量
- DATA -具體的數據存儲格式(ascii or binary)
讀取PCD
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int main (int argc, char** argv)
{
//定義一個句柄用來存放待讀取的點雲
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> ("test_pcd.pcd", *cloud) == -1) //* load the file
{
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return -1;
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
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;
}