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

Getting most of the Kinect SDK in C# - Part 1 of ?: Initialization

, 6 Jan 2012
Rate this:
Please Sign up or sign in to vote.
A series about experimenting with Kinect for Windows SDK.

Introduction

This is part one of a series documenting my experiments with Kinect for Windows SDK. After the first two or three articles, this series should make quite a good walkthrough for beginners and a nice reference for more advanced developers.

Series table of contents:

  1. Initialization
  2. ImageStreams
  3. Coming soon...

Background

Kinect is a sensor originally developed for Microsoft's XBOX 360 console. It has a regular video camera, IR camera for depth detection, and an array of microphones. Here you can find all the information about Kinect and its SDK for Windows.

SDK basics

To use the SDK in your application, you need to reference the Microsoft.Research.Kinect assembly. It contains two namespaces:

  • Microsoft.Research.Kinect.Nui - Used for visual features
  • Microsoft.Research.Kinect.Audio - Used for audio features

At this point, I will focus on the NUI part of the SDK and its features:

  • DepthStream - Interface to depth sensor
  • VideoStream - Interface to video camera
  • SkeletonEngine - Engine for tracing up to two human bodies
  • NuiCamera - Device info and control (mainly for getting/setting elevation angle)

To start coding, you need to use the Runtime class from the SDK. This class provides an entry point for device enumeration and initialization. In most cases, all you need is to get an instance of the Runtime class:

Runtime rt = Runtime.Kinects[0];

This will give you control over the first Kinect found in the system. If you have more Kinects connected, you will want to enumerate the devices using the Runtime.Kinects collection:

foreach (Runtime CurrentRuntime in Runtime.Kinects)
{
    // Do your stuff...
}

Runtime initialization

No matter which way you get reference to the Runtime, you must Initialize() it. Failing to remember to do this will cause exceptions while accessing ImageStreams or SkeletonEngine.

Before calling Initialize(), you should check the status of the runtime and device by examining the Status property. The documentation describes statuses as:

  • Connected - The sensor is fully connected and ready.
  • Error - An error has occurred.
  • Disconnected - The sensor is not connected to the USB connector.
  • NotPowered - The sensor is not powered up.
  • NotReady - Some part of the sensor is not yet ready.

You should only proceed if Status is KinectStatus.Connected. The status can be monitored by attaching to the Runtime.Kinects.StatusChanged event.

Upon initialization, several options are passed to Runtime. According to the documentation, their meaning is quite straightforward:

  • UseColor - Process color information.
  • UseDepth - Process depth information.
  • UseDepthAndPlayerIndex - Process the player index.
  • UseSkeletalTracking - Process skeletal tracking data.

Not enabling some of the features probably will result in performance changes. I will try to verify it later.

The next thing you should do is to Open() the ImageStreams. Again, failing to remember about this will result in exceptions when accessing frames. Providing wrong parameters will result in an InvalidOperationException.

CurrentRuntime.Initialize(RuntimeOptions.UseDepthAndPlayerIndex
                          | RuntimeOptions.UseSkeletalTracking
                          | RuntimeOptions.UseColor
                          );

CurrentRuntime.VideoStream.Open(ImageStreamType.Video, 2, 
               ImageResolution.Resolution1280x1024, ImageType.Color);
CurrentRuntime.DepthStream.Open(ImageStreamType.Depth, 2, 
               ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);

InfoTool

Pieces of code shown above are taken from my first working application - InfoTool. At the moment, all this tool does is:

  • Enumerates through devices
  • Initializes all devices
  • Opens ImageStreams
  • Displays all information collected during steps above

Here is a sample output:

InfoTool.PNG

This sample output shows that there is one Kinect available for usage and all initializations were executed successfully. As you can see, VideoStream and DepthStream have some improper parameters at startup and are changed to the proper ones by calling Open().

Points of interest

My next experiments will include:

  • Determining the best way of working with ImageStreams
  • Skeletal tracking
  • Benchmarking / Performance tuning

History

  • 2011-11-20: Initial submission.
  • 2012-01-06: Added series TOC and updated source code (common for whole series).

License

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

Share

About the Author

Jarek Kruza
Flextronics
Poland Poland
Programming since 10 years old with first commercial app sold in age of 16.
 
In past got Bachelor's degree in Computer Sciences and worked as Linux administrator and software developer.
 
Currently slightly over 30 and working as IT Project Manager for Flextronics.
 
Still coding for fun and/or money.

Comments and Discussions

 
QuestionI have downloaded a code for Kinect and face\ng the following problems. PinmemberPrafulla Ashok Natu12-Mar-12 1:29 
AnswerRe: I have downloaded a code for Kinect and face\ng the following problems. PinmemberJarek Kruza12-Mar-12 1:36 
Hi!
 
This is because Microsoft has released final version of SDK and article still bases on Beta version.
 
I'm planning to update the articles next weekend.
 
Please look at this post for information about migrating from Beta SDK:
http://robrelyea.wordpress.com/2012/02/01/k4w-code-migration-from-beta2-to-v1-0-managed/[^]

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
Web02 | 2.8.140827.1 | Last Updated 6 Jan 2012
Article Copyright 2011 by Jarek Kruza
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid