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

Kinect and WPF: Complete body tracking

By , 24 Apr 2011
 

It's time for a really interesting and useful Kinect tutorial. We'll see how to achieve full body tracking using Kinect sensor, OpenNI library and Windows Presentation Foundation.

I found OpenNI C# samples a little messy, so I decided to develop a .NET 4 wrapper library which could be used into WPF applications whithout requiring .NET 2 staff like GDI+, System.Drawing, etc. I named it Nui.Vision and it's part of a larger framework I currently develop. Nui.Vision is a .NET 4 assembly which offers an easy-to-use body tracking API!

Update 20/04/2011

Nui.Vision is now compatible with the latest release of OpenNI framework (1.1.0.41). I have made some changes and bug-fixes to it, including the skeleton-display fix provided by roni26_wu (see comments below). An open-source version of Nui.Vision is coming soon!

Prerequisites

Using the Library

Using Nui.Vision is a piece of cake. All body tracking is done in the background, so you only need to update your user interface when the proper events fire. Firstly, add a reference to OpenNi.net.dll and Nui.Vision.dll. Also import a valid configuration file to your project, as described here. Do not forget to type the corresponding using statement:

using Nui.Vision;

Then declare a new NuiUserTracker object and initialize it in the constructor. Provide the path of the configuration file you previously imported (do not forget to paste the same file in the Debug/Release folders of your application):

_skeleton = new NuiUserTracker("SamplesConfig.xml");

Just below that, you need to define the UserUpdated event.

_skeleton.UsersUpdated += new NuiUserTracker.UsersUpdatedHandler(Skeleton_UserUpdated);

A proper event handler is created. The NuiUserEventArgs parameter provides you with a collection of all the recognized users! You can now get the coordinates (X, Y and Z) of every body part of every user (OpenNI currently supports 15 body parts)!

foreach (var user in e.Users) {
    float headX = user.Head.X;
    float headY = user.Head.Y;
    float headZ = user.Head.Z;
    float neckX = user.Neck.X;
    float neckY = user.Neck.Y;
    // etc... 
}

Quite easy, huh?

Here is a list of all the available body parts:

  • Head
  • Neck
  • LeftShoulder
  • LeftElbow
  • LeftHand
  • RightShoulder
  • RightElbow
  • RightHand
  • Torso
  • LeftKnee
  • LeftHip
  • LeftFoot
  • RightKnee
  • RightHip
  • RightFoot

You may now start developing cool WPF Kinect applications and games. Imagination's the limit.

» Download binaries and source code.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Vangos Pterneas
Software Developer Freelancer
Greece Greece
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.
Follow on   Twitter

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   
Generalmay I ask you a question?memberkouna19-Jun-12 19:23 
Hello,thanks a lot for your contribution of the dll.But when I run the KinectBodyTracking program,VS always says that in the "OpenNI.StatusException" firstly occurs the “OpenNI.Net.dll” type exception.
Could you offer me some hint?
Thanks again.
QuestionThanks so much!memberduong21795-Jan-12 22:32 
I'm doing graduate about Kinect, so I think this article will help me so much! Again, thank you!
Questionimage raw and depth map and labelmap are not syncmembermakoos23-May-11 21:13 
hello vangos
i was working on your body tracking project and i saw that the image raw and depth map and labelmap are not sync , i dont have the source code of your nui.vision library can you help me with syncing these images
i appreciate your help
sincerely
makoos
ewe

Questionpoint cloud generatingmembermakoos22-May-11 23:09 
hello vangos
thanks for this great sample , many of my problems are solved.
i want to generate point cloud of an object that is infront of my kinect, and rotate it until the complete point cloud of my object is generated and store this data to a file for further use, can you help me with it?
1.how to generate point cloud
2. how to continuously add new points to this point cloud while rotating the object
3. store it in a file (pixel position which is point cloud and pixel color wich is raw map of what kinect is seeing)
 
i appreciate any help
sincerely
makoos
ewe

AnswerRe: point cloud generatingmemberVangos Pterneas24-May-11 15:34 
Hi makoos,
 
Unfortunately, I do not know about point cloud generation. You'd better have a look OpenNI documentation and participate in its Google groups (http://groups.google.com/group/openni-dev/topics[^]) in case someone else could help with this issue.
GeneralGreat! but some trouble...memberJonas Toftefors29-Apr-11 3:50 
Great sample!
 
I've got some problems though, the UserFound is trigged rather easily, but I have problem to get the UsersUpdated event to trig?
 
Once it trigged, it works fairly well (except for some errors with -1 data and sometimes a misalignment with a couple of 10 pixels), but sometimes it takes me a minute to get "discovered" by this event?
Any idea on why?
 
Just running the OpenNI UserTracker.net sample finds the outline of me quickly and keeps track of me.
QuestionProblem with latest versionmembercedtat123-Apr-11 11:50 
I have taken your latest version for OpenNI 1.1.0.41, It is compiling correctly but when launching debugger, I get a Null reference exception with stack trace :
 
à Nui.Vision.NuiContext.InitializeSensor(String configuration)
à Nui.Vision.NuiContext..ctor(String configuration)
à Nui.Vision.NuiUserTracker..ctor(String configuration)
à KinectBodyTracking.MainWindow..ctor() dans C:\Projets\Perso\Kinect\KinectBodyTracking\KinectBodyTracking\KinectBodyTracking\MainWindow.xaml.cs:ligne 33
 
Seems like there is a problem with config ?
 
Thank you,
Cedric
AnswerRe: Problem with latest versionmemberVangos Pterneas24-Apr-11 9:49 
You need to add a SamplesConfig.xml file like the one found under C:\Program Files\OpenNI\Data.
 
If the error persists, please let me know (also ensure that the pre-installed OpenNI samples run properly).
GeneralRe: Problem with latest version [modified]membercedtat124-Apr-11 11:28 
Oh I get it, just cleaning my solution solved the problem.
Great job ! This is amazng how those points are stcking to me Smile | :)
 
I don't know how your algorythm is working but it may be great if there was (if possible) some parameters to tweek in a config file. Don't know if it can be a good idea.
 
Just to give you a feedback, I am detected properly at a 30-40% rate. I am not sure how to behave while it is searching for me, sometimes it can take some time and I don't know if there is a particular posture that can help detect the body. Maybe if you can give us some hints on this part.
 
[EDIT] ... ok I know now what is the posture to detect properly the user, 100% detection now and pretty quick when you know how to do ! Really cool ! Maybe you should add a picture of the posture.
 
Nice job,
Cédric

modified on Sunday, April 24, 2011 6:03 PM

GeneralAWESOME! [modified]memberhenrygoro25-Mar-11 3:40 
Excellent job men. works perfectly. Smile | :)
But I have a question: how could I detect when the user is in front of the camera and when the calibration process starts ?
Thanks

modified on Friday, March 25, 2011 12:18 PM

GeneralRe: AWESOME!memberVangos Pterneas24-Apr-11 9:47 
Thank you very much. Just wait a little more for the updated version and I have added the events your asking for Wink | ;-) .

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.130617.1 | Last Updated 24 Apr 2011
Article Copyright 2011 by Vangos Pterneas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid