Install OpenCV
To enable GPU support for OpenCV's DNN module, OpenCV must be built from source with CUDA support.
Clone the Repositories
Clone both OpenCV and the extra modules (opencv_contrib):
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Build from Source
cd opencv
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DCUDA_ARCH_BIN="7.5;8.9;12.0" \
-DENABLE_CUDA_FIRST_CLASS_LANGUAGE=ON \
-DOPENCV_DNN_CUDA=ON \
-DWITH_CUBLAS=1 \
-DWITH_CUDA=ON \
-DWITH_CUDNN=ON \
-DCUDA_FAST_MATH=1 \
-DENABLE_FAST_MATH=1 \
-DBUILD_opencv_python3=ON \
-DPYTHON3_EXECUTABLE=$(which python3) \
-DWITH_TBB=ON \
-DWITH_GSTREAMER=ON \
-DWITH_FFMPEG=ON \
-DWITH_OPENMP=ON \
-DBUILD_opencv_sfm=ON \
-DBUILD_opencv_cudacodec=ON \
-DBUILD_EXAMPLES=ON \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DINSTALL_C_EXAMPLES=ON \
-DOPENCV_ENABLE_NONFREE=ON \
-DOPENCV_EXTRA_MODULES_PATH=/home/yi-chen/thirdparty/opencv_contrib/modules \
..
info
CUDA_ARCH_BIN depends on your NVIDIA GPU's compute capability. Check the CUDA GPU list on Wikipedia for the correct value for your card. For example:
- RTX 3090 / A100 →
8.6/8.0 - RTX 4090 →
8.9 - RTX 5090 →
12.0
You can turn off flags unrelated to CUDA, but the CUDA-related flags must be enabled and CUDA_ARCH_BIN must be specified.
Then build and install:
cmake --build . -j$(nproc)
sudo cmake --install .
sudo ldconfig