Saturday, March 10, 2012

Quick Start for Kinect: Camera Fundamentals

The previous one is here:http://magic-soap-vision.blogspot.com/2012/03/quick-start-for-kinect-setting-up-your.html




This video covers the basics of reading camera data from the Kinect sensor.  You may find it easier to follow along by downloading the Kinect for Windows SDK Quickstarts samples and slides that have been updated for Beta 2 (Nov, 2011).
  • [00:25] Camera data information
  • [03:30] Creating the UI
  • [04:48] Initializing the Kinect runtime
  • [07:18] Reading values from the RGB camera
  • [11:26] Reading values from the Depth camera
  • [13:06] Adjusting camera tilt 

Initializing the runtime

In the Window_Loaded event, initialize the runtime with the options you want to use. For this example, set RuntimeOptions.UseColor to use the RGB and depth camera:
C#
//Kinect Runtime
Runtime nui;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    SetupKinect();
}
private void SetupKinect()
{
    if (Runtime.Kinects.Count == 0)
    {
        this.Title = "No Kinect connected";
    }
    else
    {
        //use first Kinect
        nui = Runtime.Kinects[0];
 
        //Initialize to return both Color & Depth images
        nui.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseDepth);
    }
}
Visual Basic
'Kinect Runtime
Private nui As Runtime
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    SetupKinect()
End Sub
Private Sub SetupKinect()
    If Runtime.Kinects.Count = 0 Then
        Me.Title = "No Kinect connected"
    Else
        'use first Kinect
        nui = Runtime.Kinects(0)
        'Initialize to return both Color & Depth images
        nui.Initialize(RuntimeOptions.UseColor Or RuntimeOptions.UseDepth)
    End If

Understanding the video frame ready returned values

The video frame returns an ImageFrameReadyEventArgs that contains anImageFrame class. As shown below, the ImageFrame class contains two things:
  • Metadata about the image, such as ImageType to know if it’s a depth or color image, and resolution to know the size of the image
  • Image – the actual image data, which is stored in the Bits byte[] array
image

Converting the byte[] array to an image

To convert the byte[] array that represents the camera image to display it in an Image control (ex: image1 below), call the BitmapSource.Create method as shown below. The last parameter is stride. The stride is the number of bytes from one row of pixels in memory to the next row of pixels in memory. Stride is also called pitch.  For more information, go to http://msdn.microsoft.com/en-us/library/aa473780(VS.85).aspx:
C#
PlanarImage imageData = e.ImageFrame.Image;
image1.Source = BitmapSource.Create(imageData.Width, imageData.Height, 96, 96, 
                PixelFormats.Bgr32, null, imageData.Bits, data.Width * imageData.BytesPerPixel);
Visual Basic
Dim imageData As PlanarImage = e.ImageFrame.Image
image1.Source = BitmapSource.Create(imageData.Width, imageData.Height, 96, 96, _
    PixelFormats.Bgr32, Nothing, imageData.Bits, imageData.Width * imageData.BytesPerPixel)
The Coding4Fun Kinect Toolkit has an extension method built into the ImageFrame class that simplifies creating the bitmap:
C#
image1.Source = e.ImageFrame.ToBitmapSource();
Visual Basic
image1.Source = e.ImageFrame.ToBitmapSource()


No comments:

Post a Comment