65.9K
CodeProject is changing. Read more.
Home

Rendering Color/Depth Stream using Intel Perceptual Computing in WPF

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Aug 8, 2014

CPOL
viewsIcon

11502

Rendering PXCMImage to Image control in WPF

Introduction

When I started using the Intel perceptual computing SDK for C# I could not find any examples on  rendering the Color/Depth streams in WPF. The examples provided with the SDK are implemented in Winforms. The problem was the image obtained from Color/Depth stream is of type PXCMImage . So we could not directly render it to the Image control. After playing with the SDK and referring examples in web I was able to render the Color/Depth stream . Here is what I have done.

Using the code

We have to extract the Bitmap out of the PXCMImage type image.This can be done by two methods.


PXCMImage pxcRGBImage = base.QueryImage(PXCMImage.ImageType.IMAGE_TYPE_COLOR);
PXCMImage pxcDepthImage = base.QueryImage(PXCMImage.ImageType.IMAGE_TYPE_DEPTH);

//Convert PXCMI image to bitmap
Bitmap rgbBitmap;
Bitmap depthBitmap;

// Method 1:

pxcRGBImage.QueryBitmap(session, out rgbBitmap);
pxcDepthImage.QueryBitmap(session, out depthBitmap);

But the QueryBitmap is deprecated so it is better to use method 2.

// Method 2:

PXCMImage.ImageData depthImageData;
PXCMImage.ImageData colorImageData;
pxcDepthImage.AcquireAccess(PXCMImage.Access.ACCESS_READ_WRITE, out depthImageData);
pxcRGBImage.AcquireAccess(PXCMImage.Access.ACCESS_READ_WRITE, out colorImageData);

depthBitmap = depthImageData.ToBitmap(pxcDepthImage.info.width,pxcDepthImage.info.height);
rgbBitmap = colorImageData.ToBitmap(pxcRGBImage.info.width, pxcRGBImage.info.height);

pxcRGBImage.ReleaseAccess(ref colorImageData);
pxcDepthImage.ReleaseAccess(ref depthImageData);


Points of Interest

But even after obtaining the Bitmap of Color/Depth stream we can not directly render it to the Image control. We have to convert Bitmap to BitmapImage.

In this blog it is neatly explained how to bind a Bitmap to Image control in WPF. Or you can convert the Bitmap to BitmapImage using this function (Reference: http://stackoverflow.com/a/1069509/2256349).


public BitmapImage ToBitmapImage(Bitmap bitmap)
{
  BitmapImage bitmapImage = null;
  using (MemoryStream memory = new MemoryStream())
  {
    bitmap.Save(memory, ImageFormat.Png);
    memory.Position = 0;
    bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory;
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
  }
  return bitmapImage;
}

History