none
ColorFrame and DepthFrame toBitmap functions RRS feed

  • Question

  • I want to transform two functions that I got for kinect v1  toBitmap functions for colorFrame and DepthFrame to kinect v2 correspondant functions. The function that I have are the following:

     private Bitmap ColorImageFrameToBitmap(ColorFrame colorFrame)
            {
    
                int width = colorFrame.FrameDescription.Width;
                int height = colorFrame.FrameDescription.Height;
    
                byte[] pixelBuffer = new byte[width*height*colorFrame.FrameDescription.BytesPerPixel];
                colorFrame.CopyPixelDataTo(pixelBuffer);
                Bitmap bitmapFrame = ArrayToBitmap(pixelBuffer, colorFrame.FrameDescription.Width,  colorFrame.FrameDescription.Height, PixelFormat.Format32bppRgb);
                return bitmapFrame;
            }
    
    
            //wird aktuell nicht gebraucht
            private Bitmap DepthImageFrameToBitmap(DepthFrame depthFrame)
            {
                DepthImagePixel[] depthPixels = new DepthImagePixel[depthFrame.PixelDataLength];
                byte[] colorPixels = new byte[depthFrame.PixelDataLength * 4];
                depthFrame.CopyDepthImagePixelDataTo(depthPixels);
    
                // Get the min and max reliable depth for the current frame
                int minDepth = depthFrame.DepthMinReliableDistance;
                int maxDepth = depthFrame.DepthMaxReliableDistance;
    
                // Convert the depth to RGB
                int colorPixelIndex = 0;
                for (int i = 0; i < depthPixels.Length; ++i)
                {
                    // Get the depth for this pixel
                    short depth = depthPixels[i].Depth;
    
                    // To convert to a byte, we're discarding the most-significant
                    // rather than least-significant bits.
                    // We're preserving detail, although the intensity will "wrap."
                    // Values outside the reliable depth range are mapped to 0 (black).
    
                    // NOTE: Using conditionals in this loop could degrade performance.
                    // Consider using a lookup table instead when writing production code.
                    // See the KinectDepthViewer class used by the KinectExplorer sample
                    // for a lookup table example.
                    byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
    
                    // Write out blue byte
                    colorPixels[colorPixelIndex++] = intensity;
    
                    // Write out green byte
                    colorPixels[colorPixelIndex++] = intensity;
    
                    // Write out red byte                        
                    colorPixels[colorPixelIndex++] = intensity;
    
                    // We're outputting BGR, the last byte in the 32 bits is unused so skip it
                    // If we were outputting BGRA, we would write alpha here.
                    ++colorPixelIndex;
                }
                Bitmap bitmapFrame = ArrayToBitmap(colorPixels, depthFrame.FrameDescription.Width, depthFrame.FrameDescription.Height, PixelFormat.Format32bppRgb);
                return bitmapFrame;
            }
    

    ArrayToBitMap fnction are the following:

    Bitmap ArrayToBitmap(byte[] array, int width, int height, PixelFormat pixelFormat)
            {
                Bitmap bitmapFrame = new Bitmap(width, height, pixelFormat);
    
                BitmapData bitmapData = bitmapFrame.LockBits(new Rectangle(0, 0,
                width, height), ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);
    
                IntPtr intPointer = bitmapData.Scan0;
                Marshal.Copy(array, 0, intPointer, array.Length);
    
                bitmapFrame.UnlockBits(bitmapData);
                return bitmapFrame;
    }

    Any idea how can I do so? 

    Friday, May 29, 2015 9:34 AM

Answers

  • Getting data from frames has changed between V1 and V2:

    // Color:
    kinectSensor.ColorFrameSource.CreateFrameDescription(
        ColorImageFormat.Bgra);
    
    byte[] pixelBuffer = new byte[
        colorFrame.FrameDescription.Width * 
        colorFrame.FrameDescription.Height * 
        colorFrame.FrameDescription.BytesPerPixel];
    
    colorFrame.CopyConvertedFrameDataToArray(
        pixelBuffer, ColorImageFormat.Rgba);
    
    // Depth:
    ushort[] depthData = new ushort[
        depthFrame.FrameDescription.Width *
        depthFrame.FrameDescription.Height];
    
    depthFrame.CopyFrameDataToArray(depthData);
    
    // Now you can convert the depthData array to an RGB 
    // array, or you can render with the PixelFormat Gray8.
    Look at the ColorBasics and DepthBasics samples in the SDK browser for additional code.


    Friday, May 29, 2015 6:33 PM
    Moderator