PCL点云文件生成与读取

提要

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

今天要做的是最简单的事情 - 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


你可能感兴趣的:(科研科研)