0

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

性能损失的特点是: - Window 上显示的视频不再流畅 - 视频录制似乎丢失了一些帧,导致视频速度比正常速度更快/更低。

你能帮我告诉我这个问题可能来自哪里吗?

4

2 回答 2

1

尝试使用RecyclableMemoryStream而不是 MemoryStream。它旨在解决一些内存问题。

查看这篇文章了解详细信息 -宣布 Microsoft.IO.RecycableMemoryStream

于 2016-02-15T15:00:04.223 回答
0

您是否尝试过在单独的线程中执行内存写入 i/o,同时将数据保存在像队列一样的缓冲区中?

于 2014-03-28T17:00:32.990 回答