2012-07-28 62 views
0

我有一个应用程序,可以创建自己的深度框架(使用Kinect SDK)。问题是当检测到人时,深度的FPS(然后是颜色)显着减慢。 Here是当帧速度变慢时的电影。我使用的代码:当检测到人体时,Kinect Depth FPS显着降低

 using (DepthImageFrame DepthFrame = e.OpenDepthImageFrame()) 
     { 
      depthFrame = DepthFrame; 
      pixels1 = GenerateColoredBytes(DepthFrame); 

      depthImage = BitmapSource.Create(
       depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels1, 
       depthFrame.Width * 4); 

      depth.Source = depthImage; 
     } 

... 

    private byte[] GenerateColoredBytes(DepthImageFrame depthFrame2) 
    { 
     short[] rawDepthData = new short[depthFrame2.PixelDataLength]; 
     depthFrame.CopyPixelDataTo(rawDepthData); 

     byte[] pixels = new byte[depthFrame2.Height * depthFrame2.Width * 4]; 

     const int BlueIndex = 0; 
     const int GreenIndex = 1; 
     const int RedIndex = 2; 


     for (int depthIndex = 0, colorIndex = 0; 
      depthIndex < rawDepthData.Length && colorIndex < pixels.Length; 
      depthIndex++, colorIndex += 4) 
     { 
      int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; 

      int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth; 

      byte intensity = CalculateIntensityFromDepth(depth); 
      pixels[colorIndex + BlueIndex] = intensity; 
      pixels[colorIndex + GreenIndex] = intensity; 
      pixels[colorIndex + RedIndex] = intensity; 

      if (player > 0) 
      { 
       pixels[colorIndex + BlueIndex] = Colors.Gold.B; 
       pixels[colorIndex + GreenIndex] = Colors.Gold.G; 
       pixels[colorIndex + RedIndex] = Colors.Gold.R; 
      } 
     } 

     return pixels; 
    } 

FPS是非常重要的对我,因为我在做检测时,他们可以节省的人的照片的应用程序。我如何维持更快的FPS?为什么我的应用程序会这样做?

+0

这是windows的kinect SDK代码吗? – Fyre 2012-07-28 18:01:24

+0

@Fyre是支票编辑 – 2012-07-28 18:02:54

+0

尝试在玩家'if'之后将强度像素设置置于'其他' – 2012-07-28 18:21:44

回答

7

G.Y是正确的,你没有妥善处置。您应该重构代码,以便DepthImageFrame尽快处理。

... 
private short[] rawDepthData = new short[640*480]; // assuming your resolution is 640*480 

using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) 
{ 
    depthFrame.CopyPixelDataTo(rawDepthData); 
} 

pixels1 = GenerateColoredBytes(rawDepthData);  
... 

private byte[] GenerateColoredBytes(short[] rawDepthData){...} 

你说你在应用程序的其他地方使用深度框架。这不好。如果您需要深度框架中的某些特定数据,请另行保存。

dowhilefor也是正确的,你应该看看使用WriteableBitmap,它非常简单。

private WriteableBitmap wBitmap; 

//somewhere in your initialization 
wBitmap = new WriteableBitmap(...); 
depth.Source = wBitmap; 

//Then to update the image: 
wBitmap.WritePixels(...); 

此外,你正在创建新的阵列来存储像素数据一次又一次地在每一帧。您应该创建这些数组作为全局变量,创建它们一次,然后在每个帧上覆盖它们。

最后,虽然这不应该产生巨大的差异,但我很好奇你的CalculateIntensityFromDepth方法。如果编译器没有内联该方法,那就是很多无关的方法调用。尝试删除该方法,并立即编写方法调用所在的代码。

+0

这和我想的一样。谢谢 – 2012-07-30 14:37:09

相关问题