0

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

//#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: In function '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&’

你能帮忙吗。我已经做了几个小时没有成功:(

4

1 回答 1

4

因为 class 没有operator!重载cv::Mat在文档中,没有明确说明在获取失败的情况下应该如何处理图像。那是cv::VideoCapture::operator>>from的实现cap.cpp

VideoCapture& VideoCapture::operator >> (Mat& image)
{
    if(!grab())
        image.release();
    else
        retrieve(image);
    return *this;
}

现在转到. cv::Mat:release让我们仔细检查一下它的实现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,因为它显式返回是否成功:

if (!capture.read(frame)) break;

高温高压

于 2012-12-05T07:02:30.030 回答