使用opencv显示视频

时间:2012-12-05 06:08:21

标签: c++ opencv computer-vision

您好我正在尝试使用以下代码播放视频:

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

using namespace cv;

int main(int argc, char** argv)
{
    string filename = "anime.avi";
    VideoCapture capture(filename);
    Mat frame;
    if( !capture.isOpened() )
        throw "Error when reading steam_avi";
    namedWindow( "w", 1);
    for( ; ; )
    {
        capture >> frame;
        if(!frame)
            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
}

当我运行它时,我收到以下错误: project.cpp:在函数'int main(int,char **)'中:

project.cpp:23:13: error: no match for ‘operator!’ in ‘!frame’
project.cpp:23:13: note: candidates are:
project.cpp:23:13: note: operator!(bool) <built-in>
project.cpp:23:13: note:   no known conversion for argument 1 from ‘cv::Mat’ to ‘bool’
/usr/local/include/opencv2/core/operations.hpp:2220:20: note: bool cv::operator!(const cv::Range&)
/usr/local/include/opencv2/core/operations.hpp:2220:20: note:   no known conversion for argument 1 from ‘cv::Mat’ to ‘const cv::Range&’

你能帮忙吗?我已经在这几个小时没有成功了:(

1 个答案:

答案 0 :(得分:4)

因为课程operator!没有重载cv::MatIn the documentation,但没有说清楚,如果采集失败,图像会发生什么。这是来自cv::VideoCapture::operator>>的{​​{1}}的实施:

cap.cpp

现在转到documentation on cv::Mat:release。让我们从VideoCapture& VideoCapture::operator >> (Mat& image) { if(!grab()) image.release(); else retrieve(image); return *this; }

仔细检查它的实现
mat.hpp

因此,最后,您可以检查inline void Mat::release() { if( refcount && CV_XADD(refcount, -1) == 1 ) deallocate(); data = datastart = dataend = datalimit = 0; size.p[0] = 0; refcount = 0; } 指针以找出抓取是否成功:

data

但是,我建议在这种情况下使用函数式调用if (!frame.data) break; ,因为它显式返回它是否成功:

cv::VideoCapture::read

HTH