无法使用OpenCV从辅助摄像头读取VideoCapture中的帧

Lih*_*ihO 7 c++ usb video opencv video-capture

码:

与主网络摄像头(设备0)完美配合的简单示例:

VideoCapture cap(0);

if (!cap.isOpened()) {
     std::cout << "Unable to read stream from specified device." << std::endl;
     return;
}

while (true)
{
    // retrieve the frame:
    Mat frame;
    if (!cap.read(frame)) {
        std::cout << "Unable to retrieve frame from video stream." << std::endl;
        break;
    }
    // display it:
    imshow("MyVideo", frame);

    // check if Esc has been pressed:
    if (waitKey(1) == 27) {
        break;
    }
    // else continue:
}

cap.release();
Run Code Online (Sandbox Code Playgroud)

问题:

我有第二个网络摄像头,我想使用它.但是,当我替换VideoCapture cap(0);VideoCapture cap(1);,正在正确打开流(或至少cap.isOpened()返回true),cap.read(frame)调用返回false,我无法找出原因.

我尝试过的:

硬件,操作系统,软件详情:

我的硬件:HP ProBook 4510s内置网络摄像头,
外观完美+外部网络摄像头CANYON CNR-FWCII3,操作系统称为"USB视频设备"(麻烦的)OS,SW:Windows 8.1 Pro x86,Visual Studio 2012 Pro ,OpenCV 2.4.8~使用vc11 build

问题:

  1. 我错过了什么吗?
  2. 还有什么我可以做的吗?
  3. 是否至少有任何方法可以检索有关问题实际可能是什么的其他信息?

...在这种情况下,OpenCV的API似乎很差,而且在人们似乎面临类似问题的地方,有人声称它是"OS/HW depnendant"作为借口.

任何帮助将不胜感激.

Lih*_*ihO 5

一段时间后,我发现它始终只是第一次调用read失败并跳过第一帧开始工作正常,虽然这种行为的真正原因仍然未知.

后来詹姆斯·巴尼特(见上述评论)曾指出,其原因可能是需要一段时间,直到相机捕捉已经准备好了和我目前的解决方案看起来下列方式(C++ 11的睡眠):

#include <chrono>
#include <thread>
...

VideoCapture cap(1);

// give camera some extra time to get ready:
std::this_thread::sleep_for(std::chrono::milliseconds(200));

if (!cap.isOpened()) {
     std::cout << "Unable to read stream from specified device." << std::endl;
     return;
}

while (true)
{
    // retrieve the frame:
    Mat frame;
    if (!cap.read(frame)) {
        std::cout << "Unable to retrieve frame from video stream." << std::endl;
        continue;
    }

    // display it:
    imshow("LiveStream", frame);

    // stop if Esc has been pressed:
    if (waitKey(1) == 27) {
        break;
    }
}

cap.release();
Run Code Online (Sandbox Code Playgroud)

希望未来的一些访客会发现它有用:)