Since pre-built OpenCV binaries do not include CUDA modules, this post is a tutorial for building OpenCV with CUDA on Windows 11. The obvious dvantage of OpenCV CUDA is boosting performance of most functions, you can find evidence here.

1. Prerequisite

There are a couples of softwares or libraries having been downloaded and installed before getting started:

  1. Install the Visual Studio Community 2022 and select Desktop development with C++ workload.

  2. Download the sources for OpenCV from GitHub by cloning the repositories (opencv and opencv_contrib).

OpenCV CUDA folder

After downloading, you can indicate the OpenCV’s version you want to. For the current version, you can run the following command in Command Prompt at OpenCV’s repositories:

git checkout tags/4.8.0
  1. Install the latest stable version (not release candidate -rc) of CMake.

  2. Install the latest version of NVIDIA CUDA Toolkit and add PATH. You can follow this tutorial.

The latest CUDA Toolkit version

At the time writting this post, the latest NVIDIA CUDA Toolkit version 12.2 still makes somes error when building with OpenCV 4.8.0, like error C2666: 'operator !=': overloaded functions have similar conversions. Therefore, if meeting such problem, try installing older version.

  1. Register an account and download the latest verson of NVIDIA DNN CUDA backend for the version of CUDA. Extract the downloaded .zip file and copy bin, include and lib directories to your CUDA installation directory, i.e., C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vxx.x.

  2. Register an account and download the latest version of NVIDIA VIdeo Codec SDK. Extract the downloaded .zip file and copy the contents inside Interface and Lib to include and lib directories inside the CUDA installation directory.

  3. Optional - Download and install the latest version of Gstreamer.

  4. Download and install the lastest version of mambaforge to call OpenCV CUDA routines from python.

2. Create virtual environtment from Mambaforge

To bind OpenCV for python3 without any conflit package installation, you should create an new virtual environment for the installation.

mamba create -n opencv-cuda python=3.10

Activate the created environment and install numpy package.

mamba activate opencv-cuda
mamaba install numpy

3. Build OpenCV with CMake

Preparation

Create a build folder with your OpenCV extracted folders.

OpenCV CUDA folder

Edit the prioeiry of Python3 installation in OpenCVDetectPython.cmake file inside opencv-x.x.x/cmake folder.

Python3 Prior

Build GUI Build Configuration

Open Cmake GUI and provide the paths to the OpenCV and target build folders.

CMake GUI

Hit Configure and select x64 for the Optional platform for generator, then hit finish to start the configuration.

Once the configuration is done, edit the following parameters:

ParameterValue
CMAKE_INSTALL_PREFIXpath of opencv installation
ENABLE_FAST_MATH
WITH_CUDA
BUILD_opencv_world
BUILD_opencv_python3
OPENCV_DNN_CUDA
OPENCV_EXTRA_MODULES_PATHpath of modules directory in opencv_contrib-x.x.x
OPENCV_PYTHON3_VERSION
PYTHON3_EXECUTABLEpath of python3 executable in virtual env, i.e., C:/Users/ntthi/mambaforge/envs/opencv-cuda/python.exe
PYTHON3_INCLUDE_DIRpath of include folder in the virtual env, i.e., C:/Users/ntthi/mambaforge/envs/opencv-cuda/include
PYTHON3_LIBRARYpath of .lib file in the virtual env, i.e., C:/Users/ntthi/mambaforge/envs/opencv-cuda/libs/python310.lib
PYTHON3_NUMPY_INCLUDE_DIRSpath of numpy in the virtual env, i.e., C:/Users/ntthi/mambaforge/envs/opencv-cuda/Lib/site-pakages/numpy/core/include
PYTHON3_PACKAGES_PATHpath of site-packages in the virtual env, i.e., C:/Users/ntthi/mambaforge/envs/opencv-cuda/Lib/site-pakages

Note that the path separator hase to be “/” , not “".

Hit Configure again again and check edit more parameters:

ParameterValue
CUDA_FAST_MATH
CUDA_ARCH_BINversion of computing capability, i.e., 8.6
WITH_CUBLAS
WITH_CUDNN
WITH_CUFFT

The CUDA_ARCH_BIN corresponding to your GPU is the value found in the left column of the GPU support table. For instance, “8.6” fir the RTX 3070 Ti.

If you do not want to create shared lib and make sure the opencv python libraries is installed, edit the following parameters:

ParameterValue
BUILD_SHARED_LIBS🔳
OPENCV_FORCE_PYTHON_LIBS

Hit Configure at the last time and then hit Generate.

Build the project with Visual Studio

Open project OpenCV.sln created in the build folder. Go to Tools > Options…, then uncheck the last parameter in Projects and Solutions > Web Projects.

This setting may help to prevent the ImportError: DLL load failed while importing cv2: The specified module could not be found. error.

To build the OpenCV project, change Debug mode to Release. In the solution explorer expand CMakeTargets, right-click ALL_BUILD and select Build. This will take about an hour.

OpenCV build

Then repeat the step for INSTALL (below ALL_BUILD). Check for error in the two building steps. If everything is fine, you are done.

Check Installation and Troubleshooting

To verify the Python installation, activate the virtual environment for OpenCV install and try this code:

import cv2
print(cv2.__version__)
print(cv2.cuda.getCudaEnabledDeviceCount())

If it works, congratulations you are good to go!

If you meets the problem ImportError: DLL load failed while importing cv2: The specified module could not be found., it may lack the library’s binaries. One solution is to edit config.py in C:/Users/ntthi/mambaforge/envs/opencv-cuda/Lib/site-packages/cv2.

import os

BINARIES_PATHS = [
    os.path.join('C:/opencv-cuda-4.8.0', 'x64/vc17/bin'),
    os.path.join(os.getenv('CUDA_PATH', 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.1'), 'bin'),
    os.path.join('C:/gstreamer/1.0/msvc_x86_64', 'bin'),
] + BINARIES_PATHS

These binary paths are from installed OpenCV, CUDA and Gstreamer (if installed).

For other bugs and problems, I refer you to the chrismeunier and Bowley’s troubleshooting tutorial.

Reference