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

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

By , 8 Feb 2011
 

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 :-).

License

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

About the Author

Vangos Pterneas
Software Developer Freelancer
Greece Greece
Member
I have been a student at Athens University of Economics and Business, Department of Informatics, since September 2007.
 
I mainly develop and design .NET applications in C#, ASP.NET and Silverlight, but I have also worked on J2EE, LAMP and C++.
 
Currently, I am a member of Microsoft Student Partners team and I work as a freelancer for several employers including the Institute for the Management of Information Systems and Vodafone Corporation. As a Student Partner, I have made lots of speaking engagements considering Microsoft technologies (ASP.NET, Silverlight, Windows Phone etc) to undergraduate / postgraduate students and developers.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot workingmemberMember 77080415 Mar '13 - 15:22 
Hi Vangos,
 
I hope my mail finds you well.
I've downloaded everything and your demo but its not working, I don't know why? there is no errors but when I run the project nothing happen or appear. I'm working with Kinect device and tested it with Microsoft Kinect for windows sdk samples and it worked fine, so I'm very confused what's happening so if you could please help me as this is fairly important for me.
 
Many Thanks Vangos for your kind assistance and support
Questionquestion about rawimagesourcememberthenoisemaker22 Mar '12 - 2:03 
Hi, firstly i would say that the toutorial is really interesting.
 
i would ask you a question about the rawimagesource. I'm developping a program in which i would use my asus kinect xtionprolive as a rgbcamera in order to detect a ARmarker.
 
i saw that you implement this code:
 
imgRaw.Source = _sensor.RawImageSource;
 
but, visual studio tells me, obviusly, that imgRaw does not exsist in that context. my question is: what does imgRaw.Source stand for? because i would customize this part of code.
 
best regards,
 
salvo
QuestionDropped frames - depth/image syncmembertwsmith99923 Jun '11 - 9:21 
Are the depth frames and image frames sync'ed to each other by the frame number or the time stamp?
 
TW Smith
GeneralFormat in DepthGenerator.GetDepthMap()membernhanksd8512 Jun '11 - 5:52 
Hi Vangos Pterneas,
I've just bought a kinect sensor and just a beginner. I try to achieve DepthMap by doing like this:
context = new Context(SAMPLE_XML_FILE);

depth = new DepthGenerator(context);
MapData mapData = depth.GetDepthMap();
What does the value mapData[i,j] represent for? How to convert this value to centimet?
Thanks!
Generalerror in source codemembermakoos18 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]memberVangos Pterneas19 May '11 - 4:55 
Hi makoos,
 
I wrote this tutorial considering the first release of OpenNI. If you are currently using the latest version, then you do not need to replace the XML files. Just keep the pre-installed ones!
 
Furthermore, I would suggest you downloading my Nui.Vision library, as it is based on the latest OpenNI release and, apart from raw/depth images, also provides easy body tracking: http://www.codeproject.com/Articles/169161/Kinect-and-WPF-Complete-body-tracking.aspx[^]
 
Hope I helped you.
 
Regards,
Vangos
modified on Thursday, May 19, 2011 11:24 AM

GeneralRe: error in source codememberdekindia16 Jun '11 - 3:30 
I also got the same error. Do we have to refer to something?
 
Error 1 The type or namespace name 'Context' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 135 16 KinectWPF
Error 2 The type or namespace name 'ImageGenerator' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 140 16 KinectWPF
Error 3 The type or namespace name 'DepthGenerator' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 145 16 KinectWPF
Error 4 The type or namespace name 'xn' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 7 7 KinectWPF
Error 5 The type or namespace name 'ImageMetaData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 60 17 KinectWPF
Error 6 The type or namespace name 'ImageMetaData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 60 44 KinectWPF
Error 7 The type or namespace name 'DepthMetaData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 65 17 KinectWPF
Error 8 The type or namespace name 'DepthMetaData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 65 46 KinectWPF
Error 9 'Nui.NuiSensor.Context' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 135 16 KinectWPF
Error 10 'Nui.NuiSensor.ImageGenerator' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 140 16 KinectWPF
Error 11 'Nui.NuiSensor.DepthGenerator' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 145 16 KinectWPF
Error 12 'Nui.NuiSensor.Context' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 189 31 KinectWPF
Error 13 The name 'NodeType' does not exist in the current context C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 196 55 KinectWPF
Error 14 'Nui.NuiSensor.ImageGenerator' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 196 74 KinectWPF
Error 15 The name 'NodeType' does not exist in the current context C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 197 55 KinectWPF
Error 16 'Nui.NuiSensor.DepthGenerator' is a 'property' but is used like a 'type' C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 197 74 KinectWPF
Error 17 The type or namespace name 'MapOutputMode' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 206 13 KinectWPF
Error 18 The type or namespace name 'DepthMetaData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\dekindia\Downloads\KinectWPF\KinectWPF\KinectWPF\NuiSensor.cs 249 44 KinectWPF
GeneralRe: error in source codememberVangos Pterneas16 Jun '11 - 5:41 
You need to install the latest version of OpenNI and change the namespace from "xn" to "OpenNI".
GeneralRe: error in source codemembermcdaniels27 Apr '12 - 14:09 
Hello!
 
I belive tht's not enought. xn and OpenNi namespaces are deferent API's. Same things, but with diferent methods.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 8 Feb 2011
Article Copyright 2011 by Vangos Pterneas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid