OpenCV getCudaEnabledDeviceCount() returning -1

Building an OpenCV app with MSVC 2017 using the opencv[cuda] package installed by vcpkg. To check for usable CUDA devices I call cv::getCudaEnabledDeviceCount() and it returns -1, which OCV documents as meaning CUDA is supported but the driver is incompatible. Reinstalling opencv[cuda] with vcpkg did not help.
How can I find out what is actually going wrong here?

If that is so, this should be easily fixable by installing the latest driver package available for your GPU. Unless your GPU is so old that it is no longer supported by CUDA. What GPU is in this system? Are you able to run any other CUDA enabled apps, e.g. the sample apps that ship with CUDA? What is returned by cudaGetDeviceCount()? Is there an environment variable CUDA_VISIBLE_DEVICES?

Other cuda apps run on this box, a Sager gaming laptop with GTX1060 and Intel Integrated Graphics. The CUDA setup is toolkit 10.2, driver 425, which is current. Toolkits 9.0 and 10.1 are also installed.
If you can tell me what header and library I need for cudaGetDeviceCount() I will be glad to call it and let you know the result.

I am not completely sure, but I think driver version 425 is too old for CUDA 10.2, but may be sufficient for CUDA 10.1. I would suggest updating to the latest driver available. I am not sure why you state the driver is current. I do not update drivers frequently, but have driver 456.81 from October 2020 installed on my system with a Pascal-based GPU. I also have an older system with driver 441.66 dated December 2019.

I don’t know what your code base and build system looks like. Inside a .cu (CUDA source) file, you can simply invoke cudaGetDeviceCount(). The CUDA toolchain knows what is needed. E.g. if I put in a file cuda_get_device_count.cu:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    int count = 0;
    cudaError_t stat = cudaGetDeviceCount (&count);
    printf ("stat=%08x count=%d\n", stat, count);
    return EXIT_SUCCESS;
}

Compile with nvcc -o cuda_get_device_count.exe cuda_get_device_count.cu. When run it prints on my system: stat=00000000 count=1. Ideally you would put that right before your call to cv::getCudaEnabledDeviceCount(). Any discrepancies between the two counts would be interesting.

I guess you are on the right track. I just built and ran the device query sample from the 10.2 toolkit and it reports the error

CUDA driver version is insufficient for CUDA runtime version

Unfortunately it seems I have the most up-to-date driver for this hardware - version 426, which I guess was installed by Windows Update. The nVidia download page recommends 425.31. Installing that gave no relief. I guess I shall have to fall back to an older cuda runtime.

Good news. Uninstalled cuda 10.2, the 10.1 samples now work (with driver 425.31)

That is rather odd. I went here: https://www.nvidia.com/en-us/geforce/drivers/ chose the manual driver search for Windows 10 64bit, GTX 1060 and the latest WHQL driver is 461.09 released Jan 7th 2021.

My machine is an old Sager gaming laptop and the most recent recommended ‘gaming’ display driver is 425.31. I tried installing the ‘studio’ driver you mention but that failed with a hardware incompatibility check. I would not be surprised if this had something to do with the presence of Intel Integrated Graphics, which causes all sorts of trouble with GPU usage on laptops.

SOLVED. I had installed CUDA toolkit version 10.2, but that no longer supports my old GTX680M gpu. Rolling back to version 10.1 fixed it.

Your original posts stated your machine has a GTX 1060. I saw no mention of a GTX 680M. It is always a good idea to state the full machine configuration when seeking assistance.