通過pcl寫入點雲資料
阿新 • • 發佈:2019-02-13
點雲資料寫入PCD檔案
這個教程我們學習怎樣把點雲資料寫入PCD檔案。
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
int
main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
// 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.0 f);
}
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);
}
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
pcl/io/pcd_io.h 是包含了PCD檔案IO操作定義的標頭檔案,PCL的PCD格式檔案的輸入輸出標頭檔案
pcl/point_types.h 是包含了若干PointT資料結構定義的標頭檔案。/PCL對各種格式的點的支援標頭檔案
pcl::PointCloud<pcl::PointXYZ> cloud;
描述我們建立的點雲模板類,模板的型別為 PointXYZ。
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);
}
用隨機數填充點雲資料結構,並設定引數width,height,is_dense。
pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
儲存點雲資料到test_pcd.pcd檔案中。
執行上面的程式,你可能會看到這樣的結果
Saved 5 data points to test_pcd.pcd.
0.352222 -0.151883 -0.106395
-0.397406 -0.473106 0.292602
-0.731898 0.667105 0.441304
-0.734766 0.854581 -0.0361733
-0.4607 -0.277468 -0.916762
開啟生成的pcd檔案,你會看到如下樣式的東西:
$ cat test_pcd.pcd
.PCD v.5 - Point Cloud Data file format
FIELDS x y z
SIZE 4 4 4
TYPE F F F
WIDTH 5
HEIGHT 1
POINTS 5
DATA ascii
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