Building OpenCV with CUDA Support: A Step-by-Step Guide python by Onkar Chaudhari - February 19, 2025February 19, 20250 Contents 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! ๐๐ฅ Share this: Share on X (Opens in new window) X Share on Facebook (Opens in new window) Facebook More Share on LinkedIn (Opens in new window) LinkedIn Share on WhatsApp (Opens in new window) WhatsApp Email a link to a friend (Opens in new window) Email Like this:Like Loading... Related