This blog post follows the last post on “Installing Point Cloud Library on Ubuntu,” where we installed PCL and all of its required dependencies. In this post, I will go through the steps to bringing up a live demo of PCL via Microsoft Kinect.
To begin, connect the Kinect camera to your computer and make a directory anywhere in your hard drive. Inside that directory, create a text file called CMakeLists.txt. Open it with any editor and paste the following content:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(PCL_openni_viewer) find_package(PCL 1.7 REQUIRED) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) set(PCL_BUILD_TYPE Release) file(GLOB PCL_openni_viewer_SRC "src/*.h" "src/*.cpp" ) add_executable(openniViewer ${PCL_openni_viewer_SRC}) target_link_libraries (openniViewer ${PCL_LIBRARIES})
The first line asks for a CMake version 2.8 installation, minimum. The second line declares a new project named “openni_PCL_viewer”. We tell CMake to check for the presence of PCL library development files, version 1.7. If the system does not meet the CMake and PCL version requirement, the process will fail. Next, we feed the compiler and link the directories where PCL can be found. We tell CMake to use the “Release” build type. Finally, we create a variable, “opennipclviewer_SRC”, that will store the list of files to be compiled. We create a new binary to be compiled from these source files and link it with the PCL library.
Next, create a subdirectory named src and create a new cpp named main.cpp. Copy the following code into main:
#include <pcl/io/openni_grabber.h> #include <pcl/visualization/cloud_viewer.h> class SimpleOpenNIViewer { public: SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {} void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud) { if (!viewer.wasStopped() && !viewer.updatePointCloud (cloud, "cloud")) viewer.addPointCloud (cloud, "cloud"); // viewer.showCloud (cloud); viewer.spinOnce(); } void run () { pcl::Grabber* interface = new pcl::OpenNIGrabber(); boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1); interface->registerCallback (f); interface->start (); while (!viewer.wasStopped()) { boost::this_thread::sleep (boost::posix_time::seconds (1)); } interface->stop (); } pcl::visualization::PCLVisualizer viewer; }; int main () { SimpleOpenNIViewer v; v.run (); return 0; }
Save and close.
To compile your code, create a new build/ subdirectory next to the src/ one. Open a terminal there and issue the following commands:
cmake -DCMAKE_BUILD_TYPE=Release .. make
Then run the compiled example program:
./openniViewer
After some seconds, the main window will appear and the application will start grabbing frames from the device. However, you will notice that the image is upside down. You can fix this using the mouse; drag with the left key to change the angle and the right one (or the mouse wheel) to zoom. To rotate an image, hold the Ctrl key while dragging the image with mouse. Another useful key is R, which will reset the camera parameters when pressed. Use it when you zoom too much and notice that you cannot see the cloud.
Helpful Resources:
Here are some additional guides on how to run PCL:
http://pointclouds.org/documentation/tutorials/using_pcl_pcl_config.php: This is the official PCL tutorial on how to run programs using PCL.
http://pointclouds.org/documentation/tutorials/openni_grabber.php: Here is a PCL demo on how to use the OpenNI grabber in PCL projects.
http://yuechuan.me/fetch-pcd-point-cloud-data-from-kinect-by-using-pcl-and-openni/: This blog post has great details on how to run PCL as well as how to install PCL without source and how to integrate it into Eclipse projects.