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 staff 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).
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.
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:
And here is the corresponding depth image result:
Have a look at the demo project I created. Download it and read the following lines to find out how things work.
Ensure that OpenNI is properly installed in your Windows operating system.
Open Visual Studio and create a new WPF application. I named it "KinectWPF".
Add a reference to OpenNI.net.dll. OpenNI.net is found under C:\Program Files\OpenNI\Bin.
Add an existing item and load SamplesConfig.xml to your project. Remember to copy this file in your output folder (Debug / Release). 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.
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:
and
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:
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:
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 :-).
The window was freezing instantly when moving it on screen, so I updated NuiSensor class and MainWindow.xaml.cs. Adding a background worker solved the problem.
Pingback from DBTech Mini DVR Video Pen – Gold-accented Executive Pen w/Micro SD Slot Expandable to 8gb, Captures High Res Photos and Video | Camcorders Optical Zoom
Pingback from Twitter Trackbacks for Kinect and WPF: Getting the raw and depth image using OpenNI - Vangos Pterneas blog [studentguru.gr] on Topsy.com
Awesome! Thanks for this...
Pingback from Learn Digital Photography – Aperture 3: Portable Genius
Pingback from Kinect and WPF: Getting the raw and depth image using OpenNI | www.nalli.net
Pingback from Welcome To Open Ni and Kinect Hacking :D - alfredoalvarez.com
When i am trying to compile your project it says me that XamlParseExceptions:
Call the constructor for the type of "KinectWPF.MainWindow", satisfying the specified binding constraints, led to an exception. ": the line number"3 "and position in the string"9 ".
@dogged:
You need to add a SamplesConfig.xml file in the Debug/Release folder of your project.
I have downloaded your Project and here are the same error :(
Do the pre-installed OpenNI demos run properly?
Very cool. Thanks!
Yes, all demos works fine except NiViewer and other, which uses RGB camera.
But some demos couldn`t detec gestures.
@dogged
You have probably installed OpenNI incorrectly. Try to replace the XML files under OpenNI\Data with the ones I provided in my previous blog post (www.studentguru.gr/.../how-to-successfully-install-kinect-windows-openni-nite.aspx).
Vangos, thanks.I have a question for U. Is your project for UserTracker in C# + WPF already done?
I'm currently working on the same thing. Are you intending to post code?