C ++ OpenCV 2.4.11:列出所有摄像机

Loo*_*mis 6 c++ webcam opencv video-capture

我想使用C ++,OpenCV 2.4.11,Windows 8.1和Qt Creator 3.4.2列出所有连接的网络摄像头(USB网络摄像头和内部网络摄像头)。对我而言,通过以下方式获取可访问的网络摄像头数量就足够了:

VideoCapture videoCapture(0); // Will access my internal laptop webcam.
VideoCapture videoCapture(1); // Will access the first connected usb webcam.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

// Following procedure detects, how many webcams are accessible from 0 on upwards.
numberOfDevices = 0;
bool noError = true;

while (noError)
{
    try
    {
        // Check if camera is available.
        VideoCapture videoCapture(numberOfDevices); // Will crash if not available, hence try/catch.

        // ...
    }
    catch (...)
    {
        noError = false;
    }

    // If above call worked, we have found another camera.
    numberOfDevices++;
}
Run Code Online (Sandbox Code Playgroud)

如果我激活了内部网络摄像头,则尝试块中的代码将起作用。当我在硬件管理器中停用内置摄像机(并且笔记本电脑未连接其他摄像机)时,调用失败,并显示以下错误消息(调试模式):

Exception Triggered
---------------------------
The inferior stopped because it triggered an exception.<p>Stopped in thread 0 by: 
Exception at 0x7ff8533d9090, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance).
Run Code Online (Sandbox Code Playgroud)

和以下两个构建问题:

Exception at 0x7ff871af8b9c, code: 0xa1a01db1: , flags=0x0 (first chance)

Exception at 0x7ff8533d9090, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
Run Code Online (Sandbox Code Playgroud)

如何获取发生的错误?如您所见,try / catch不起作用。

还是有一种方法可以使我在OpenCV中访问所有可用的网络摄像头而又不会出现这种麻烦的循环?

avt*_*ton 5

目前OpenCV中仍然没有任何与相机数量相关的功能(3.0.0版本) - 请参阅相应的票证

正确的相机处理看起来像是OpenCV内部问题(例如,描述在这里这里)。通常它在物理禁用相机后出现在捕获代码中,而它仍然在OpenCV 中打开(当我们尝试读取被破坏的文件描述符时)。

通常,您甚至可以实现自己的访问冲突处理程序(请查看此线程),但这确实是一个肮脏的技巧。