C++ OpenCV 2.4.11: List all cameras

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

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.


I created this C++ class that allows enumerating devices (including the ID) to be used inside OpenCV. It is hosted on GitHub.

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.