PCL点云文件生成与读取-创新互联

提要

   PCL中创造了一种用于描述空间点集的文件  -  PCD.关于PCD的简介,可以参考这里 - http://pointclouds.org/documentation/tutorials/pcd_file_format.php

成都创新互联是一家专注于成都网站建设、成都做网站与策划设计,黑山网站建设哪家好?成都创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:黑山等地区。黑山做网站价格咨询:13518219792

今天要做的是最简单的事情 - PCD文件的生产与读取。

环境:Win7 64bit VS2010 PCL1.7

PCL编译参考这里 - Window7下手动编译最新版的PCL库

PCD文件生成

   官网推荐的是用cmake来管理工程,在windows中,我们可以通过Cmakegui来生成VS2010的工程,然后导入。这样就免去了在VS中各种添加lib,头文件之苦了 ^^

   首先创建主程序 pcd_write.cpp ,随便找个记事本写一下就可以了,代码如下:

#include  #include  #include  #include  #include  using namespace std;   int main (int argc, char** argv) {   pcl::PointCloud 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); }

再创建CMakeLists.txt

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文件夹,接下来打开CMake-gui,将CMakeList.txt托进去,Configure->Generate.如果环境是配置好的话,结果就像这样:


如果报错了,基本是你环境没配置好,检查一下该装的东西是否都安装好了,环境变量是否设置好。

成功时候在build文件夹下就生成了对应的vs2010工程了,直接双击打开 MY_GRAND_PROJECT.sln


导入后在cloud_view上 右击->Set as StartUp Project,如下图


直接运行,效果如下:


工程目录下就生成了一个 test_pcd.pcd点云文件。

点云文件的读取

套路和上面的一样,贴一下代码就好。

cloud_viewer.cpp

#include  #include  #include  #include   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::Ptr cloud (new pcl::PointCloud);     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文件拷贝过来,运行的结果如下:


由于之前生成的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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:PCL点云文件生成与读取-创新互联
路径分享:http://ybzwz.com/article/jpcij.html