How to control the frame interval of Kinect color data stream?

Kinect device has a color camera that delivers the color stream data with the combination of three basic color components red, green and blue (RGB).  This stream data is nothing but a succession of still image frame  and the sensor can deliver them with a range of 12 to 31 frame per second (fps) depends on the resolution of the frames.  You can take control over the frame rate by changing the interval of the color stream data while capturing the data from the sensor.
The KinectSensor class has a property ColorStream of type ColorImageStream, which handles everything that is required for capturing and processing the color image stream.   The ColorImageStream class has another property called CameraSettings, which is of the type ColorCameraSettings class. This class is responsible for applying different effects in the color image data such as controlling brightness, contrast etc. You can change the frame interval by changing the value of ColorCameraSettings.FrameInterval property. The ColorCameraSettings having some additional properties that returns the maximum and minimum frame interval.

Get the maximum frame interval

var maximumFrameInterval = this.sensor.ColorStream.CameraSettings.MaxFrameInterval;

The maximum value of the frame interval is 4000.

Get the Minimum frame interval

var minimumFrameInterval = this.sensor.ColorStream.CameraSettings.MinFrameInterval;

The minimum value of the frame interval is 0.

Get the current frame interval

var currentFrameInterval= this.sensor.ColorStream.CameraSettings.FrameInterval;

You have to make sure the AutoExposure property is set to false while working with FrameInterval; otherwise there will have no effects on the frames.

Frame intervals and frames rates are inversely proportional, which means if you increase the frame interval, you will find the drops in the frames rates.

You can test this by writing one simple WPF application. Use the following XAML code snippet that has one image control to display the color image stream and a slider control that control the frame interval.




Use the following StartColorStream() method to enable the basic settings of the application and start the sensor.

private void StartColorStream()
{
if (this.sensor == null)
{
return;
}
this.sensor.ColorStream.Enable();
this.sensor.ColorFrameReady += sensor_ColorFrameReady;
this.SliderframeInterval.Maximum = this.sensor.ColorStream.CameraSettings.MaxFrameInterval;
this.SliderframeInterval.Minimum = this.sensor.ColorStream.CameraSettings.MinFrameInterval;
this.SliderframeInterval.Value = this.sensor.ColorStream.CameraSettings.FrameInterval;
this.sensor.ColorStream.CameraSettings.AutoExposure = false;
this.sensor.Start();
}

Call the StartColorStream() method from MainWindow()

public MainWindow()
{
InitializeComponent();
if (KinectSensor.KinectSensors.Count > 0)
{
this.sensor = KinectSensor.KinectSensors[0];
StartColorStream();
}
else
{
MessageBox.Show("No Device Connected");
}
}

Add the following code for slider change event,

  Dispatcher.Invoke(DispatcherPriority.Normal,
           new Action(
               delegate()
               {
                   this.sensor.ColorStream.CameraSettings.FrameInterval = e.NewValue;
               }
           ));

Make sure you are handling the sensor_ColorFrameReady event handler properly to process the color image stream.

Check out other Kinect for Windows SDK Tips and Tricks

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.