Click here to Skip to main content
15,883,901 members
Articles / Desktop Programming / WPF
Tip/Trick

Rendering Color/Depth Stream using Intel Perceptual Computing in WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Aug 2014CPOL 11.2K   3  
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.

C#
<code>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);
</code>

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).

C#
<code>
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;
}
</code>

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --