【问题标题】:opencv library video play [closed]opencv库视频播放[关闭]
【发布时间】:2012-03-18 18:44:08
【问题描述】:

我想播放视频

如何使用 opencv 完成?

【问题讨论】:

  • 粘贴您目前编写的代码,以便我们帮助您修复它。
  • 顺便说一句,如果你不想在 OpenCV 的原生窗口中显示任何内容,你应该删除所有对 cvNamedWindow()cvShowImage()cvDestroyWindow() 的调用。

标签: c++ opencv


【解决方案1】:

我正在使用our previous conversation 中的代码构建一个最小示例,该示例展示了如何从相机中检索帧并将它们显示在QLabel

请注意,传递给cvCreateCameraCapture() 的值可以在您的系统上更改。在我的 Linux 上,0 是神奇的数字。在 Windows 和 Mac 上,我使用 -1

为简单起见,我只是分享main() 函数的主体:

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    CvCapture* capture = cvCreateCameraCapture(0); // try -1 or values > 0 if your camera doesn't work
    if (!capture)
    {   
        std::cout << "Failed to create capture";
        return -1; 
    }   

    IplImage* frame = NULL;
    QLabel label;
    while (1) 
    {   
        frame = cvQueryFrame(capture);
        if( !frame ) break;

        QImage qt_img = IplImage2QImage(frame);
        label.setPixmap(QPixmap::fromImage(qt_img));
        label.show();

        // calling cvWaitKey() is essencial to create some delay between grabbing frames
        cvWaitKey(10); // wait 10ms
    }   

    return app.exec();
}

编辑:

播放视频文件或来自摄像机的视频的过程是相同的。唯一真正改变的是cvCreateCameraCapture() for cvCreateFileCapture()

如果cvCreateFileCapture()返回NULL,则表示无法打开视频文件。这可能有两个原因:它没有处理视频的编解码器,或者找不到文件。

很多人在 Windows 上加载文件时会犯以下错误:

capture = cvCreateFileCapture("C:\path\video.avi");

他们使用单斜杠,而应该使用双斜杠:

capture = cvCreateFileCapture("C:\\path\\video.avi");

如果某些事情可能失败,您必须始终检查调用的返回

capture = cvCreateFileCapture("C:\\path\\video.avi");
if (!capture)
{
   // print error message, then exit
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多