繁体   English   中英

C#WPF AForge:如何使视频流畅?

[英]C# WPF AForge : How to make video smooth?

我是C#程序员(WPF),并且遇到实时视频图像处理问题。

我的英语很差,我会尽力描述我的问题:

我使用以下代码通过AForge.net播放视频:

// persume resolution is 640x480
// presume snapshot is not available
_cameraDevice.OnNewVideoFrame += NewVideoFrame;

在NewVideoFrame中,我将Bitmap对象提供给可观察的Bitmap模型:

private Bitmap _liveView = null;
public Bitmap LiveView { get { return _liveView; } set { SetProperty(ref _liveView, value); } }

private void NewVideoFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    LiveView = (Bitmap)eventArgs.Frame.Clone();
}

然后,我使用位图到ImageSource转换器将位图转换为WPF图像控件:

// Bitmap to ImageSource Converter
public class BitmapToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return null;

        try
        {
            System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)value;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (bmp.RawFormat.Guid == System.Drawing.Imaging.ImageFormat.MemoryBmp.Guid)
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            else
                bmp.Save(ms, bmp.RawFormat);
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }
        catch
        {
            return null;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

}

最后,我的xaml是这样的:

<Image Source="{Binding LiveView, Converter={StaticResource BitmapToImageSourceConverter}}"/>

然后我运行该应用程序,一切看起来都很好。 但是,当我尝试向NewVideoFrame添加一些逻辑时,视频变得非常慢:

private void NewVideoFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    LiveView = (Bitmap)eventArgs.Frame.Clone();

    SomeLiveVideoFaceDetectFunction((Bitmap)eventArgs.Frame.Clone()); 
    OrSomeShapeDetectionFunction((Bitmap)eventArgs.Frame.Clone());
    // Such functions like these 2 make video very slow.
    // how slow? maybe 5-10 frames per second with 640x480 reslution
    // Logitec C920 camera, it's a very good device.
}

SomeLiveVideoFaceDetectFunction()由Innovatrics IFace提供,在向他们展示的演示中,带有面部检测的实时视频非常流畅,因此我相信他们的SDK并不是问题。

如果他们的SDK不错,那么我的代码就是问题所在。

我的问题是:

  1. 通常每个人如何制作实时取景图像处理逻辑? 我的想法正确吗?

  2. 转换器将位图转换为图像源,这是在WPF中显示实时图像的一种坏方法吗?

简而言之,我想了解有关如何通过图像处理制作流畅视频的全部知识,谢谢。

我自己弄清楚了,使用另一个线程来执行图像处理工作:

private Bitmap _imageToProcess = null;
private bool _processingImage = false;

private void NewVideoFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    _imageToProcess = (Bitmap)eventArgs.Frame.Clone();
    LiveView = (Bitmap)eventArgs.Frame.Clone();
}

private void StartProcessImage()
{
    Thread t = new Thread(ProcessImage);
    t.Start();
    _processingImage = true;
}

private void ProcessImage()
{
    while(_processingImage)
    {
        SomeLiveVideoFaceDetectFunction((Bitmap)_imageToProcess.Clone()); 
        OrSomeShapeDetectionFunction((Bitmap)_imageToProcess.Clone());
    }

    _processingImage = false;
}

现在,帧速率是正常的。

希望这对实时视频图像处理的初学者有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM