OpenCV won't compile with cuDNN on Ubuntu 18.04

Hello all!

I am attempting to compile OpenCV with CUDA and CuDNN for a research project I’m working on. Technical details:

OpenCV 4.2
CUDA 11.6
CuDNN 8
Ubuntu 18.04
GeForce RTX 2060 Graphics Card

Here’s the situation:

I’ve successfully installed CUDA and CuDNN on my computer, and can locate both using “whereis”. Navigating to my OpenCV build folder, I attempt to compile it with cmake via the command:

cmake -D CMAKE_BUILD_TYPE=RELEASE
-D CMAKE_INSTALL_PREFIX=/usr/local
-D INSTALL_PYTHON_EXAMPLES=OFF
-D INSTALL_C_EXAMPLES=OFF
-D OPENCV_ENABLE_NONFREE=ON
-D WITH_CUDA=ON
-D WITH_CUDNN=ON
-D OPENCV_DNN_CUDA=ON
-D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/
-D CUDNN_INCLUDE_DIR=/usr/include/
-D ENABLE_FAST_MATH=1
-D CUDA_FAST_MATH=1
-D CUDA_ARCH_BIN=7.5
-D WITH_CUBLAS=1
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules/
-D HAVE_opencv_python3=ON
-D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python
-D BUILD_EXAMPLES=OFF …

When I do this, OpenCV compiles and recognizes CUDA, but try as I might I can’t get it to recognize CuDNN:

Any suggestions as to what I’m doing wrong?

Completely outside my area of expertise, but generally it is important that the various pieces of the software stack are designed to work together. and only certain version combinations will work (check documentation for each relevant component). Is this question and answer on Stackoverflow relevant to your case?

CMAKE in my OpenCV 4.2.0 source code couldn’t find any cuDNN installed in my machine - Stack Overflow

Hello!

It’s actually a pretty similar issue, except it couldn’t find it at all. I eventually realized that it was a version compatibility issue.

Instead of trying to install OpenCV 4.2, I installed OpenCV 4.5 and the problem magically disappeared. Thanks!