2015-11-22 23 views
0

我試圖從WPF中的視頻中抓取所有幀並使用emguCV 3.0。Wpf - 無法抓取視頻中的幀?

但是Retrive(frame,0)總是返回空位圖?

我想這個問題發生在Capture(字符串文件名),但我不知道它爲什麼。

任何人都可以向我解釋並給我一些解決方案嗎? 謝謝。

這裏是我的代碼

Capture _capture; 
    private void btnCut_Click(object sender, RoutedEventArgs e) 
      { 
       _capture = new Capture(@"H:\VisualC\HK5\LT Win\ForTesting\TestCutVideo\bin\Debug\Hay.mp4"); 
       _capture.Start(); 
       //bool isReading = true; 
       while (/*isReading*/_capture.Grab()) 
       { 
        Mat frame = new Mat(); 
        _capture.Retrieve(frame, 0); 
        if (frame != null) 
        { 
         imageArray.Add(frame); 
        } 

       } 

       //to Cut list of frames from video. 
       int start = 1, end = 10; 
       start = start * (int)_capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps); 
       end = end * (int)_capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps); 
       VideoWriter vw = new VideoWriter("test.mp4", 15, new System.Drawing.Size(400, 400), true); 
       for (int i = start; i <= end; i++) 
       { 
        vw.Write(imageArray[i]); 
       } 
      } 

回答

0

「而」應該在其他地方調用,否則你只有一次做到了。嘗試

Mat frame = new Mat();//Global 
private void btnCut_Click(object sender, RoutedEventArgs e) 
{ 
    _capture.ImageGrabbed += ImageGrabbedEvent; 
    _capture.Start(); 
} 
void ImageGrabbedEvent(object sender, EventArgs e) { 
    _capture.Retrieve(frame, 0); 
} 

記錄

//new VideoWriter 
VideoWriter vw = new VideoWriter("test.avi", 15, 400, 400, true); 

//maybe a new Thread that include while 
    while(isRunning){ 
     if (frame != null) 
     { 
      Image<Bgr, Byte> videoframe = frame.ToImage<Bgr, Byte>(); 
      vw.WriteFrame(videoframe); 
     } 
    } 
+0

我已經編輯我的代碼,這樣我可以抓住所有的幀。但現在我的問題是從這些幀寫入視頻。雖然我已經寫了250幀,但我的結果視頻無法打開,並且只有6KB的大小和0幀。你可以幫我嗎? – Duy

+1

我無法錄製「mp4」。確保您的畫幅尺寸與您在VideoWriter – Gina

+0

中設置的尺寸相匹配非常感謝! – Duy