Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / WPF

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
3 Dec 2013CPOL3 min read 70.7K   10   17
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:

C#
public ImageSource RawImageSource

and:

C#
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:

C#
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:

C#
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)



Comments and Discussions

 
Questionπροβλημα με το OpenNi Pin
Member 1098311031-Jul-14 0:49
Member 1098311031-Jul-14 0:49 
AnswerRe: προβλημα με το OpenNi Pin
Vangos Pterneas31-Jul-14 1:48
professionalVangos Pterneas31-Jul-14 1:48 
GeneralRe: προβλημα με το OpenNi Pin
Member 1098311031-Jul-14 2:25
Member 1098311031-Jul-14 2:25 
GeneralRe: προβλημα με το OpenNi Pin
Vangos Pterneas31-Jul-14 2:46
professionalVangos Pterneas31-Jul-14 2:46 
Questionaligning depth with rgb Pin
sorosh jahandideh21-Jan-14 4:52
sorosh jahandideh21-Jan-14 4:52 
AnswerRe: aligning depth with rgb Pin
Vangos Pterneas23-Jan-14 9:07
professionalVangos Pterneas23-Jan-14 9:07 
Questiondepth raw data Pin
sorosh jahandideh29-Dec-13 19:14
sorosh jahandideh29-Dec-13 19:14 
AnswerRe: depth raw data Pin
Vangos Pterneas30-Dec-13 4:32
professionalVangos Pterneas30-Dec-13 4:32 
QuestionNot working Pin
Member 77080415-Mar-13 15:22
Member 77080415-Mar-13 15:22 
Questionquestion about rawimagesource Pin
thenoisemaker22-Mar-12 2:03
thenoisemaker22-Mar-12 2:03 
QuestionDropped frames - depth/image sync Pin
twsmith99923-Jun-11 9:21
twsmith99923-Jun-11 9:21 
GeneralFormat in DepthGenerator.GetDepthMap() Pin
nhanksd8512-Jun-11 5:52
nhanksd8512-Jun-11 5:52 
Generalerror in source code Pin
xocode18-May-11 21:19
xocode18-May-11 21:19 
GeneralRe: error in source code [modified] Pin
Vangos Pterneas19-May-11 4:55
professionalVangos Pterneas19-May-11 4:55 
GeneralRe: error in source code Pin
dekindia16-Jun-11 3:30
dekindia16-Jun-11 3:30 
GeneralRe: error in source code Pin
Vangos Pterneas16-Jun-11 5:41
professionalVangos Pterneas16-Jun-11 5:41 
GeneralRe: error in source code Pin
mcdaniels27-Apr-12 14:09
mcdaniels27-Apr-12 14:09 

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

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