Wpf - 无法从视频中抓取帧?

时间:2015-11-22 16:23:05

标签: wpf emgucv

我正在尝试从WPF中的视频中获取所有帧并使用emguCV 3.0。

但是Retrive(frame,0)总是返回具有null Bitmap的帧?

我猜这个问题发生在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]);
                }
            }

1 个答案:

答案 0 :(得分:0)

“while”应该在其他地方调用,否则你只做了一次。试试

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