C++ OpenCV 2.4.11:列出所有相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34197825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ OpenCV 2.4.11: List all cameras
提问by Loomis
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:
我想列出所有连接的网络摄像头(USB 网络摄像头和内部网络摄像头),使用 C++、OpenCV 2.4.11、Windows 8.1 和 Qt Creator 3.4.2。对我来说,通过以下方式获取可访问网络摄像头的数量就足够了:
VideoCapture videoCapture(0); // Will access my internal laptop webcam.
VideoCapture videoCapture(1); // Will access the first connected usb webcam.
Here is my code:
这是我的代码:
// 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++;
}
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):
如果我激活了内部网络摄像头,try-block 中的代码就可以工作。当我在硬件管理器中停用内部摄像头(并且没有其他摄像头连接到我的笔记本电脑)并显示以下错误消息(调试模式)时,调用失败:
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).
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).
and the following 2 build problems:
以及以下 2 个构建问题:
Exception at 0x7ff871af8b9c, code: 0xa1a01db1: , flags=0x0 (first chance) Exception at 0x7ff8533d9090, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
Exception at 0x7ff871af8b9c, code: 0xa1a01db1: , flags=0x0 (first chance) Exception at 0x7ff8533d9090, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
How can I fetch the occuring error? As you see, try/catch does not work.
如何获取发生的错误?如您所见,try/catch 不起作用。
Or is there a method where I can access all available webcams in OpenCV without such a dirty loop?
或者有没有一种方法可以在没有这种脏循环的情况下访问 OpenCV 中的所有可用网络摄像头?
采纳答案by avtomaton
There is still no any functionality related to camera count in OpenCVat the current moment (3.0.0version) - see corresponding ticket.
目前OpenCV中仍然没有任何与相机数量相关的功能(3.0.0版本) - 请参阅相应的票证。
Proper camera handling seems like OpenCVinternal problem (for example, described hereor 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).
正确的相机处理看起来像是OpenCV内部问题(例如,描述在这里或这里)。通常它在物理禁用相机后出现在捕获代码中,而它仍然在OpenCV 中打开(当我们尝试读取被破坏的文件描述符时)。
Generally you can even implement your own handler for access violations (please look into this thread), but it's really dirty trick.
通常,您甚至可以为访问冲突实现自己的处理程序(请查看此线程),但这确实是一个肮脏的技巧。
回答by dagilpe
I created this C++ class that allows enumerating devices (including the ID) to be used inside OpenCV. It is hosted on GitHub.
我创建了这个 C++ 类,它允许在 OpenCV 中使用枚举设备(包括 ID)。它托管在 GitHub 上。
https://github.com/studiosi/OpenCVDeviceEnumerator
https://github.com/studiosi/OpenCVDeviceEnumerator
The idea is to use DirectShow to get all the devices that have the category with GUID CLSID_VideoInputDeviceCategory, and then, through an enumerator, you get in which order they appear on the system, which is the ID you need to open them on OpenCV by creating a VideoCapture object (by using the constructor that receives the ID, which would be the index of the device on the enumeration). Obviously, this approach only works on Windows.
这个想法是使用 DirectShow 获取所有具有 GUID CLSID_VideoInputDeviceCategory 类别的设备,然后,通过枚举器,您可以获得它们在系统中出现的顺序,这是您通过创建在 OpenCV 上打开它们所需的 ID一个 VideoCapture 对象(通过使用接收 ID 的构造函数,这将是枚举中设备的索引)。显然,这种方法只适用于 Windows。