2012-07-28 30 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