While testing out some OpenCV features related to their Deep Neural Network (DNN) featureset, we noticed that OpenCV supports a CUDA backend. However, the OpenCV libraries provided by Ubuntu 22.04 LTS were not compiled with CUDA support, so we decided to compile it ourselves and use it.
The process is quite simple but has several steps described in short below.
Install NVIDIA drivers for your system.
Install a supported CUDA for your NVIDIA GPUs on the system. We used CUDA 12.6
Install NVIDIA’s DNN library cuDNN on the system for CUDA 12.
Download OpenCV and its contributing repositories from Github and checkout the version 5.0.0
Build and install the OpenCV with CUDA support.
Installing Dependencies from NVIDIA
Download and install the correct NVIDIA Drivers, the CUDA 12.6 SDK and the latest cuDNN release for CUDA 12. The steps to download and install are out of scope of this blog post and depend on the type of Linux you are using.
For our purposes we will assume that the CUDA packages have been installed at /usr/local/cuda.
DownLOADING OPENCV
Download the opencv and opencv_contrib repositories from Github.
$ mkdir -p opencv_with_cuda
$ cd opencv_with_cuda
$ git clone https://github.com/opencv/opencv
$ git clone https://github.com/opencv/opencv_contrib
$ cd opencv_contrib
$ git checkout 5.0.0
$ cd ../opencv
$ git checkout 5.0.0DOWNLOAD PRE-REQUISITES
To build the OpenCV modules with a lot of dependencies, we need the following packages installed on Ubuntu or Debian based systems.
$ sudo apt -y install build-essential cmake ffmpeg libavcodec-dev \
libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev \
libjpeg-turbo8-dev libpng-dev libtiff-dev libopenexr-dev \
libgstreamer1.0-dev libgstreamer-plugins-good1.0-dev \
libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-dev \
libboost-all-dev libgtk-4-dev libgtk-3-dev libavif-dev zlib1g-dev \
libopenblas-dev gcc-12 g++-12
If you are on a system that does not have gcc-12 but has gcc-11 or gcc-13 you can use those versions. If your system has anything higher than gcc-13 and you are trying to use CUDA 12.6, you will either need to upgrade to CUDA 13 or install gcc-13 and g++-13 and then set those as your temporary default compilers as shown below.
BUILD OPENCV
Now we compile OpenCV that we have downloaded in the above steps. We are using gcc-12 here, but you can change to whatever your best fit gcc version is.
$ cd opencv_with_cuda
$ cd opencv
$ mkdir build
$ cd build
$ export NVCC_APPEND_FLAGS="-allow-unsupported-compiler"
$ export CC=$(which gcc-12)
$ export CXX=$(which g++-12)
$ cmake -DOPENCV_EXTRA_MODULES_PATH=$(readlink -f ../../opencv_contrib/modules) \
-DWITH_CUDA=ON -DWITH_CUDNN=ON -DWITH_NVCUVID=OFF -DWITH_NVCUVENC=OFF \
-DCUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
-DCUDNN_INCLUDE_DIR=/usr/local/cuda/include -DCUDA_ARCH_BIN=7.5 \
-DOPENCV_DNN_CUDA=ON -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$HOME/.local/ -DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_JAVA=OFF ..
$ make -j$(nproc)
### the tests can take very long to run, maybe even 12 hours
$ make test
$ make installAs you can see above we are installing this newly compiled OpenCV locally at $HOME/.local . You can choose to install it globally in /usr/local and then you will need to run sudo make install .
You will also need to set your LD_LIBRARY_PATH variable to point to the installed folder.
$ export LD_LIBRARY_PATH=$HOME/.local/lib:$LD_LIBRARY_PATHCOMPILING CODE WITH CMAKE
Now let’s try to use this newly installed OpenCV with CMake and a test program.
Below is the content of a C++ program that tests the OpenCV version.
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int argc, char **argv)
{
std::cout << "OpenCV Build Information: " << cv::getBuildInformation()
<< std::endl;
return 0;
}
Below is the CMakeLists.txt file that you would need to compile the above C++ file, which we shall call build_info.cpp.
cmake_minimum_required(VERSION 3.5)
project(opencv_testing)
include(FindPkgConfig)
## set the OPENCV_ROOT to $HOME/.local if using a custom install
if ($ENV{OPENCV_ROOT})
find_package(OpenCV REQUIRED PATHS $ENV{OPENCV_ROOT})
else ($ENV{OPENCV_ROOT})
## find a system-wide installed OpenCV
find_package(OpenCV REQUIRED)
endif ($ENV{OPENCV_ROOT})
if (OpenCV_VERSION)
include_directories(${OpenCV_INCLUDE_DIRS})
endif (OpenCV_VERSION)
add_executable(opencv_buildinfo build_info.cpp)
target_link_libraries(opencv_buildinfo ${OpenCV_LIBS})Now we compile and run this code.
$ export OPENCV_ROOT=$HOME/.local
$ ls
build_info.cpp
$ mkdir _build
$ cd _build
$ cmake .. && make
$ ./opencv_buildinfo
....
NVIDIA CUDA: YES (ver 12.4, CUFFT CUBLAS)
NVIDIA GPU arch: 75
NVIDIA PTX archs:
cuDNN: YES (ver 9.23.2)
....
You will see a lot of details but the main items to look for are that your OpenCV correctly supports CUDA and cuDNN, which was the goal.
Hope this helps you setup your own OpenCV for using deep neural networks and CUDA GPUs.

