Install OpenCV with CUDA Support on Ubuntu Easily

OpenCV is a widely-used computer vision library that provides a lot of functionalities for image and video processing, feature detection, and more. When working with large datasets or computationally intensive tasks, leveraging the power of NVIDIA's CUDA can significantly speed up processing times. In this article, we will walk through the steps to install OpenCV with CUDA support on Ubuntu easily.

Before diving into the installation process, ensure you have a compatible NVIDIA GPU and the necessary CUDA toolkit installed on your system. This guide assumes you are using Ubuntu 20.04 or a similar version.

Prerequisites for OpenCV with CUDA Installation

To install OpenCV with CUDA support, you will need:

  • An NVIDIA GPU with CUDA capability
  • Ubuntu 20.04 or similar
  • CUDA toolkit installed (version 11.0 or higher recommended)
  • cuDNN library (version 8.0 or higher)

Step 1: Update Your System

Start by updating your package list and upgrading existing packages:

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Packages

Install the necessary packages for OpenCV and CUDA:

sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python3-dev python3-numpy libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev

Step 3: Install CUDA and cuDNN

If you haven't installed CUDA and cuDNN yet, follow these steps. First, install CUDA:

sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
sudo apt update
sudo apt install -y cuda-11-4

Then, download and install cuDNN from the NVIDIA developer website:

wget https://developer.download.nvidia.com/compute/cuda/11.4/linux/cudnn-11.4-linux-x64-v8.2.4.15.tgz
tar -xvf cudnn-11.4-linux-x64-v8.2.4.15.tgz
sudo cp cudnn-11.4-linux-x64-v8.2.4.15/cuda/lib64/libcudnn* /usr/local/cuda-11.4/lib64/
sudo cp  cudnn-11.4-linux-x64-v8.2.4.15/cuda/include/cudnn*.h /usr/local/cuda-11.4/include/

Step 4: Download and Build OpenCV

Clone the OpenCV repository and build it with CUDA support:

git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.x
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.4 -DCUDNN_LIBRARY=/usr/local/cuda-11.4/lib64/libcudnn.so ..
make -j$(nproc)
sudo make install

Key Points

  • Ensure your NVIDIA GPU supports CUDA and you have the correct version installed.
  • cuDNN library version 8.0 or higher is required for OpenCV with CUDA.
  • Update your system and install necessary packages before building OpenCV.
  • Building OpenCV from source allows for customization and CUDA support.
  • Verify OpenCV installation with pkg-config --cflags opencv4 and python3 -c "import cv2; print(cv2.__version__)".

Verification

After installation, verify that OpenCV is installed correctly with CUDA support:

pkg-config --cflags opencv4
python3 -c "import cv2; print(cv2.__version__)"

Additionally, you can test CUDA support by running a sample OpenCV program that utilizes CUDA.

Troubleshooting

If you encounter issues during installation, ensure that:

  • Your CUDA and cuDNN versions are compatible with OpenCV.
  • You have correctly set the CUDA_TOOLKIT_ROOT_DIR and CUDNN_LIBRARY paths during CMake configuration.
  • Your NVIDIA drivers are up to date.

What are the main prerequisites for installing OpenCV with CUDA support on Ubuntu?

+

The main prerequisites include an NVIDIA GPU with CUDA capability, Ubuntu 20.04 or similar, CUDA toolkit (version 11.0 or higher), and cuDNN library (version 8.0 or higher).

How do I verify if OpenCV has been installed with CUDA support?

+

You can verify OpenCV installation with CUDA support by checking the OpenCV configuration with pkg-config --cflags opencv4 and testing OpenCV's Python interface with python3 -c "import cv2; print(cv2.__version__)". Additionally, you can run sample OpenCV programs that utilize CUDA.

What should I do if I encounter issues during the installation of OpenCV with CUDA?

+

If you encounter issues, ensure that your CUDA and cuDNN versions are compatible with OpenCV, verify the CUDA_TOOLKIT_ROOT_DIR and CUDNN_LIBRARY paths, and check that your NVIDIA drivers are up to date.

By following these steps, you should be able to install OpenCV with CUDA support on Ubuntu easily and efficiently. This setup will enable you to leverage the power of your NVIDIA GPU for computer vision tasks, significantly improving processing speeds for computationally intensive applications.