2012-07-28 128 views
-1

我正在使用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

+0

@downvoter有什么不对? – 2012-08-14 19:37:27

回答

0

不,你是如此亲密。我假设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,因此图像显示为完全透明。