從PCD檔案中讀取點雲資料(Reading Point Cloud data from PCD files)
阿新 • • 發佈:2018-12-20
在本教程中,我們將學習如何從PCD檔案中讀取點雲資料。
#程式碼
首先,在你最喜歡的編輯器中建立一個名為pcd_read.cpp
的檔案,並在其中放置下面的程式碼:
#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); }
#說明 現在,讓我們逐一分解程式碼。
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
建立一個PointCloud <PointXYZ> boost
共享指標並初始化它。
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); }
從磁碟載入PointCloud
資料(我們假設test_pcd.pcd
已經從前面的教程中建立)到binary blob
中。
或者,您可以閱讀PCLPointCloud2 blob
(僅在PCL 1.x中可用)。由於點雲的動態特性,我們傾向於將它們讀為binary blobs,然後轉換為我們想要使用的實際表示。
pcl::PCLPointCloud2 cloud_blob; pcl::io::loadPCDFile ("test_pcd.pcd", cloud_blob); pcl::fromPCLPointCloud2 (cloud_blob, *cloud); //* convert from pcl/PCLPointCloud2 to pcl::PointCloud<T>
讀取binary blob
並將其轉換為模板化的PointCloud
格式,此處使用pcl::PointXYZ
作為基礎點型別。
最後:
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;
用於顯示從檔案載入的資料。
#編譯和執行程式
將下面的行新增到您的CMakeLists.txt
檔案中:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(pcd_read)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (pcd_read pcd_read.cpp)
target_link_libraries (pcd_read ${PCL_LIBRARIES})
製作好可執行檔案之後,就可以執行它了。簡單地做:
./pcd_read
你會看到類似於:
Loaded 5 data points from test_pcd.pcd with the following fields: x y z
0.35222 -0.15188 -0.1064
-0.39741 -0.47311 0.2926
-0.7319 0.6671 0.4413
-0.73477 0.85458 -0.036173
-0.4607 -0.27747 -0.91676
請注意,如果檔案test_pcd.pcd
不存在(不是已建立或已被刪除),則應該收到錯誤訊息,如:
Couldn't read file test_pcd.pcd