Kinect for Windows SDK–Depth stream

 

Depth Stream in WPF Application

Add Image control in MainWindow.xaml in the Window grid

 <Image Name="kinectDepthImage" /> 

 KinectSensor sensor = KinectSensor.KinectSensors[0];
private short[] depthPixelData;
private byte[] depthFrame32;
private WriteableBitmap outputBitmap;

 Add enabling depth stream in MainWindow() constructor of Main.Window.xaml
 sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
sensor.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(sensor_DepthFrameReady);
sensor.Start();

 Add Depth frame ready handler
 void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
{

    using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
    {
        if (depthFrame != null)
        {
            //Using standard SDK this.depthPixelData = new short[depthFrame.PixelDataLength];
            this.depthFrame32 = new byte[depthFrame.Width * depthFrame.Height * 4];
            depthFrame.CopyPixelDataTo(this.depthPixelData);
            byte[] convertedDepthBits = this.ConvertDepthFrame(this.depthPixelData, ((KinectSensor)sender).DepthStream);

            this.outputBitmap = new WriteableBitmap(
                    depthFrame.Width,
                    depthFrame.Height,
                    96,  // DpiX 96,  // DpiY PixelFormats.Bgr32,
                    null);

            this.outputBitmap.WritePixels(
                new Int32Rect(0, 0, depthFrame.Width, depthFrame.Height),
                convertedDepthBits,
                depthFrame.Width * 4,
                0);

            this.kinectDepthImage.Source = this.outputBitmap;

            //Using Coding4Fun Kinect Toolkit ////turn raw data into an array of distances; //var depthArray = depthFrame.ToDepthArray(); ////image //kinectDepthImage.Source = depthFrame.ToBitmapSource(); }
    }

}

// color divisors for tinting depth pixels private static readonly int[] IntensityShiftByPlayerR = { 1, 2, 0, 2, 0, 0, 2, 0 };
private static readonly int[] IntensityShiftByPlayerG = { 1, 2, 2, 0, 2, 0, 0, 1 };
private static readonly int[] IntensityShiftByPlayerB = { 1, 0, 2, 2, 0, 2, 0, 2 };

private const int RedIndex = 2;
private const int GreenIndex = 1;
private const int BlueIndex = 0;

// Converts a 16-bit grayscale depth frame which includes player indexes into a 32-bit frame // that displays different players in different colors private byte[] ConvertDepthFrame(short[] depthFrame, DepthImageStream depthStream)
{
    int tooNearDepth = depthStream.TooNearDepth;
    int tooFarDepth = depthStream.TooFarDepth;
    int unknownDepth = depthStream.UnknownDepth;

    for (int i16 = 0, i32 = 0; i16 < depthFrame.Length && i32 < this.depthFrame32.Length; i16++, i32 += 4)
    {
        int player = depthFrame[i16] & DepthImageFrame.PlayerIndexBitmask;
        int realDepth = depthFrame[i16] >> DepthImageFrame.PlayerIndexBitmaskWidth;

        // transform 13-bit depth information into an 8-bit intensity appropriate // for display (we disregard information in most significant bit) byte intensity = (byte)(~(realDepth >> 4));

        if (player == 0 && realDepth == 0)
        {
            // white this.depthFrame32[i32 + RedIndex] = 255;
            this.depthFrame32[i32 + GreenIndex] = 255;
            this.depthFrame32[i32 + BlueIndex] = 255;
        }
        else if (player == 0 && realDepth == tooFarDepth)
        {
            // dark purple this.depthFrame32[i32 + RedIndex] = 66;
            this.depthFrame32[i32 + GreenIndex] = 0;
            this.depthFrame32[i32 + BlueIndex] = 66;
        }
        else if (player == 0 && realDepth == unknownDepth)
        {
            // dark brown this.depthFrame32[i32 + RedIndex] = 66;
            this.depthFrame32[i32 + GreenIndex] = 66;
            this.depthFrame32[i32 + BlueIndex] = 33;
        }
        else {
            // tint the intensity by dividing by per-player values this.depthFrame32[i32 + RedIndex] = (byte)(intensity >> IntensityShiftByPlayerR[player]);
            this.depthFrame32[i32 + GreenIndex] = (byte)(intensity >> IntensityShiftByPlayerG[player]);
            this.depthFrame32[i32 + BlueIndex] = (byte)(intensity >> IntensityShiftByPlayerB[player]);
        }
    }

    return this.depthFrame32;
}