[英]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不錯,那么我的代碼就是問題所在。
我的問題是:
通常每個人如何制作實時取景圖像處理邏輯? 我的想法正確嗎?
轉換器將位圖轉換為圖像源,這是在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.