Share via


Getting and Displaying Infrared Data in C#

Kinect for Windows 1.6, 1.7, 1.8

Overview

This How To contains user tasks from the InfraredBasics C# sample, which demonstrates how to capture data and save the infrared stream data in a bitmap.

Code It

To get and display color data, you must first initialize the sensor. Here are the tasks for initializing the sensor to generate color data.

  • Enumerate Kinect sensors

  • Enable Data Streaming

    After enumerating the available sensors and finding one that is connected and ready, the next step is to enable the Kinect to stream out data. There are several types of data that can be streamed out: color, depth, skeleton and infrared. This example demonstrates how to enable each of the data streams.

  • Start the Kinect

Once you have initialized the sensor, you are ready to get the next frame of data.

Register an event that fires when data is ready

A Kinect streams data out continuously, one frame at a time, until you tell it to stop streaming. One method your application can use to get each frame of data, is to set the runtime to notify your application each time a frame is ready by raising an event.

  if (this.sensor != null)
  {
    this.sensor.ColorFrameReady += this.SensorColorFrameReady;
  }
      

Do this by adding a ColorFrameReady event to the KinectSensor class. This will cause the Kinect to raise the event each time a new frame of data is ready.

Implement an event handler

In this example, each time the sensor has a new frame of color data available, the KinectSensor object raises a ColorFrameReady event. The event handler implements code to store the color data in temporary storage. Use the ColorImageFrameReady event arguments to pass information about the object that raised the event to the event handler.

  private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
  {
    using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
    {
      if (colorFrame != null)
      {
        colorFrame.CopyPixelDataTo(this.colorPixels);
        ...
      }
    }
  }
      

In the event handler, use the OpenColorImageFrame method to create a ColorImageFrame object to give you access to the frame of color data returned in the event argument. Then use the CopyPixelDataTo method to copy the data to your local memory. The object sender is the KinectSensor that fired the event.

This event handler runs each time a new frame of color data is available from the sensor. Since it runs frequently, this code maximizes performance by doing the minimum processing necessary to get the new data and copy it to local memory. The using statement automatically discards the ColorImageFrame object when you are done using it.

Allocate storage for the data

    private byte[] pixels;

    this.pixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
      

First, declare a member variable to store the pixel data and then allocate the memory array. Each byte will store the data from one pixel. Get the size of the array to allocate from the PixelDataLength property.

For the best performance, allocate the memory for the data outside the event handler, since the event handler runs every frame. For example, allocate the array when you start the application.

Get the data

  using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
  {
    if (colorFrame != null)
    {
      colorFrame.CopyPixelDataTo(this.pixels);

      ...
    }
  }