Sunday, 29 December 2013

OpenCVKinect: Acquiring Kinect Data Streams in OpenCV

Click here to go to code download step directly

Edit (26/05/2016) : I have updated the OpenCVKinect to fix some bugs and make use of different visualization for depth maps. Details of this newer version can be seen here. All other details mentioned in this blog post still apply to the updated version.

Holiday season is here, and I have finally found sometime to write something for this blog. Sometime back I wrote a guide for compiling OpenCV with OpenNI support. Since that post a lot has been changed in the newer OpenNI 2.x SDK, there has been a lot of improvements for using Microsoft Kinect Sensor. One of the major change is the Object Oriented Implementation as opposed to pure C implementation in previous OpenNI 1.x versions. I have had many requests asking me to write a tutorial for compiling OpenCV with the new OpenNI 2.x support. Although this is not possible with the current SDK and OpenCV, this blog post introduces an implementation for acquiring Kinect Data Streams in OpenCV.

After a long time, I recently started working with Kinect Sensor again, and therefore I wanted to access the data streams in a more efficient manner. While it looked straightforward to use the built in function calls in OpenNI 2.x, I was more comfortable with using OpenCV format instead. Therefore I wanted to hide all that detail of OpenNI Objects and their methods in a simple and convenient way. To achieve this, I wrote my own implementation and this blog post officially releases this code as open source, for anyone to use/modify/update to. Right now I have some basic methods, but in future I am thinking of adding simple image processing/data recording methods too (to make it easier for beginners).

The code is dependent on both OpenCV and OpenNI binaries and unlike my previous post, this one does not needs compiling OpenCV with OpenNI support. Every SDK used here is used as it is, without any special compiling parameters. The only additional information is that you will be required to include library paths and link precompiled OpenCV and OpenNI binaries to your project. For completeness everything is explained below in steps, if you are aware any/most of these steps then you can skip to this step to get the code and start developing.

Setting up Libraries

The code for OpenCVKinect was written in Windows environment, therefore this post will explain everything related to using it in Windows set up. Nevertheless it can be easily used on any other platform.

To set up everything, download and install Kinect SDK v1.8 followed by OpenNI SDK v2.2 (I have tested with x86 but might also work with x64). As opposed to version 1.x of OpenNI, version 2.x does not conflict with Kinect SDK, infact some driver functionality from Kinect SDK is used in OpenNI 2.x. This is the reason for first installing Kinect SDK and then OpenNI SDK.

If you already have OpenCV compiled and installed for Microsoft Visual C++, then skip to the next part of this post. Otherwise, download the latest OpenCV precompiled binaries for Microsoft Visual C++ and follow the instructions in my previous post to set up the environment variables.

Using the Existing Code

Get/fork the code from github repository for this implementation at:

This repo contains an MS VC++ project with everything set up, and a working example showing the usage of the OpenCVKinect class. The project settings point to the location of OpenCV and OpenNI libraries on my hard drive, which you might have to change to match the location of these libraries on your local PC.

Creating a New Project

If existing project from Github repo does not work out for you, then you can create an Empty Console Project in MS VC++. Apply the project settings for OpenCV and OpenNI. Once the project is set-up and linked to all the libraries, add the source code from:
  • Code (there are three files, two for the OpenCVKinect class and one main source)
If everything is set up fine then you will be able to access and display the depth and color data streams from Kinect sensor.

Thats it for now. I hope this code helps anyone facing the problem of quickly accessing the data streams from Kinect. I will be adding more details and possibly some description/explanation of code here soon. Until then Good night and Happy Holidays!!