使用OpenCV打开和读取AVI文件-Ubuntu [英] Open and read avi files with OpenCV - Ubuntu

查看:654
本文介绍了使用OpenCV打开和读取AVI文件-Ubuntu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在R. Laganiere撰写的《 OpenCV 2计算机视觉应用编程指南》一书中读到了这本书:

I have just read this in the book OpenCV 2 Computer Vision Application Programming Cookbook by R. Laganiere :

请务必注意,为了打开指定的视频 文件,您的计算机必须安装了相应的编解码器, 否则cv :: VideoCapture将无法理解输入 文件.通常,如果您能够打开包含视频的视频文件 播放器(例如Windows Media Player)上的播放器,然后是OpenCV 还应该能够读取此文件.

It is important to note that in order to open the specified video file, your computer must have the corresponding codec installed, otherwise cv::VideoCapture will not be able to understand the input file. Normally, if you are able to open your video file with a video player on your machine (such as the Windows Media Player), then OpenCV should also be able to read this file.

不幸的是,事情对我而言并不那么容易.是的,我可以在视频播放器上读取avi文件,但是它不适用于我的OpenCV-Qt应用程序.尽管路径正确,但VideoCapture的isOpen()方法仍返回false,并且似乎每个所需的编解码器都在此处.我尝试了几个文件,所以它与一种特定格式无关.

Unfortunately, things aren't that easy for me. Yes, I can read avi files on my video player, however it does not work with my OpenCV-Qt application. VideoCapture isOpen() method returns false, despite the fact that the path is correct, and every codec needed seem to be here. I tried several files, so it is not related to one particular format.

这里有人在使用OpenCV在Ubuntu中打开avi文件方面有经验吗?我认为这是一个大问题,无法在互联网上找到任何真正相关的解决方案...

Does someone here has experience in opening avi files in Ubuntu using OpenCV ? I think this is a big issue, can't find any trully relevant solution on the internet...

谢谢!!

这是我正在使用的功能;这里的某些变量是类成员,因此它可能看起来不完整.但是,正是这段代码无法正常工作.特别是我实例化一个新的VideoCapture对象的行.

Here is the function I am working on; some of the variables here are class members, so it may looks incomplete. However, it is this very piece of code that is not working. In particular, the line where I instanciate a new VideoCapture object.

void MainWindow::on_actionOuvrir_fichier_triggered()
{
    //mettre a -1 streamId
    streamId = -1;
    //get path to the avi file
    QString fileName = QFileDialog::getOpenFileName(this,tr("Ouvrir fichier video"),"/home", tr("Videos (*.avi)"));
    std::string utf8_text = fileName.toUtf8().constData();
    //open .avi
    capture = new VideoCapture(utf8_text);
    //check 
    if(!capture->isOpened())
        cout << "probleme ouverture fichier video" << endl;
    //delay between each frame in ms
    rate = capture->get(CV_CAP_PROP_FPS);
    delay = 1000 / rate;
    //start Qtimer recordId
    recordId = startTimer(delay);
    //capture first frame
    if(!capture->read(in))
        cout << "probleme lecture frame fichier video" << endl;
}

在Windows 7上进行测试

test on Windows 7

void MainWindow::on_actionOuvrir_fichier_triggered()
{
    //mettre a -1 streamId
    streamId = -1;
    //ouvrir fenetre navigation fichiers pour recuperer path vers .avi
    QString fileName = QFileDialog::getOpenFileName(this,tr("Ouvrir fichier video"),"/home",
                                                    tr("Videos (*.avi)"));
    //std::string utf8_text = fileName.toUtf8().constData();
    std::string current_locale_text = fileName.toLocal8Bit().constData();
    if(QDir().exists(current_locale_text.c_str())) std::cout << "Path is good!" << endl;
    //ouvrir .avi
    capture = new VideoCapture(current_locale_text);
    //check ouverture
    if(!capture->isOpened())
        cout << "probleme ouverture fichier video" << endl;
    //calculer delay between each frame in ms
//    rate = capture->get(CV_CAP_PROP_FPS);
//    delay = 1000 / rate;
    //demarrer timer recordId
    recordId = startTimer(100);
    //capture premiere frame
    if(!capture->read(in))
        cout << "probleme lecture frame fichier video" << endl;
}

使用该代码,我可以打开一些avi文件,但不是全部(实际上距离不是很远).所以我想我绝对是有编解码器问题的...有人可以告诉我如何在Ubuntu下解决此问题吗?不要让我为那一个而赏金!非常感谢.

With that code I was able to open SOME avi files, but not all (actually far from that). So I guess I definitely have a codec issue... does anyone can tell me how to fix this under Ubuntu ? Don't let me go on bounty for that one ! Thank you very much.

根据 Etienne 的建议,我遵循了此处,并尝试使用给定命令行的mencoder,将我的视频转换为OpenCV在所有平台上支持的I420格式.因此,根据VLC,我从24位RGB(RV24)编解码器转到了4:2:0 YUV(I420)平面.尽管行为相同,但我仍然无法实例化VideoCapture对象.

As suggested by Etienne, I followed instructions here and tried to convert my video to the I420 format supposedly supported by OpenCV on all platforms, using mencoder with the command line given. So I went from 24 bits RGB (RV24) codec to Planar 4:2:0 YUV (I420), according to VLC. Same behavior though, I'm still unable to instanciate the VideoCapture object.

有很多未解决的案例类似于我的Stack Overflow ...

THERE IS A LOT OF UNSOLVED CASES similar to mine on Stack Overflow...

推荐答案

我终于设法解决了我的问题.

I finally managed to solve my problem.

我做了什么:

  • 尝试打开一些视频,最后找到可以使用的视频.幸运的是,我只用了3个视频就找到了一个可行的视频.
  • 使用VLC检查编解码器.这是MPEG-4 XVID.
  • 使用mencoder将我想读取的文件转换为XVID:

  • try to open some video to finally find one that would work. Fortunately it only took me 3 videos to find one that work.
  • check the codec with VLC. It is MPEG-4 XVID.
  • use mencoder to convert the file I wanted to read to XVID:

sudo mencoder myFile.avi -ovc lavc vcodec=mpeg4:mbd=2:cbp:trell:vbitrate=300 -ffourcc XVID -o test.avi

我还切换到了旧的 C 功能cvCaptureFromFile().我将返回 C ++ 界面以确保确实确实是编解码器问题,但是我很确定这是( 这是一个编解码器问题).

I also switched to old C function cvCaptureFromFile(). I will go back to C++ interface to make sure it was indeed a codec issue, but I am pretty sure it was ( it was a codec issue).

什么没用:

  • 在启用了Qt支持的情况下重新编译OpenCV

这篇关于使用OpenCV打开和读取AVI文件-Ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆