You are here
Home > python >

Building OpenCV with CUDA Support: A Step-by-Step Guide

Introduction

OpenCV is a powerful library for computer vision, but to achieve real-time performance, we need GPU acceleration using CUDA. This guide will walk you through building OpenCV with CUDA support, solving common errors, and ensuring OpenCV uses the GPU.

✅ You Will Learn:

  • How to build OpenCV with CUDA on Linux
  • Common errors and their fixes
  • How to verify GPU acceleration in OpenCV

🔹 Prerequisites

Before starting, ensure you have:

  • Ubuntu 22.04 or later
  • NVIDIA GPU (GTX 1050 or higher)
  • CUDA 11.8+ and cuDNN installed
  • CMake 3.16+

🔹 Step 1: Install Dependencies

1️⃣ Update Your System

sudo apt update && sudo apt upgrade -y

2️⃣ Install Required Libraries

sudo apt install -y build-essential cmake git unzip pkg-config \
libgtk-3-dev libcanberra-gtk3-module \
libavcodec-dev libavformat-dev libswscale-dev \
libv4l-dev libxvidcore-dev libx264-dev \
libjpeg-dev libpng-dev libtiff-dev libopenexr-dev \
gfortran libtbb2 libtbb-dev libdc1394-22-dev \
python3-dev python3-numpy

3️⃣ Verify CUDA Installation

nvcc --version
nvidia-smi

If CUDA is missing, install it from NVIDIA’s website.


🔹 Step 2: Check and Update CUDA Compatibility

Check Installed CUDA and cuDNN Versions

Run:

nvcc --version
nvidia-smi
cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

If CUDA and cuDNN versions mismatch (e.g., CUDA 11.5 but cuDNN for 11.8), update CUDA:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu$(lsb_release -rs | tr -d .)/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt update
sudo apt install -y cuda-11-8

Set the environment variables:

echo 'export PATH=/usr/local/cuda-11.8/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

🔹 Step 3: Download OpenCV and OpenCV Contrib

Clone OpenCV and its contrib modules:

cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv
git checkout 4.11.0  # Use the latest stable version
cd ../opencv_contrib
git checkout 4.11.0

🔹 Step 4: Configure OpenCV with CUDA

cd ~/opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=~/opencv_cuda_env \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D OPENCV_DNN_CUDA=OFF \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D CUDA_ARCH_BIN=6.1 \
-D CUDA_ARCH_PTX="" \
-D CMAKE_CXX_STANDARD=17 ..

🔹 Step 5: Compile and Install OpenCV

Compile using:

make -j$(nproc)

If you run into compiler errors, try:

export CC=/usr/bin/gcc-10
export CXX=/usr/bin/g++-10
make -j$(nproc)

Once compiled, install OpenCV:

sudo make install

🔹 Step 6: Verify OpenCV Installation

Check OpenCV Version

/home/onkar/opencv_cuda_env/bin/opencv_version

Check CUDA in OpenCV

import cv2
print(cv2.getBuildInformation())
print(cv2.cuda.getCudaEnabledDeviceCount())  # Should return >0

🔹 Common Errors & Fixes

1️⃣ CUDA Not Found in OpenCV

echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

2️⃣ g++: internal compiler error

sudo apt install gcc-10 g++-10
export CC=/usr/bin/gcc-10
export CXX=/usr/bin/g++-10

3️⃣ NVIDIA Codec SDK Not Found

wget https://developer.nvidia.com/nvidia-video-codec-sdk-12-1
tar -xvf Video_Codec_SDK*.tar.gz
cd Video_Codec_SDK*
sudo cp Samples/common/inc/* /usr/local/include/
sudo cp Lib/linux/stubs/x86_64/* /usr/local/lib/
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

🔹 Conclusion

🎉 Congratulations! You have successfully built OpenCV with CUDA support. Now you can process videos, detect objects, and apply filters at GPU speeds! 🚀

✅ Key Takeaways:

  • Use cv2.cuda functions for acceleration.
  • Always verify CUDA support using cv2.getBuildInformation() .
  • Fix common build errors by installing the right dependencies.

💬 Got stuck? Drop a comment, and I’ll help you debug! 🚀🔥

Top