Background
I'm developing a C# WinForms application uses OpenCvSharp4 to capture images from one or more connected webcam on demand.
I currently have code that captures from the first camera using var capture = new VideoCapture(0)
. This works great for the first connected camera (i.e. index 0), and any other index I specify manually if the camera is connected.
Question
I want each deployment to dynamically choose the cameras to capture from. Other than trial and error, how can I find the index of all available webcams on the current system so I can create a list of camera indexes to cycle through later?
Intended usage
I was hoping to use it something like this:
void main()
{
var cameras = GetAvailableCameras();
foreach (var idx in cameras)
{
var capture = VideoCapture(idx);
// capture the image...
}
}
IEnumerable<int> GetAvailableCameras()
{
// do something here to get the available indexes
}
Caveats
To keep the program a lean as possible, I would like to avoid adding any additional third party libraries, which is why I have not used AForge.Net even though I know there are published solutions using that library.
I would consider adding additional Microsoft library if it simplifies things.