opencvsharp
opencvsharp copied to clipboard
Different open cameras depending on the videoCaptureApi used
Issue
I'm trying to initialize my camera with VideoCapture by passing the camera index and the videoCaptureApi. The issue is that depending on the VideoCaptureApi, the camera index points to one camera or another. Why does this happen and how could it be resolved?
Environment
- Operating System: Windows 11 Pro
- Development Environment: Visual Studio 2022
- Dependencies:
- DirectShowLib (1.0.0)
- OpenCvSharp4.Windows (4.9.0.20240103)
- Cameras:
- Microsoft LifeCam Cinema
- ELP-USBFHD06H-BL36
Example code:
DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (devices.Length == 0)
{
Console.WriteLine("No se encontraron dispositivos de captura de video.");
return;
}
DsDevice webcam = devices[0];
Console.WriteLine($"Usando la webcam: {webcam.Name}");
using VideoCapture capture = new VideoCapture(0);
if (!capture.IsOpened())
{
Console.WriteLine("No se pudo abrir la webcam.");
return;
}
using (Window window = new Window("Webcam"))
{
while (true)
{
Mat frame = new Mat();
capture.Read(frame);
window.ShowImage(frame);
int key = Cv2.WaitKey(30);
if (key == 27)
break;
}
}
Using
using VideoCapture capture = new VideoCapture(0, VideoCaptureAPIs.ANY);
Output
Opened Camera: ELP-USBFHD06H-BL36
Using
using VideoCapture capture = new VideoCapture(0, VideoCaptureAPIs.DSHOW);
Output
Opened Camera: Microsoft LifeCam Cinema
I also have the same situation, I have a device that passes indexes and calls different cameras. you have resolved it?
Having the same issue. Using DsDevice, It mean you are fetching video device input using DirectShowLib. The result for me look like this.
[0 = 'Camera 1', 1 = 'Camera 2']
Which work correctly when you used VideoCaptureAPIs.DSHOW. But If i used VideoCaptureAPIs.MSMF or VideoCaptureAPIs.ANY (Which apparently picked VideoCaptureAPIs.MSMF first. You can see the result yourself with GetBackendName() method from OpenCVSharp.VideoCapture.) . The device order are reversed (or simply return differently) And look like this instead
[0 = 'Camera 2', 1 = 'Camera 1']
I've been trying a lot of things. From switching from DsDevice to just follow this guide to fetch device using interop But no luck. The order always return just like DsDevice. While MSMF switched them. It appears to be happened only on MSMF as well.
I also tried the hard way, put camera index + domain_offset as found in StackOverflow. Given that MSMF = 1400. I tried,
VideoCapture videoCapture = VideoCapture.FromCamera(index: 1401, apiPreference: VideoCaptureAPIs.MSMF);
This return the output as Camera 1 as expected. But obviously not ideal solution.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.