如何将Microsoft.Kinect.ColorImageFormat转换为System.Windows.Media.PixelFormat?

时间:2013-09-05 10:51:24

标签: c# wpf image-processing kinect pixelformat

我正在进行Kinect编程,当我启用颜色流时,我定义为使用Bayer 30帧/秒格式(ColorImageFormat.RawBayerResolution640x480Fps30)。

渲染图像时,我使用以下代码:

    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;

如何在没有硬编码的情况下根据所选的ColorImageFormat锻炼PixelFormat?

提前致谢!

1 个答案:

答案 0 :(得分:0)

只是让任何人感兴趣,这就是我最终如何做到这一点:

    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;
        }
    }