C++ 从文件 opencv 读取视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13709274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:40:05  来源:igfitidea点击:

reading video from file opencv

c++copencv

提问by Ikemesit Ansa

Hi So I have written this code to capture a video from file

嗨所以我写了这段代码来从文件中捕获视频

#include <stdio.h>
#include <cv.h>
#include "highgui.h"
#include <iostream>

//using namespace cv

int main(int argc, char** argv)
{
    CvCapture* capture=0;
    IplImage* frame=0;
    capture = cvCaptureFromAVI(char const* filename); // read AVI video    
    if( !capture )
        throw "Error when reading steam_avi";

    cvNamedWindow( "w", 1);
    for( ; ; )
    {
        frame = cvQueryFrame( capture );
        if(!frame)
            break;
        cvShowImage("w", frame);
    }
    cvWaitKey(0); // key press to close window
    cvDestroyWindow("w");
    cvReleaseImage(&frame);
}

Everytime I run it, I get the following error:

每次我运行它时,都会出现以下错误:

CaptureVideo.cpp: In function ‘int main(int, char**)':

CaptureVideo.cpp:13:28: error: expected primary-expression before ‘char'

CaptureVideo.cpp:在函数“int main(int, char**)”中:

CaptureVideo.cpp:13:28: 错误:'char' 之前的预期主表达式

Any help will be much appreciated.

任何帮助都感激不尽。

回答by Barney

This is C++ question, so you should use the C++ interface.

这是 C++ 问题,因此您应该使用 C++ 接口。

The errors in your original code:

原始代码中的错误:

  • You forgot to remove char const*in cvCaptureFromAVI.
  • You don't wait for the frame to be displayed. ShowImageonly works if it is followed by WaitKey.
  • I'm not sure if capture=NULL would mean that your file was not opened. Use isOpenedinstead.
  • 你忘记char const*cvCaptureFromAVI.
  • 您无需等待框架显示出来。ShowImage仅在后面跟有 WaitKey 时才有效。
  • 我不确定 capture=NULL 是否意味着您的文件没有打开。使用isOpened来代替。

I have corrected your code and put it into the C++ interface, so it is a proper C++ code now. My rewrite does line-by-line the same as your program did.

我已更正您的代码并将其放入 C++ 接口,因此现在它是正确的 C++ 代码。我的重写与您的程序一样逐行进行。

//#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//#include <iostream>

using namespace cv;
using std::string;

int main(int argc, char** argv)
{
    string filename = "yourfile.avi";
    VideoCapture capture(filename);
    Mat frame;

    if( !capture.isOpened() )
        throw "Error when reading steam_avi";

    namedWindow( "w", 1);
    for( ; ; )
    {
        capture >> frame;
        if(frame.empty())
            break;
        imshow("w", frame);
        waitKey(20); // waits to display frame
    }
    waitKey(0); // key press to close window
    // releases and window destroy are automatic in C++ interface
}