Unclear and unstable meaning of VideoCapture camera index on Windows (MSMF) when multiple cameras are present

Environment

  • OS: Windows 11

  • Compiler: MSVC (Visual Studio 2022)

  • OpenCV version: (please fill in, e.g. 4.9.0)

  • Backend: MSMF

  • Related framework: Qt (QCameraInfo)


Description

I am experiencing serious ambiguity and instability regarding the meaning of the camera index used by cv::VideoCapture on Windows.

In addition, my project needs to run on both Linux and Windows; I am currently testing it on Windows only.

In a system with multiple cameras (e.g. an integrated camera + one or more USB cameras), the same numeric index (e.g. 0) does not reliably correspond to the same physical camera across:

  • Different application runs

  • Different boot orders

  • USB plug/unplug events

More importantly, the camera opened by OpenCV using a given index does not necessarily correspond to the camera described by other system-level APIs using the same index.


Practical example (Qt + OpenCV)

In my application, I use Qt to enumerate cameras and show camera information to the user:

QList<QCameraInfo> cameras = QCameraInfo::availableCameras();

Qt provides stable and correct information such as:

  • Camera description (e.g. Integrated Camera)

  • Underlying device identifier (e.g. contains VID / PID)

However, when I pass the same numeric index to OpenCV:

cv::VideoCapture cap(index, cv::CAP_MSMF);

OpenCV may open a different physical camera than the one represented by QCameraInfo[index].

Example scenario:

  • Qt index 0Integrated Camera

  • OpenCV index 0USB Camera

This happens even though both APIs are running in the same process on the same system.


Observed behavior

  • VideoCapture(index) opens the first camera in OpenCV’s internal enumeration order

  • This enumeration order appears to be:

    • Backend-dependent (MSMF / DirectShow)

    • Affected by USB topology and device initialization order

    • Not stable across reboots or hot-plug events

  • There is no documented or exposed way to:

    • Query which physical device a given OpenCV index refers to

    • Bind VideoCapture to a specific device using a stable identifier (VID / PID / device path)


Questions

  1. What exactly is the camera index in cv::VideoCapture based on on Windows (MSMF)?

    • Device enumeration order?

    • Backend-specific internal list?

  2. Could you provide a simple demo showing how to integrate Qt with OpenCV so that the camera index from Qt can be used consistently with the camera index in OpenCV?


Motivation

This behavior makes it difficult to build reliable multi-camera applications on Windows, especially when integrating OpenCV with other frameworks (Qt, Media Foundation, DirectShow, etc.) that expose richer and more stable device information.

Understanding the intended semantics of the camera index would greatly help developers design correct and robust solutions.