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.


4 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.

Leave a Reply

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