How to convert Microsoft.Kinect.ColorImageFormat to System.Windows.Media.PixelFormat?

800 views Asked by At

I am doing Kinect programming and when I enable the color stream, I defined to use Bayer 30 frames per second format (ColorImageFormat.RawBayerResolution640x480Fps30).

When rendering the image, I use the following code:

    PixelFormat format = PixelFormats.Bgr32;

    // Bitmap source to write to.
    WriteableBitmap src = new WriteableBitmap((int)_kinectSensor.ColorStream.FrameWidth, (int)_kinectSensor.ColorStream.FrameHeight, 96, 96, format, null);

    // Stride for image.
    var stride = (int)_kinectSensor.ColorStream.FrameWidth * format.BitsPerPixel / 8;

    // Write pixels to bitmap source.
    src.WritePixels(new Int32Rect(0, 0, (int)imgPic.Width, (int)imgPic.Height), rawBayerPixels, stride, 0);

    // Set XAML image source = Bitmap source.
    imgPic.Source = src;

How do I workout the PixelFormat without hardcoding as above, based on the selected ColorImageFormat?

Thanks in advance!

1

There are 1 answers

0
Rob On BEST ANSWER

Just incase anyone's interested, here's how I ended up doing it:

    private PixelFormat GetFormat(ColorImageFormat colFormat)
    {
        // Work out the pixel format depending on the resolution.
        switch (colFormat)
        {
            case ColorImageFormat.InfraredResolution640x480Fps30: return PixelFormats.Gray16;
            case ColorImageFormat.RawYuvResolution640x480Fps15: return PixelFormats.Gray16;
            default: return PixelFormats.Bgr32;
        }
    }