Kinect Development Basics On WPF: Using Color Stream, Depth Stream And Infrared Stream

Kinect is quite a fascinating device which has utilities in many fields. A person playing a game like FIFA using just his body motion or a medical situation can be measured using a Kinect. Though the device  has been on the market for quite a time, but still there are a few applications on the Kinect device. So the motive of these tutorial series is to develop an understanding of Kinect development.

Here, in this blog post, we will be covering some basics of Kinect V2, where our main focus is on three type of streams: Color stream, Depth stream and Infrared stream. There is one other stream: Body Stream, which I will cover in my next post.

COLOR STREAM, DEPTH STREAM and INFRARED STREAM

Our Kinect Sensor is just a device which sends only the raw data to our system, now it is upon a developer to use that raw data in whatever way he feels like. But with Kinect SDK 2.0, we get few additional classes to utilize that data. So, the SDK provides us ColorStream class to access the color data from a Kinect Device.

Similarly, to access Infrared Frames coming from Kinect Device we need InfraredStream class which provides us functionality to access infrared frames and accordingly we can perform an operation.

DepthStream class is one special class which is different from the classes discussed above. Using this class, one can access the data of Depth of a person or object standing in front of the Kinect device. This way we can find to how much distance is a visible object from Kinect sensor.

Here, I am building a WPF application in Visual Studion to perform Kinect functionality. To add Kinect dll file, just follow the steps:

Go to the solution explorer of your WPF project -> right click on references -> Add reference -> Assemblies -> Extensions -> Microsoft Kinect.

Inside the project, open MainWindows.xaml.cs or the .cs file of your page and add the namespace:

  1. using Microsoft.Kinect;      
Now in the body of the .cs file, add following code to initial the variables to use kinect in the application: 
  1. KinectSensor _sensor;    
  2. MultiSourceFrameReader _reader;    

After initializing variables, add the following code into the Windows_Loaded method. Here, we will fetch the default Kinect sensor into _sensor, so that, there is no error. Then, after checking whether sensor is there or not, open the sensor: _sensor.Open(). Then, we will define which Frames will the _reader be accepting. In the code below, we added options to access three frame source types: Color, Depth, Infrared which we can use, 

  1. private void window_Loaded(object sender, RoutedEventArgs e)  
  2.   
  3. {  
  4.   
  5.     _sensor = KinectSensor.GetDefault();  
  6.   
  7.     if (_sensor != null)  
  8.   
  9.     {  
  10.   
  11.         _sensor.Open();  
  12.   
  13.         _reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared);  
  14.   
  15.         _reader.MultiSourceFrameArrived + = Reader_MultiSourceFrameArrived;  
  16.   
  17.     }  
  18.   
  19. }   

Now, after we have added code for initializing sensor, assigning values of FrameSourceTypes to the reader, we will move to the XAML file or the designer file. Here, we have to add some code for Image control within the page so that, we can get the Stream of those three frames on that control. The code will be: 

  1. <Grid>  
  2.   
  3.     <Image Name="displayStream" />  
  4.   
  5.     <Button Content="Color" Name="ColorStreamButton" Click="ColorStreamButton_Click" Margin="100,915,0,0" />  
  6.   
  7.     <Button Content="Infrared" Name="InfraredStreamButton" Click="InfraredStreamButton_Click" Margin="180,915,0,0" />  
  8.   
  9.     <Button Content="Depth" Name="DepthStreamButton" Click="DepthStreamButton_Click" Margin="260,915,0,0" />  
  10.   
  11. </Grid>   

Now in XAML.CS file add the following code:

  1. int _mode = 0;  
  2.   
  3. private void ColorStreamButton_Click(object sender, RoutedEventArgs e)   
  4. {  
  5.     _mode = 0;  
  6. }  
  7.   
  8. private void DepthStreamButton_Click(object sender, RoutedEventArgs e)  
  9. {  
  10.     _mode = 1;  
  11. }  
  12.   
  13. private void InfraredStreamButton_Click(object sender, RoutedEventArgs e)   
  14. {  
  15.     _mode = 2;  
  16. }  
  17.   
  18. void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)  
  19.   
  20. {  
  21.   
  22.     // Get a reference to the multi-frame    
  23.   
  24.     var reference = e.FrameReference.AcquireFrame();  
  25.   
  26.     // Open color frame    
  27.   
  28.     using(var frame = reference.ColorFrameReference.AcquireFrame())   
  29.     {  
  30.   
  31.         if (frame != null)  
  32.         {  
  33.   
  34.             if (_mode == 0)  
  35.             {  
  36.                 displayStream.Source = frame.ToBitmap();  
  37.             }  
  38.   
  39.         }  
  40.   
  41.     }  
  42.   
  43.     // Open depth frame    
  44.   
  45.     using(var frame = reference.DepthFrameReference.AcquireFrame())  
  46.     {  
  47.   
  48.         if (frame != null)  
  49.         {  
  50.   
  51.             if (_mode == 1)  
  52.             {  
  53.                 displayStream.Source = frame.ToBitmap();  
  54.             }  
  55.   
  56.         }  
  57.   
  58.     }  
  59.   
  60.     // Open infrared frame    
  61.   
  62.     using(var frame = reference.InfraredFrameReference.AcquireFrame())  
  63.     {  
  64.   
  65.         if (frame != null)  
  66.         {  
  67.   
  68.             if (_mode == 2)  
  69.             {  
  70.                 displayStream.Source = frame.ToBitmap();  
  71.             }  
  72.   
  73.         }  
  74.   
  75.     }  
  76.   
  77. }    

In the above code, I have used '_mode' to toggle between the states of the video stream and comparing its value in the reader function to toggle in the video stream. Moreover, in this application a file named 'extension.cs' is used which is also available as part of nuget package on nuget library. The file provides functionality to convert Kinect raw data into respective streams.

Now, run the program and test with a Kinect V2. :)

I hope with this blog post, the basics of Kinect development on WPF are covered up. Many more are in the line for Kinect development. Thanks!

Read more articles on WPF: