我正在使用WPF来展示Kinect ColorImageFrame和骨架表示的项目。我还必须录制这两个视频。
我能够显示和记录(使用EmguCV)这两个图像,但我有一些性能问题。似乎我的代码的这一部分是我失去性能的原因。
private void DrawSkeleton(Skeleton[] skeletons)
{
using (System.Drawing.Bitmap skelBitmap = new System.Drawing.Bitmap(640, 480))
{
foreach (Skeleton S in skeletons)
{
if (S.TrackingState == SkeletonTrackingState.Tracked)
{
DrawBonesAndJoints(S,skelBitmap);
}
else if (S.TrackingState == SkeletonTrackingState.PositionOnly)
{
}
}
_videoArraySkel.Add(ToOpenCVImage<Bgr, Byte>(skelBitmap));
BitmapSource source = ToWpfBitmap(skelBitmap);
this.skeletonStream.Source = source;
}
}
更确切地说,来自ToWpfBitmap,它允许我在我的窗口中显示它:
public static BitmapSource ToWpfBitmap(System.Drawing.Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
// Force the bitmap to load right now so we can dispose the stream.
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
性能损失的特点是: - 窗口上显示的视频不再流畅 - 视频录制似乎错过了一些帧,导致视频比正常情况更快/更低。
你可以告诉我这个问题可能来自哪里来帮助我吗?
答案 0 :(得分:1)
尝试使用RecyclableMemoryStream而不是MemoryStream。它旨在解决内存问题。
查看此文章了解详情 - Announcing Microsoft.IO.RecycableMemoryStream
答案 1 :(得分:0)
您是否尝试在单独的线程中执行内存写入i / o,同时将数据保存在缓冲区中,如队列?