Lia*_*roy 0 c# wpf frame-rate depth kinect
我有一个应用程序,我创建自己的深度框架(使用Kinect SDK).问题是当检测到人的深度的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;
}
Run Code Online (Sandbox Code Playgroud)
FPS对我来说非常重要,因为我正在创建一个可以在检测到人物时保存图片的应用程序.如何保持更快的FPS?为什么我的应用程序这样做?
GY是正确的,你没有妥善处理.您应该重构代码,以便尽快处理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){...}
Run Code Online (Sandbox Code Playgroud)
你说你在应用程序的其他地方使用了深度框架.这是不好的.如果您需要深度框架中的某些特定数据,请单独保存.
dowhilefor也是正确的,你应该看看使用WriteableBitmap,它非常简单.
private WriteableBitmap wBitmap;
//somewhere in your initialization
wBitmap = new WriteableBitmap(...);
depth.Source = wBitmap;
//Then to update the image:
wBitmap.WritePixels(...);
Run Code Online (Sandbox Code Playgroud)
此外,您正在创建新阵列以在每个帧上反复存储像素数据.您应该将这些数组创建为全局变量,一次创建它们,然后在每一帧上覆盖它们.
最后,虽然这不应该产生巨大的差异,但我对你的CalculateIntensityFromDepth方法感到好奇.如果编译器没有内联该方法,那就是很多无关的方法调用.尝试删除该方法,只需编写现在方法调用的代码.