opencv不会在mac osx中显示视频

时间:2013-04-18 16:52:07

标签: macos opencv video

我刚用OSX 10.8.3在我的Mac上安装了opencv,我用brew安装了它:

brew install opencv

,版本为2.4.3

>>> ls /usr/local/Cellar/opencv
2.4.3

我尝试显示视频。格式为.asf,编解码器为MJPG(我只能用VLC打开它,见截图)

帧数(如果在opencv中打印或在VLC中看到)是相同的。

但是,如果我运行opencv程序,只显示第一帧。另一个不......为什么??

这是opencv代码

#include <iostream> 
#include <string> 
#include <sstream>

#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/highgui/highgui.hpp> 

using namespace std;
using namespace cv;


int main(int argc, char *argv[]) {

    Mat frame;
    int numframe = 0;

    if (argc != 2) {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source = argv[1];
    VideoCapture capture(source);
    namedWindow("video", CV_WINDOW_AUTOSIZE);

    if (!capture.isOpened()) {
        cout  << "Could not open " << source << endl;
        return -1;
    }

    for(;;) {
        capture >> frame;
        numframe++;
        if (frame.empty()) {
            cout << "frame empty.." << endl;
            break;
        }
        cout << numframe << endl;
        imshow("video", frame);
    }

    return 0;
}

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试在启用FFMPEG支持的情况下编译OpenCV,以便能够访问更多编解码器。如果brew无法做到,请使用brew安装ffmpeg和cmake,然后在github上获取OpenCV的源代码并重新编译它们。

答案 1 :(得分:1)

后:

imshow("video", frame);

呼叫:

waitKey(10);

必须致电cv::waitKey()来显示窗口。参数是窗口保持打开的毫秒数。此函数返回在此期间按下的键的ASCII码。

理想情况下,您可以将10替换为使其在正确的FPS处显示帧的数字。换句话说:cv::waitKey(1000 / fps);