Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Kinect and WPF: Getting the raw and depth image using OpenNI

, 3 Dec 2013
Rate this:
Please Sign up or sign in to vote.
Kinect and WPF: Getting the raw and depth image using OpenNI.

It's time for the first Kinect tutorial! In the upcoming blog posts, we'll see how to use the managed API provided by OpenNI and NITE in order to build our own Natural User Interface applications. OpenNI and NITE are two great libraries, offered by PrimeSense, which let us access lots of cool stuff such as body tracking, gesture recognition and much more. Both of them provide .NET wrappers utilized directly from C# applications! Learn how to install these libraries reading my previous blog post.

OpenNI comes with some interesting demos (SimpleRead.net, SimpleUser.net and UserTracker.net specifically) built using the managed OpenNI.net.dll library. Unfortunately, these demos run on .NET 2.0 in order to be fully compatible with Mono platform. So, I decided to create new samples (or modify some of the existing ones) enabling them to run on .NET 4.0 and Windows Presentation Foundation (WPF).

Kinect OpenNI UserTracker.net

The Power of WPF

WPF offers great advantages over WinForms considering user experience. Furthermore, WPF's System.Windows.Media is way more powerful than WinForms' System.Drawing. WPF uses WriteableBitmap and ImageSource instead of Bitmap and BitmapData. As a result, I had to rewrite much of the initial code to make it WPF-compliant.

Requirements

Accessing Kinect's Raw and Depth Image

Kinect device comes with two cameras: a raw and a depth one (640x480 resolution each). Different color in a raw image means different RGB value in the real scene. Different color in a depth image means different distance in the real scene. OpenNI lets us access both camera sources. Here is the raw image result:

Kinect OpenNI raw image

And here is the corresponding depth image result:

Kinect OpenNI depth image

Wrapping Them All Together

Have a look at the demo project I created. Download it and read the following lines to find out how things work.

Step 0

Ensure that OpenNI is properly installed in your Windows operating system.

Step 1

Open Visual Studio and create a new WPF application. I named it "KinectWPF".

Step 2

Add a reference to OpenNI.net.dll. OpenNI.net is found under C:\Program Files\OpenNI\Bin.

Step 3

Add an existing item and load SamplesConfig.xml to your project. SamplesConfig.xml is found under C:\Program Files\OpenNI\Data and provides all the necessary information about the sensor (available cameras, resolution, PrimeSense key). You need to have the default XML file replaced with something like the one I provided in my "how-to" post.

Step 4

Download my NuiSensor class and add it to your project. NuiSensor class uses OpenNI.net.dll internally in order to acquire the camera images. You need the following properties:

public ImageSource RawImageSource

and:

public ImageSource DepthImageSource

Step 5

Navigate to MainWindow.xaml and add two Image controls. In the code-behind file, firstly create a new instance of NuiSensor providing the SamplesConfig.xml path:

NuiSensor _sensor = new NuiSensor("SamplesConfig.xml");

Then, add an event handler for CompositionTarget.Rendering event. CompositionTarget.Rendering is raised when the frame needs to be redrawn (that means 60 times per second). You simply have to call the proper NuiSensor properties and you are done:

imgRaw.Source = _sensor.RawImageSource;
imgDepth.Source = _sensor.DepthImageSource;

Download demo. As you can see, I have also added a "toggle image visibility" button in order to reduce the Window size.

Wish you happy Kinect programming Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vangos Pterneas
Product Manager LightBuzz
United Kingdom United Kingdom
Vangos Pterneas is a Microsoft Most Valuable Professional in the Kinect technology. He helps companies from all over the world grow their revenue by creating profitable software products. Vangos is the owner of LightBuzz Software agency and author of two technical books.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Generalerror in source code Pinmembermakoos18-May-11 21:19 
hi, thanx for this nice programming , i got a problem while i was using your sourcecode
i installed these files:
OpenNI-Win32-1.1.0.41-Dev.msi
NITE-Win32-1.3.1.5-Dev.msi
SensorKinect-Win-OpenSource32-5.0.1.msi
 
note : im using visual studio 2010
 
and replaced original xml files with the ones your recomended, but i dont khow why these errors are occuring:
error in this line : using xn; (it cant find this header)
 

Error 1 'OpenNI.DepthGenerator' does not contain a definition for 'GetMapOutputMode' and no extension method 'GetMapOutputMode' accepting a first argument of type 'OpenNI.DepthGenerator' could be found (are you missing a using directive or an assembly reference?) I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 210 53 KinectPaint
 
Error 2 'OpenNI.MapOutputMode.nXRes' is inaccessible due to its protection level I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 212 34 KinectPaint
 
Error 3 'OpenNI.MapOutputMode.nYRes' is inaccessible due to its protection level I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 213 35 KinectPaint
 
Error 8 Argument 1 should not be passed with the 'ref' keyword I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 290 47 KinectPaint
 
Error 7 The best overloaded method match for 'OpenNI.HandsGenerator.StartTracking(OpenNI.Point3D)' has some invalid arguments I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 290 13 KinectPaint
 
Error 4 The type name 'GestureRecognizedHandler' does not exist in the type 'OpenNI.GestureGenerator' I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 223 73 KinectPaint
 
Error 6 The type name 'HandDestroyHandler' does not exist in the type 'OpenNI.HandsGenerator' I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 227 63 KinectPaint
 
Error 5 The type name 'HandUpdateHandler' does not exist in the type 'OpenNI.HandsGenerator' I:\Components\VisualStudio\Kinect\kinect_soft\KinectPaint\KinectPaint\KinectPaint\NuiHandTracker.cs 226 62 KinectPaint
 
i appreciate your help
ewe

GeneralRe: error in source code [modified] PinmemberVangos Pterneas19-May-11 4:55 
GeneralRe: error in source code Pinmemberdekindia16-Jun-11 3:30 
GeneralRe: error in source code PinmemberVangos Pterneas16-Jun-11 5:41 
GeneralRe: error in source code Pinmembermcdaniels27-Apr-12 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 3 Dec 2013
Article Copyright 2011 by Vangos Pterneas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid