【问题标题】:Wpf - Can't grab frames from a video?Wpf - 无法从视频中抓取帧?
【发布时间】:2015-11-27 17:00:26
【问题描述】:

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

但是 Retrive(frame, 0) 总是返回具有空位图的帧?

我猜这个问题出现在 Capture(string filename) 但我不知道为什么会这样。

谁能给我解释一下并给我一些解决方案? 谢谢。

这是我的代码

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

【问题讨论】:

    标签: wpf emgucv


    【解决方案1】:

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

    【讨论】:

    • 我已经编辑了我的代码,以便可以抓取所有帧。但现在我的问题是从这些帧中编写视频。虽然我已经写了 250 帧,但我的结果视频无法打开,只有 6KB 大小和 0 帧。你能帮帮我吗?
    • 我无法录制“mp4”。确保您的帧大小与您在 VideoWriter 中设置的相匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多