[#KINECT] HowTo: Remove the background while preserving a body with the new #KinectSdkEnter a post title



in yesterday post I commented that one of the news in the Kinect SDK 1.8 is the ability to remove the background of an image of the kinect sensor preserving the person’s body (green screen).

To see how this new feature works, see example “Background Removal Basis-WPF” included in the Kinect SDK Developer Toolkit.


Once downloaded the source code for this example, we see that it uses 2 dlls external., compiled for x 86 and x 64. These dlls are created with C++ (also you can access the source code of them) and have a wrapper. net, which we see as a reference: Microsoft.Kinect.Toolkit.BackgroundRemoval


We see a bit the code needed to run this app. The first thing we must do is to declare a variable of typeBackgroundRemovedColorStream .

   1: /// <summary>
   2: /// Our core library which does background 
   3: /// </summary>
   4: private BackgroundRemovedColorStream backgroundRemovedColorStream;

Then, when it detects a Kinect sensor, it is time to change this variable. In this case, taking into account the values that has been initialized the camera and depth:

   1: this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor);
   2: this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat);

Note:This parameter is a bit redundant, the sensor itself already has this information. Suggestion up for Kinect equipment.

We endorse us “change” event

   1: // Add an event handler to be called when the background removed color frame is ready, so that we can
   2: // composite the image and output to the app
   3: this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler;

And ready. Now just need to revise the code of this handler for the event. The 1, that we see is that we fear an object of type BackgroundRemovedStream which can access (line 3). We can then work with the image that we want to use background with a couple of lines add the bodys detected by the sensor in front of the same Open-mouthed smile

   1: private void BackgroundRemovedFrameReadyHandler(object sender, BackgroundRemovedColorFrameReadyEventArgs e)
   2: {
   3:     using (var backgroundRemovedFrame = e.OpenBackgroundRemovedColorFrame())
   4:     {
   5:         if (backgroundRemovedFrame != null)
   6:         {
   7:             if (null == this.foregroundBitmap || this.foregroundBitmap.PixelWidth != backgroundRemovedFrame.Width 
   8:                 || this.foregroundBitmap.PixelHeight != backgroundRemovedFrame.Height)
   9:             {
  10:                 this.foregroundBitmap = new WriteableBitmap(backgroundRemovedFrame.Width, backgroundRemovedFrame.Height, 96.0, 96.0, PixelFormats.Bgra32, null);
  12:                 // Set the image we display to point to the bitmap where we'll put the image data
  13:                 this.MaskedColor.Source = this.foregroundBitmap;
  14:             }
  16:             // Write the pixel data into our bitmap
  17:             this.foregroundBitmap.WritePixels(
  18:                 new Int32Rect(0, 0, this.foregroundBitmap.PixelWidth, this.foregroundBitmap.PixelHeight),
  19:                 backgroundRemovedFrame.GetRawPixelData(),
  20:                 this.foregroundBitmap.PixelWidth * sizeof(int),
  21:                 0);
  22:         }
  23:     }
  24: }

Greetings @ La Finca

El Bruno



1 comment

Leave a comment

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 )

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.

%d bloggers like this: