【问题标题】:Why isn't Color Image Displaying?为什么不显示彩色图像?
【发布时间】:2012-07-26 16:56:56
【问题描述】:

我正在使用 Kinect SDK 并尝试添加色框。我正在使用代码:

byte[] pixels = new byte[sensor.ColorStream.FramePixelDataLength];

WriteableBitmap image = new WriteableBitmap(sensor.ColorStream.FrameWidth, sensor.ColorStream.FrameHeight, 96, 96,
        PixelFormats.Bgra32, null);

video.Source = image;

colorFrame.CopyPixelDataTo(pixels);

image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), pixels, image.PixelWidth * sizeof(int), 0);

但是图片没有显示。我知道我可以连接到 Kinect,因为我可以改变仰角。我究竟做错了什么?提前致谢。 注意:我试图避免使用 Coding4Fun

【问题讨论】:

    标签: c# wpf image image-processing kinect


    【解决方案1】:

    不,你离得太近了。我假设 video 是 WPF 图像。

    private WriteableBitmap wBitmap; 
    private byte[] pixels;
    
    private void WindowLoaded(...)
    {
        //set up kinect first, but don't start it
        ...
    
        pixels = new byte[sensor.ColorStream.FramePixelDataLength];
    
        wBitmap = new WriteableBitmap(sensor.ColorStream.FrameWidth, sensor.ColorStream.FrameHeight, 
            96, 96, PixelFormats.Bgra32, null);
    
        video.Source = wBitmap;
    
        sensor.Start();
    }
    
    private void ColorFrameReady(object sender, ColorImageFrameReadyArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame == null)
            {
                return;
            }
    
    
            colorFrame.CopyPixelDataTo(pixels);
    
            wBitmap.WritePixels(new Int32Rect(0, 0, wBitmap.PixelWidth, wBitmap.PixelHeight),
                pixels, image.PixelWidth * 4, 0);
        }
    }
    

    您不应该在每一帧上都创建一个新的 BitmapSource,最好在一开始就创建一个 WriteableBitmap,然后在每一帧上刷新它。

    此外,您之前看不到图像的原因实际上并不是因为它没有被刷新。绝对是,但它是看不见的。您将 WriteableBitmap 格式设置为 Bgra32,其中a 是 alpha。 Kinect 以 Bgr32 格式发送数据;没有设置 Alpha 通道。因此,当您创建 Bgra32 位图时,它会看到 Kinect 将 alpha 通道设置为 0,因此图像显示为完全透明。

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      相关资源
      最近更新 更多