[#KINECTONE] HowTo: Capture and display the feed from the camera’s sensor with #KinectSdk v2

Hello!

This week I will write a couple of posts about the Kinect SDK v2, and with these I’ll get the bases for complex examples. Today I’ll show how to use the new SDK to capture and display the feed from the camera’s sensor.

The first thing we see is that we need to work with an element of type ColorFrameDescription (line 14), in this element will be stored the camera-related information. Then we open the reader’s color camera (line 16) and also initialize a Bitmap (line 18) which have a binding to an Image of WPF.

   1: public MainWindow()

   2: {

   3:     _kinectSensor = KinectSensor.Default;

   4:     if (_kinectSensor != null)

   5:     {

   6:         _kinectSensor.Open();

   7:  

   8:         // get the coordinate mapper

   9:         _coordinateMapper = _kinectSensor.CoordinateMapper;

  10:         _bodies = new Body[_kinectSensor.BodyFrameSource.BodyCount];

  11:         _bodyReader = _kinectSensor.BodyFrameSource.OpenReader();

  12:  

  13:         // open the color reader for the color frames

  14:         var colorFrameDescription = _kinectSensor.ColorFrameSource.FrameDescription;

  15:  

  16:         _colorReader = _kinectSensor.ColorFrameSource.OpenReader();

  17:         _pixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * _bytesPerPixel];

  18:         _colorBitmap  = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);

  19:  

  20:         StatusText = "start";

  21:     }

  22:     else

  23:     {

  24:         // on failure, set the status text

  25:         StatusText = "something goes wrong";

  26:     }

  27:     Loaded += MainWindow_Loaded;

  28:     Closing += MainWindow_Closing;

  29:     DataContext = this;

  30:     InitializeComponent();

  31: }

In the Load of the View we endorse the Reader update event and in the same add the next code:

   1: void ColorReader_FrameArrived(object sender, ColorFrameArrivedEventArgs e)

   2: {

   3:     var frameReference = e.FrameReference;

   4:     try

   5:     {

   6:         var frame = frameReference.AcquireFrame();

   7:         if (frame == null) return;

   8:         using (frame)

   9:         {

  10:             var frameDescription = frame.FrameDescription; 

  11:             if ((frameDescription.Width == _colorBitmap.PixelWidth) && (frameDescription.Height == _colorBitmap.PixelHeight))

  12:             {

  13:                 if (frame.RawColorImageFormat == ColorImageFormat.Bgra)

  14:                 {

  15:                     frame.CopyRawFrameDataToArray(_pixels);

  16:                 }

  17:                 else

  18:                 {

  19:                     frame.CopyConvertedFrameDataToArray(_pixels, ColorImageFormat.Bgra);

  20:                 }

  21:  

  22:                 _colorBitmap.WritePixels(

  23:                     new Int32Rect(0, 0, frameDescription.Width, frameDescription.Height),

  24:                     _pixels,

  25:                     frameDescription.Width * _bytesPerPixel,

  26:                     0);

  27:             }

  28:         }

  29:     }

  30:     catch (Exception)

  31:     {

  32:         // ignore if the frame is no longer available

  33:     }

  34: }

The syntax is quite clear, the steps we follow at this point

  • We request the frame and validate that the same non-null
  • Verify that the size of the frame matches the bitmap we created in the previous step
  • Copy the array of the image bitmap to Bitmap associated with an IMAGE in WPF
  • Donate!

Compared with version 1.X of the KinectSdk is quite different. I am not in a position to comment on which version is best, although I think the V2 is simpler when working with various aspects of the sensor at the same time, for example with body count and camera.

And now holiday’s time!

Disclaimer:

“This is preliminary software and/or hardware and APIs are preliminary and subject to change

Saludos @ Home in Cordoba

El Bruno

image image image Google

One thought on “[#KINECTONE] HowTo: Capture and display the feed from the camera’s sensor with #KinectSdk v2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.