为什么无法在OpenCV(C和C ++ API)中打开视频文件? [英] Why would video files not open in OpenCV (both C & C++ APIs)?

查看:92
本文介绍了为什么无法在OpenCV(C和C ++ API)中打开视频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从EmguCV交换到OpenCV,以便可以尝试EmguCV尚未包装的一些OpenCV函数.但是,我无法加载我的简单测试视频文件,该文件在EmguCV中加载没有问题.在查看EmguCV源代码时,我注意到它们调用了OpenCV C函数而不是C ++来初始化从文件(或网络摄像头)的捕获,因此我编写了以下测试代码(在

I am swapping from EmguCV to OpenCV so that I can try some of the OpenCV functions that are not yet wrapped by EmguCV. However I'm failing to load my simple test video file, which loads without issue in EmguCV. Looking at the EmguCV source code I notice that they call the OpenCV C functions rather than C++ to initialise capture from a file (or a webcam) so I wrote the following test code (with help from this answer). Here is my simple code

int _tmain()
{
    string filename = "sample.wmv";
    if (!FileExists(filename.c_str))
    {
        cout << "File does not exist" << endl;  
        return -1;
    }
    else
    {
        cout << "File exists" << endl;
        VideoCapture cap (filename);
        if(!cap.isOpened())
        {
            cout << "Failed to open file in OpenCV C++" << endl;
            //Trying C API too just-in-case
            CvCapture* capture = cvCreateFileCapture(filename.c_str());
            if (!capture)
            {
                cout << "Failed to open file in OpenCV C" << endl;  
            }
            else
            {
                //Grab frames and do stuff
                cvReleaseCapture(&capture);
            }
            return -1;
        }
        else
        {
            //Grab frames and do stuff
        }
    }
    return 0;
}

运行此命令时,我会得到输出

When I run this I get the output

File exists
Failed to open file in OpenCV C++
Failed to open file in OpenCV C

这与文件无关,即发生在"sample.wmv","sample.avi"甚至是OpenCV示例文件"tree.avi"之一中.

This happens regardless of the file, i.e. it happens for "sample.wmv", "sample.avi", and even one of the OpenCV sample files "tree.avi".

为什么?发生了什么事?

Why? What is going on?

(我正在Windows 8和Windows 8.1计算机上运行OpenCV 2.4.6.)

(I'm running OpenCV 2.4.6 on a Windows 8 and a Windows 8.1 machine.)

推荐答案

根据@ tobias-senst关于答案,我需要在本地计算机上构建OpenCV,而不是依赖于标准发行版随附的DLL.使OpenCV在本地构建一直是一件令人头疼的事情(请参见此处此处),但现在我引用的是副本这台机器上构建的OpenCV 2.4.6的版本,我可以打开视频文件.

It appears that, following advice from @tobias-senst on another answer, I needed to build OpenCV on my local machine rather than rely on the DLLs that are provided with the standard distribution. It has been a headache to get OpenCV to build locally (see here, here, and here) but now that I reference to a copy of OpenCV 2.4.6 that was built on this machine I can open the video files.

我不知道为什么这样做,为什么在需求中没有提到它,为什么其他人不经常遇到这个问题,或者默认部署失败了,但是至少我已经解决了.

I have no idea why this works, why it is not mentioned in the requirements, why others are not more frequently having this problem, nor what is failing with the default deployment, but at least I have it fixed.

这篇关于为什么无法在OpenCV(C和C ++ API)中打开视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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