您好,登錄后才能下訂單哦!
PCL中創造了一種用于描述空間點集的文件 - PCD.關于PCD的簡介,可以參考這里 - http://pointclouds.org/documentation/tutorials/pcd_file_format.php
今天要做的是最簡單的事情 - PCD文件的生產與讀取。
環境:Win7 64bit VS2010 PCL1.7
PCL編譯參考這里 - Window7下手動編譯最新版的PCL庫
官網推薦的是用cmake來管理工程,在windows中,我們可以通過Cmakegui來生成VS2010的工程,然后導入。這樣就免去了在VS中各種添加lib,頭文件之苦了 ^^
首先創建主程序 pcd_write.cpp ,隨便找個記事本寫一下就可以了,代碼如下:
#include <iostream> #include <pcl/io/io.h> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/visualization/cloud_viewer.h> using namespace std; 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.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; getchar(); return (0); }
cmake_minimum_required(VERSION 2.6 FATAL_ERROR) project(MY_GRAND_PROJECT) find_package(PCL 1.3 REQUIRED COMPONENTS common io) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable(pcd_write_test pcd_write.cpp) target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})
如果報錯了,基本是你環境沒配置好,檢查一下該裝的東西是否都安裝好了,環境變量是否設置好。
成功時候在build文件夾下就生成了對應的vs2010工程了,直接雙擊打開 MY_GRAND_PROJECT.sln
導入后在cloud_view上 右擊->Set as StartUp Project,如下圖:
直接運行,效果如下:
工程目錄下就生成了一個 test_pcd.pcd 點云文件。
套路和上面的一樣,貼一下代碼就好。
cloud_viewer.cpp
#include <pcl/visualization/cloud_viewer.h> #include <iostream> #include <pcl/io/io.h> #include <pcl/io/pcd_io.h> int user_data; void viewerOneOff (pcl::visualization::PCLVisualizer& viewer) { viewer.setBackgroundColor (0.0, 0.0, 0.0); pcl::PointXYZ o; o.x = 1.0; o.y = 0; o.z = 0; viewer.addSphere (o, 0.25, "sphere", 0); std::cout << "i only run once" << std::endl; } void viewerPsycho (pcl::visualization::PCLVisualizer& viewer) { static unsigned count = 0; std::stringstream ss; ss << "Once per viewer loop: " << count++; viewer.removeShape ("text", 0); viewer.addText (ss.str(), 200, 300, "text", 0); //FIXME: possible race condition here: user_data++; } int main () { pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile ("bunny.pcd", *cloud); pcl::visualization::CloudViewer viewer("Cloud Viewer"); //blocks until the cloud is actually rendered viewer.showCloud(cloud); //use the following functions to get access to the underlying more advanced/powerful //PCLVisualizer //This will only get called once viewer.runOnVisualizationThreadOnce (viewerOneOff); //This will get called once per visualization iteration viewer.runOnVisualizationThread (viewerPsycho); while (!viewer.wasStopped ()) { //you can also do cool processing here //FIXME: Note that this is running in a separate thread from viewerPsycho //and you should guard against race conditions yourself... user_data++; } return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(cloud_viewer) find_package(PCL 1.2 REQUIRED) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable (cloud_viewer cloud_viewer.cpp) target_link_libraries (cloud_viewer ${PCL_LIBRARIES})
由于之前生成的pcd里面僅有一些離散的點,看的不是很明顯,加載一個Stanford bunny 的點云(PCL目錄下有)來看看。
Using PCL in your own project - http://pointclouds.org/documentation/tutorials/using_pcl_pcl_config.php#using-pcl-pcl-config
讀取 pcd 檔並顯示三維影像 - http://coldnew.github.io/blog/2013/04/12_64cf9.html
The CloudViewer - http://pointclouds.org/documentation/tutorials/cloud_viewer.php#cloud-viewer
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。