Getting started with Windows Kinect SDK 1.0
Written by Mike James   
Article Index
Getting started with Windows Kinect SDK 1.0
Hello Kinect
ColorImageFrame
Working in WPF

ColorImageFrame - WPF

The only difference between the Windows Forms version and a WPF version is the way the pixel data is converted to a bitmap that can be displayed.

If you want to do the same job in WPF then simply change the ImageToBitmap function to:

BitmapSource ImageToBitmap(
ColorImageFrame Image)
{
byte[] pixeldata =
new byte[Image.PixelDataLength];
Image.CopyPixelDataTo(pixeldata);
BitmapSource bmap = BitmapSource.Create(
Image.Width,
Image.Height,
96, 96,
PixelFormats.Bgr32,
null,
pixeldata,
Image.Width * Image.BytesPerPixel);
return bmap;
}

I'm not going to explain this in detail because it is a simple application of the BitmapSource class.

See the references at the end of the article for more information on how WPF works with bitmap data.

The event handler needs only minor changes:

void FrameReady(object sender, 
ColorImageFrameReadyEventArgs e)
{
ColorImageFrame imageFrame =
e.OpenColorImageFrame();
BitmapSource bmap =
ImageToBitmap(imageFrame);
image1.Source = bmap;
}

To make the WPF version work, place an Image control on the form If you now run the program - with the Kinect plugged in of course - you will see a video displayed in the Image control.

Finishing Touches

The  Windows Forms and WPF programs described above are the very simplest examples possible. They lack code to handle the proper selection of the Kinect device to use and they lack any error handling - particularly what to do if a Kinect isn't ready or available. We can deal with such details later. However there are two things we really need to deal with at this early stage.

The first is switching off the data streams when the program has finished using them.

 

private void Form1_FormClosing(
object sender, FormClosingEventArgs e)
{
sensor.Stop();
}

The second problem is that, if you stop the application by closing its window, you will see an error reporting  that the ColorImageFrame doesn't exist. The reason is that the OpenColorImageFrame method returns null if the image data has been abandoned for one reason or another. The solution is to add a test for a null ColorImageFrame:

 

void FrameReady(object sender, 
ColorImageFrameReadyEventArgs e)
{
ColorImageFrame imageFrame =
e.OpenColorImageFrame();
if (imageFrame != null)
{
Bitmap bmap = ImageToBitmap(imageFrame);
pictureBox1.Image = bmap;
}
}

Another problem with the example is that the byte array, bitmaps and other objects are created each time the event handler runs. This is OK for a demonstration but for a real application you need to write code that creates everything outside of the render loop i.e. the event handler, to minimize the work it has to do.

Camera elevation

From here you can start to investigate the additional features of the video sensor, add a video capture facility for example. 

You can control the video camera's angle by setting the tilt of the entire Kinect. Add two buttons, label one Up and the other Down and enter:

 

private void button1_Click(
object sender, EventArgs e)
{
sensor.ElevationAngle += 4;
}


private void button2_Click(
object sender, EventArgs e)
{
sensor.ElevationAngle -= 4;
}

Also take care when using the motor driven elevation control - you can burn the motor out if you make it move to often. The code for the buttons given above doesn't check to make sure that you haven't attempted to move it beyond its allowed range - so you can crash the program.

Notice that in general no error handling is included in any of the code so that you can see how it works more easily and in practice you would need to add Try-Catch statements to make sure that the application didn't crash.

 

You can download the code for both the Windows Forms version and the WPF version in a single ZIP file from the CodeBin.

Practical Windows Kinect in C#
Chapter List

  1. Introduction to Kinect
  2. Getting started with Microsoft Kinect SDK 1
  3. Using the Depth Sensor
  4. The Player Index
  5. Depth and Video Space
  6. Skeletons
  7. The Full Skeleton
  8. A 3D Point Cloud

 

 

The original version of of the articles based on the final beta of the Kinect SDK are still available:

Getting started with Microsoft Kinect SDK is the article this chapter is based on. Future chapters will be based on these existing articles:

Depth
Player index

Depth and Video space
Skeletons
The Full Skeleton

    Further reading:

    On Kinect:

    All About Kinect

    Getting Started with PC Kinect using the open source drivers

    Kinect goes self aware - a warning well we think it's funny and something every Kinect programmer should view before proceeding to create something they regret!

    On WPF Bitmap handling:

    BitmapSource: WPF Bitmaps

    BitmapImage

    WriteableBitmap

    Custom BitmapSource

    To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

     

    raspberry pi books

     

    Comments




    or email your comment to: comments@i-programmer.info