EN ·
🌏 中文

Compiling OpenCV with CUDA Support using CMake and VS2019 on Windows 10

OpenCV is one of the most widely used libraries for C++ based computer vision development. I have been using OpenCV since version 2.4, up to the current release 4.5.2.

OpenCV is feature-rich, covering most classic algorithms in traditional image processing. Its core matrix type, Mat, is simple yet powerful. Recent versions have introduced the DNN (Deep Neural Network) module to support inference for deep learning models. Recently, I decided to use the DNN module for algorithm deployment. Since I required GPU acceleration—which the standard official pre-built packages do not provide—I decided to compile OpenCV from source to create a custom build that meets my specific needs. For daily development, mastering this process is a worthwhile long-term investment.

Due to project requirements, this guide is based on Windows 10. While compiling on Linux is often more straightforward, Visual Studio’s support for CMake on Windows has become increasingly robust.

Preparation

I created a folder named opencv-github in my work directory and cloned the latest opencv and opencv_contrib source code from GitHub. The opencv_contrib repository contains experimental modules and “non-free” components.

git clone https://github.com/opencv/opencv.git

image

git clone https://github.com/opencv/opencv_contrib.git

image

CMake Configuration

If you have Visual Studio 2019 installed, you can usually launch cmake-gui directly from the command prompt. Once open, configure the source and build paths. My setup is as follows:

image

Click Configure, and select Visual Studio 16 2019 with the x64 platform.

image

image

During the first configuration, the ippicv dependency might fail to download. Clicking Configure again usually resolves this.

image

Customizing Build Options

  1. Add Contrib Modules: Search for the OPENCV_EXTRA_MODULES_PATH option and enter the path to the modules folder within opencv_contrib.

    D:/WORK/opencv-github/opencv_contrib/modules

    image

  2. Enable CUDA Support: Search for “cuda” and check OPENCV_DNN_CUDA and WITH_CUDA. Do not check BUILD_CUDA_STUBS. Note: Ensure CUDA and cuDNN are pre-installed and their versions are compatible. Click Configure again. image

  3. Build OpenCV World: Check the BUILD_opencv_world option. This combines all modules into a single library file, making it much easier to link in your projects, though it results in a larger binary. image

  4. Enable Non-free Algorithms: Check OPENCV_ENABLE_NONFREE. This includes algorithms that are patented and not free for commercial use, which is useful for research and experimentation. SIFT was once the most famous non-free algorithm, though it has since become free to use. image

Generation and Compilation

Click Generate.

image

Once generation is complete, click Open Project to launch Visual Studio 2019.

image

In the Solution Explorer, expand CMakeTargets and locate ALL_BUILD.

image

image

Right-click and select Build (or press F7). After ALL_BUILD completes, locate the INSTALL project and build it as well.

The resulting headers, .lib files, .dll files, and CMake configuration files will be located in the install folder within your build directory. This process is the Windows equivalent of running make and make install on Linux.

You now have a custom OpenCV build ready for development!