2011年8月22日月曜日

Point Cloud Libraryを試す(その3:Kinectからデータ取得)

いよいよKinectを使ってデータを取得してみます。
http://pointclouds.org/documentation/tutorials/openni_grabber.php#openni-grabber

データが取得できれば、これまでやった、ファイルへの保存、ビューワでの表示、がとりあえずできるようになるわけです。
公式のサンプルコードは動かないので↓のように修正しました。

  1. #include <pcl/io/openni_grabber.h>  
  2. #include <pcl/visualization/cloud_viewer.h>  
  3.   
  4.  class SimpleOpenNIViewer  
  5.  {  
  6.    public:  
  7.      SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}  
  8.   
  9.      void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &cloud)  
  10.      {  
  11.        if (!viewer.wasStopped())  
  12.          viewer.showCloud (cloud);  
  13.      }  
  14.   
  15.      void run ()  
  16.      {  
  17.        pcl::Grabber* interface = new pcl::OpenNIGrabber();  
  18.   
  19.        boost::function<void (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&)> f =  
  20.          boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);  
  21.   
  22.        interface->registerCallback (f);  
  23.   
  24.        interface->start ();  
  25.   
  26.        while (!viewer.wasStopped())  
  27.        {  
  28.          sleep (1);  
  29.        }  
  30.   
  31.        interface->stop ();  
  32.      }  
  33.   
  34.      pcl::visualization::CloudViewer viewer;  
  35.  };  
  36.   
  37.  int main ()  
  38.  {  
  39.    SimpleOpenNIViewer v;  
  40.    v.run ();  
  41.    return 0;  
  42.  }  

ビューワで表示されます。




さらに
#include<pcl/io/pcd_io.h>
して、cloud_cb_の中に
pcl::io::savePCDFileBinary("kinect.pcd", *cloud);
を追加すればファイルに保存もできますね。
マイフレーム保存してもけっこう軽いです。

次回は平面抽出をやります。

0 件のコメント:

コメントを投稿