shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Different open cameras depending on the videoCaptureApi used #1663

Open P4TTT0 opened 2 months ago

P4TTT0 commented 2 months ago

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

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

simphonydeveloper commented 4 days ago

I also have the same situation, I have a device that passes indexes and calls different cameras. you have resolved it?