Drawing Point Cloud retrieve from Kinect v2 using Point Cloud Library – part.2 with Grabber

What is Grabber?

Point Cloud Library (PCL) have some modules for input data that called “Grabber”.

  • OpenNI2Grabber
  • This module is grabber for input data from PrimeSensor based on OpenNI2.

  • HDLGrabber/VLPGrabber
  • This module is grabber for input data from Velodyne LiDAR based on Boost.Asio and PCAP.

  • RealSenseGrabber
  • This module is grabber for input data from Intel RealSense cameras based on RealSense SDK and librealsense.

PCL with Kinect v2

PCL doesn’t have grabber for input data from Kinect v2.
So, I will introduce two ways to input data from Kinect v2 based on Kinect SDK v2.

  • part.1 without Grabber
  • This method doesn’t use Grabber.
    It will manually inputting data to PCL from Kinect v2.

  • part.2 with Grabber (this article)
  • This method use Grabber.
    It will use Kinect2Grabber.


These Grabbers is implemented as class that inherits from pcl::Grabber.
You can implement original Grabber that are not included in PCL by implementing class that inherits from pcl::Grabber.

Kinect2Grabber is grabber to input data from Kinect v2 based on Kinect SDK v2.
This Grabber only depend on Kinect SDK v2. You don’t need to have other libraries to use Kinect2Grabber.*1
In addition, This Grabber has been implemented in only one header file (kinect2_grabber.h). You can use Kinect2Grabber by just include header file.

*1 You need to setting project for Kinect SDK v2 because this Grabber depend on Kinect SDK v2. The sample program uses FindKinectSDK2.cmake.

This Grabber is published in following.

Drawing Point Cloud retrieved from Kinect v2 using Point Cloud Library with Grabber

This sample program is published in following.


You need to include kinect2_grabber.h in order to use pcl::Kinect2Grabber.


#include "kinect2_grabber.h"


You will create instance of pcl::Kinect2Grabber.


// Kinect2Grabber
boost::shared_ptr<pcl::Grabber> grabber = boost::make_shared<pcl::Kinect2Grabber>();

Register Callback

You will define callback function to retrieve point cloud using pcl::Kinect2Grabber.
This callback function is called each time to retrieve point cloud of one frame.
You will write point cloud processing in here, and returns shared pointer of point cloud to outside.

Then, You will register callback function to pcl::Kinect2Grabber.
You will give function pointer to pcl::Kinect2Grabber::registerCallback().


// Retrieved Point Cloud Callback Function
pcl::PointCloud<PointType>::ConstPtr cloud;
boost::mutex mutex;
boost::function<void( const pcl::PointCloud<PointType>::ConstPtr& )> function =
    [&cloud, &mutex]( const pcl::PointCloud<PointType>::ConstPtr& ptr ){
        boost::mutex::scoped_lock lock( mutex );

        /* Point Cloud Processing */

        cloud = ptr->makeShared();


// Register Callback Function
boost::signals2::connection connection = grabber->registerCallback( function );


You will call pcl::Kinect2Grabber::start() to start retrieve point cloud.
As well, You will call pcl::Kinect2Grabber::stop() to stop retrieve point cloud.


// Start Grabber

while( !viewer->wasStopped() ){
    // Update Viewer

    boost::mutex::scoped_try_lock lock( mutex );
    if( lock.owns_lock() && cloud ){
        // Update Point Cloud
        if( !viewer->updatePointCloud( cloud, "cloud" ) ){
            viewer->addPointCloud( cloud, "cloud" );

// Stop Grabber

Execution Result

It will be real-time display Point Cloud that retrieved from Kinect v2 when running this sample program.


14 thoughts on “Drawing Point Cloud retrieve from Kinect v2 using Point Cloud Library – part.2 with Grabber”

  1. Hello, I wonder how to connect two kinect on one PC, is there any sample to retrieve point cloud by connecting two kinect on one PC?
    Thank you very much!

    1. Hi Max,

      The multiple Kinect v2 sensors can’t be direct connected to one PC.
      Please consider that retrieve point cloud data from Kinect v2 sensor that connected to each client PCs, and send to server PC.
      (But, I don’t have that sample.)

  2. Hi Tsukasa Sugiura,

    Great tutorial !!! Im just wondering what would be the code that can save .pcd files using a key stroke. Please can you help me with this?

    Thank you in advanced.


    1. Hi Namal Bandara,

      For example, It is able to implement as follows.
      Please include pcl/io/pcd_io.h for I/O of PCD format.

      #include <iostream>
      #include <sstream>
      #include <iomanip>
      #include <pcl/visualization/pcl_visualizer.h>
      #include <pcl/io/pcd_io.h>

      Then, define callback function to handle keyboard events.
      Write point cloud data to PCD format using pcl::savePCDFile() in callback function.
      And, register callback function to pcl::visualization::PCLVisualizer::registerKeyboardCallback().

      // Keyboard Callback Function
      boost::function<void( const pcl::visualization::KeyboardEvent& )> keyboard =
          [&cloud, &mutex]( const pcl::visualization::KeyboardEvent& event ){
              if( event.getKeySym() == "s" && event.keyDown() ){
                  boost::mutex::scoped_lock lock( mutex );
                  // Generate Indices ( 000, 001, 002, ... )
                  static uint32_t index = 0;
                  std::ostringstream oss;
                  oss << std::setfill( '0' ) << std::setw( 3 ) << index++;
                  std::cout << oss.str() + ".pcd" << std::endl;
                  // Save Point Cloud to PCD File
                  pcl::io::savePCDFile( oss.str() + ".pcd", *cloud );
      // Register Keyboard Callback Function
      viewer->registerKeyboardCallback( keyboard );

      If you want more information, Please post to PCL users forum.

  3. Hi Namal Bandara,

    thanks od this articles.

    Do you prefer using Kinect v2 and PCL with grabber or without grabber?

    Also, can I use all advantages of Kinect v2 with official MS drivers for PCL?

    Thank you!

    1. Hi Eugen C,

      I prefer that use PCL with Grabber.
      If you want to switch to other sensor, It can easily to do it.

      Kinect2Grabber implements the basic features for retrieving point cloud data from Kinect v2.
      If you want to use other features, You need implement it yourself.

  4. Thank you for your help.

    I’ll try some things on my own before I go to collage for help.

    For example:

    I run sample program you include in Kinect2Grabber, bit I get an error:

    I uploaded image on imgur.com


    1. Hi Eugen C,

      It works successfully in my environment (PCL 1.8.1, Kinect SDK 2.0, Visual Studio 2017, CMake 3.10.1).
      Sorry, I don’t know the cause of your error with that image alone.

    2. Now I successful running this viewer, also I succeed to run algorithms which you can find on PCL website with Cmake, PCL 1.8.1 and VS 2015.

      Problem was in VS 2013 probably, but now everything works great.

      Next step is to make .PCD file from Camera, I managed to make my own PCD file with my own points for testing some libraries, but I still don’t know how to get points directly from camera.

      I really appreciate your help.

  5. I tried your cord to Namal Bandara (2017/10/12 at 02:47)
    I succeed building the proejct. But I want to know about Point Cloud Data’s x,y,z data.So I save Point Cloud Data as a csv file

    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    using namespace std;
    int main(int argc, char** argv)
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
        if (pcl::io::loadPCDFile<pcl::PointXYZ>("000tt.pcd", *cloud) == -1) // load the file
            PCL_ERROR("Couldn't read file test_pcd.pcd \n");
            return (-1);
        cout << "Loaded "
             << cloud->width * cloud->height
             << " data points from test_pcd.pcd with the following fields: "
             << endl;
        ofstream myfile;
        for (int i = 0; i < 512; i++)
            for (int j = 0; j < 424; j++)
                myfile << cloud->points[i*j + j].x << " " << cloud->points[i*j + j].y << " " << cloud->points[i*j + j].z << ",";
            myfile << "\n";
        cout << "저장 끝";
        return (0);

    I want to know how pcd file is organized

    1. Hi We,

      Do you want to write point cloud to csv file?
      For example, It can be written as follows.

      #include <iostream>
      #include <fstream>
      #include <pcl/io/pcd_io.h>
      #include <pcl/point_types.h>
      typedef pcl::PointXYZ PointType;
      int main( int argc, char* argv[] )
          // Load PCD File
          pcl::PointCloud<PointType>::Ptr cloud( new pcl::PointCloud<PointType> );
          if( pcl::io::loadPCDFile<PointType>( "file.pcd", *cloud ) == -1 ){
              return -1;
          // Create Output File Stream
          std::ofstream ofs( "file.csv" );
          if( !ofs.is_open() ){
              return -1;
          // Write Point Cloud to CSV
          if( cloud->isOrganized() ){
              std::cout << "organized point cloud" << std::endl;
              for( int y = 0; y < cloud->height; y++ ){
                  for( int x = 0; x < cloud->width; x++ ){
                      const PointType& point = cloud->at( x, y );
                      ofs << point.x << "," << point.y << "," << point.z << ",";
                  ofs << "\n";
              std::cout << "unorganized point cloud" << std::endl;
              for( pcl::PointCloud<PointType>::iterator it = cloud->begin(); it != cloud->end(); it++ ){
                  ofs << it->x << "," << it->y << "," << it->z << "\n";
          return 0;
  6. Hi, i am trying to work PCL with Xtion2. But there is some problem with it. There is some errors when i use openni2grabber.
    Do i need to make file like kinect2grabber? or is there any way to do it?

    thanks. beom

    1. Hi beom,

      OpenNI2 that included in PCL All-in-one Installer have not Xtion2 driver (,because it is original installer created by OpenNI.org).
      Please use OpenNI2 that published from ASUS. It is contain Xtion2 driver.

  7. Thank you for making this tutorial.
    I could compile the project but when I ran an exception in the method registerCallback from pcl Grabber class is thrown saying that “no callback for type:void __cdecl(class boost::shared_ptr<class pcl::PointCloud const > const & __ptr64)”.
    I am using PCL 1.8.1, VS community 17, CMAKE 3.11.18040201-MSVC_2 and Kinect SDK v2.0_1409.
    Do I need to install additional software or set more environment variables?

Leave a Reply

Your email address will not be published. Required fields are marked *