[#KINECTSDK] HowTo: Use Kinect as a green screen (II)

image

Buenas,

This is the 2nd part of the post where I show you how to use the House of Kinect as a green screen to plant our body image… then wherever we want.

In the previous post was an important part of analyzing that it was the process of each frame of the skeleton, camera and depth sensor.

   1: void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e)

   2: {

   3:     if (_sensor == null) return;

   4:     var hasDepth = false;

   5:     var hasColor = false;

   6:     hasDepth = GetDepthFrame(e);

   7:     hasColor = GetColorFrame(e);

   8:     ProcessDepthFrame(hasDepth);

   9:     ProcessColorFrame(hasColor);

  10: }

As we shall see in the following code, the GetDepthFrame and GetColorFrame functions validate that we own camera and depth sensor, correct information before giving the process as valid.

Then the function ProcessDepthFrame undertakes a mapping of the points in the array of the camera and the array of the depth sensor that gives as a result an array where at each point know, information from the camera, away from the same sensor, if it relates to a player and also we link also it with a joint in the skeleton.

With this information, in the 2nd function ProcessColorFrame already can paint on bitmap for the background image, only the body of each player and leave the other bits in transparent mode.

   1: private void ProcessColorFrame(bool hasColor)

   2: {

   3:     if (!hasColor) return;

   4:     // Write the pixel data into our bitmap

   5:     _colorBitmap.WritePixels(new Int32Rect(0, 0, _colorBitmap.PixelWidth, _colorBitmap.PixelHeight), _colorPixels,

   6:                              _colorBitmap.PixelWidth*sizeof (int), 0);

   7:  

   8:     if (_playerOpacityMaskImage == null)

   9:     {

  10:         _playerOpacityMaskImage = new WriteableBitmap(_depthWidth, _depthHeight, 96, 96, PixelFormats.Bgra32, null);

  11:         imgMask.OpacityMask = new ImageBrush

  12:                                   {

  13:                                       ImageSource = _playerOpacityMaskImage

  14:                                   };

  15:     }

  16:  

  17:     _playerOpacityMaskImage.WritePixels(new Int32Rect(0, 0, _depthWidth, _depthHeight), _greenScreenPixelData,

  18:                                         _depthWidth*((_playerOpacityMaskImage.Format.BitsPerPixel + 7)/8), 0);

  19: }

  20:  

  21: private void ProcessDepthFrame(bool hasDepth)

  22: {

  23:     if (hasDepth)

  24:     {

  25:         _sensor.MapDepthFrameToColorFrame(DepthImageFormat.Resolution320x240Fps30, _depthPixels,

  26:                                           ColorImageFormat.RgbResolution640x480Fps30, _colorCoordinates);

  27:  

  28:         Array.Clear(_greenScreenPixelData, 0, _greenScreenPixelData.Length);

  29:  

  30:         for (var y = 0; y < _depthHeight; ++y)

  31:         {

  32:             for (var x = 0; x < _depthWidth; ++x)

  33:             {

  34:                 var depthIndex = x + (y*_depthWidth);

  35:                 var depthPixel = _depthPixels[depthIndex];

  36:                 var player = depthPixel & DepthImageFrame.PlayerIndexBitmask;

  37:                 if (player <= 0) continue;

  38:  

  39:                 var colorImagePoint = _colorCoordinates[depthIndex];

  40:                 var colorInDepthX = colorImagePoint.X/_colorToDepthDivisor;

  41:                 var colorInDepthY = colorImagePoint.Y/_colorToDepthDivisor;

  42:                 if (colorInDepthX <= 0 || colorInDepthX >= _depthWidth || colorInDepthY < 0 ||

  43:                     colorInDepthY >= _depthHeight) continue;

  44:                 var greenScreenIndex = colorInDepthX + (colorInDepthY*_depthWidth);

  45:                 _greenScreenPixelData[greenScreenIndex] = OpaquePixelValue;

  46:                 _greenScreenPixelData[greenScreenIndex - 1] = OpaquePixelValue;

  47:             }

  48:         }

  49:     }

  50: }

  51:  

  52: private bool GetColorFrame(AllFramesReadyEventArgs e)

  53: {

  54:     bool hasColor;

  55:     using (var colorFrame = e.OpenColorImageFrame())

  56:     {

  57:         if (colorFrame != null)

  58:         {

  59:             colorFrame.CopyPixelDataTo(_colorPixels);

  60:             hasColor = true;

  61:         }

  62:     }

  63:     return hasColor;

  64: }

  65:  

  66: private bool GetDepthFrame(AllFramesReadyEventArgs e)

  67: {

  68:     bool hasDepth;

  69:     using (var depthFrame = e.OpenDepthImageFrame())

  70:     {

  71:         if (depthFrame != null)

  72:         {

  73:             depthFrame.CopyPixelDataTo(_depthPixels);

  74:             hasDepth = true;

  75:         }

  76:     }

  77:     return hasDepth;

  78: }

Now if they have simplified you Risa

 

Saludos @ Home

El Bruno

image image image
About these ads

2 pensamientos en “[#KINECTSDK] HowTo: Use Kinect as a green screen (II)

Deja un comentario

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s