【问题标题】:Displaying video using opencv使用opencv显示视频
【发布时间】:2012-12-05 15:02:30
【问题描述】:

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

//#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&’

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

【问题讨论】:

  • 接受答案后,请考虑评论或投票。

标签: c++ opencv computer-vision


【解决方案1】:

因为cv::Mat 类没有重载operator!In the documentation,没说清楚,采集失败时图片怎么办。那就是cap.cppcv::VideoCapture::operator&gt;&gt;的实现:

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

现在转到documentation on 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;

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多