[英]C# WPF AForge : How to make video smooth?
I'm a C# programmer(WPF) and I have a live video image process problem. 我是C#程序员(WPF),并且遇到实时视频图像处理问题。
MY ENGLISH IS VERY BAD, I will try my best to descript my question: 我的英语很差,我会尽力描述我的问题:
I use following code to play video with AForge.net : 我使用以下代码通过AForge.net播放视频:
// persume resolution is 640x480
// presume snapshot is not available
_cameraDevice.OnNewVideoFrame += NewVideoFrame;
In NewVideoFrame, I give the Bitmap object to a observable Bitmap model: 在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();
}
Then I use a Bitmap to ImageSource Converter to convert bitmap to WPF Image control: 然后,我使用位图到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();
}
}
At last, my xaml like this: 最后,我的xaml是这样的:
<Image Source="{Binding LiveView, Converter={StaticResource BitmapToImageSourceConverter}}"/>
Then I run the application, everything looks good. 然后我运行该应用程序,一切看起来都很好。 BUT, when I try to add some logic to NewVideoFrame, the video become very slow:
但是,当我尝试向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() is provided by Innovatrics IFace, the live video with face detection is very smooth in their demo they show me, so I believe their SDK is not the problem. SomeLiveVideoFaceDetectFunction()由Innovatrics IFace提供,在向他们展示的演示中,带有面部检测的实时视频非常流畅,因此我相信他们的SDK并不是问题。
If their SDK's good, then my codes are the problem. 如果他们的SDK不错,那么我的代码就是问题所在。
My question are: 我的问题是:
Normally how everybody make a live-view-image-processing logic? 通常每个人如何制作实时取景图像处理逻辑? Is my idea upon right or not?
我的想法正确吗?
The Converter converts bitmap to imagesource, is it a bad way to show live image in WPF? 转换器将位图转换为图像源,这是在WPF中显示实时图像的一种坏方法吗?
In a nutshell, I want to know the whole thing about how to make a smooth video with image-processing, Thanks. 简而言之,我想了解有关如何通过图像处理制作流畅视频的全部知识,谢谢。
I figure it out myself, use another thread to do image-processing job: 我自己弄清楚了,使用另一个线程来执行图像处理工作:
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;
}
Now the frame rate is normal. 现在,帧速率是正常的。
Hope this helps beginners of Live video image processing. 希望这对实时视频图像处理的初学者有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.