2017-01-05 36 views
0

私はC#プログラマー(WPF)で、ライブビデオ画像の処理に問題があります。 MY ENGLISH非常に悪いC#WPF AForge:ビデオを滑らかにする方法は?

することは、私は私の質問をDESCRIPTするために私の全力を尽くす:

私はAForge.netでビデオを再生するには、次のコードを使用します。NewVideoFrameで

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

を、私はビットマップを与えます観測可能なビットマップモデルのオブジェクト:

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(); 
} 

それから私は、WPFのImageコントロールにビットマップを変換するImageSourceはコンバータにビットマップを使用します。

このような最後の、私のXAMLで
// 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(); 
    } 

} 

<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でライブイメージを表示するのが悪い方法ですか?

簡単に言えば、画像処理でスムーズな動画を作成する方法についてすべてを知りたいのですが、ありがとう。

+0

ビットマップをMemoryStreamにエンコードしないでください。その後、すべてのフレームに*のストリーム*から新しいBitmapImageを作成してください。 WriteableBitmap * once *を作成し、[this answer](http://stackoverflow.com/a/30729641/1136211)に示すように生のピクセルデータをコピーしてください。 – Clemens

答えて

0

Iは、画像処理の仕事をするために別のスレッドを使用して、自分自身をそれを把握:

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; 
} 

は、今のフレームレートが通常です。

これは、ライブビデオ画像処理の初心者に役立ちます。

関連する問題