C ++ OpenCV 2.4.11:列出所有摄像机 [英] C++ OpenCV 2.4.11: List all cameras

查看:104
本文介绍了C ++ OpenCV 2.4.11:列出所有摄像机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to list all connected webcams (USB-webcams and internal webcams), using C++, OpenCV 2.4.11, Windows 8.1 and Qt Creator 3.4.2. For me it is enough to get the number of accessible webcams in the following way:

VideoCapture videoCapture(0); // Will access my internal laptop webcam.
VideoCapture videoCapture(1); // Will access the first connected usb webcam.

这是我的代码:

// 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++;
}

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

The code in the try-block works if I have activated my internal webcam. The call fails when I deactivate the internal camera in the hardware manager (and no other camera is attached to my laptop) with the following error message (debug mode):

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).

以及以下2个构建问题:

and the following 2 build problems:

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

Exception at 0x7ff8533d9090, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)

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

How can I fetch the occuring error? As you see, try/catch does not work.

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

Or is there a method where I can access all available webcams in OpenCV without such a dirty loop?

推荐答案

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

There is still no any functionality related to camera count in OpenCV at the current moment (3.0.0 version) - see corresponding ticket.

正确处理相机似乎是 OpenCV 内部问题(例如,在此处 此处).通常,在物理禁用相机后,它仍会显示在捕获代码中,而它仍在 OpenCV 中打开(当我们尝试读取损坏的文件描述符时).

Proper camera handling seems like OpenCV internal problem (for example, described here or here). Usually it appears in capture code after physically disabling camera while it is still opened in OpenCV (when we try to read destroyed file descriptor).

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

Generally you can even implement your own handler for access violations (please look into this thread), but it's really dirty trick.

这篇关于C ++ OpenCV 2.4.11:列出所有摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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