简体   繁体   中英

Displaying video using opencv

Hi I am trying to play a video using the following code:

//#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
}

When I run it though, I get the following errors: 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&’

Could you possibly help. I've been on this for hours without success :(

Because there is no operator! overloaded for class cv::Mat . In the documentation , it not said clearly, what should happen with the image in case of acquisition failed. That's the implementation of cv::VideoCapture::operator>> from cap.cpp :

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

Now go to documentation on cv::Mat:release . And let's double check it's implementation from the 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;
}

Hence, finally, you can check the data pointer to find out, whether the grab was successful:

if (!frame.data) break;

However, I recommend to use function-style call cv::VideoCapture::read in this case, since it explicitly returns whether it was successful, or not:

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

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM