In this post, we’ll go through the process of installing OpenCV library 4.2.0 with the contrib package and the CUDA enabled on the latest Ubuntu desktop 19.10. The desktop setup is i9 9900KS and Nvidia 1080 Ti.
OpenCV (Open source computer vision) is a famous and useful library in computer vision programming tasks. It provides functional APIs for Python, C++, Java, etc. though it was initially written in C++. The contrib extension contains additional functions and modules that are not well tested and they’re all listed here. CUDA Toolkit (Compute Unified Device Architecture) provides a development environment for creating high-performance GPU-accelerated applications. With the CUDA Toolkit, you can develop, optimize and deploy your applications on GPU-accelerated embedded systems, desktop workstations, enterprise data centers, cloud-based platforms, and HPC supercomputers.
The OpenCV with CUDA enabled means that we can utilize GPU(s) for some of the OpenCV functionalities. However, they’re not combined while installing, we have to configure the packages and build from source in order to create this setup. Here are the steps to complete the tutorial:
Installing CUDA
We can download the latest “run” file from the CUDA official website or use the commands directly (The legacy releases can be found on another page linked there, for example, CUDA 10.0 could be found here):
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
sudo sh cuda_10.2.89_440.33.01_linux.run
A “run” file has all the components and dependencies included, hence we just need to run it to have CUDA installed. All variables and PATH should be set after installation.
Installing OpenCV dependencies
The tutorial is based on raulqf’s solution.
1. Update system and packages:
sudo apt update
2. Generic tools:
sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
3. Image I/O libs:
sudo apt install libjpeg-dev libpng-dev libtiff-dev
4. Video/Audio Libs – FFMPEG, GSTREAMER, x264 and so on:
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev
5. OpenCore – Adaptive Multi Rate Narrow Band (AMRNB) and Wide Band (AMRWB) speech codec:
sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev
6. Cameras programming interface libs:
sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd ~
7. GTK lib for the graphical user functionalites coming from OpenCV highghui module:
sudo apt-get install libgtk-3-dev
8. Python libraries for python3:
sudo apt-get install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
sudo apt install python3-testresources
9. Parallelism library C++ for CPU:
sudo apt-get install libtbb-dev
10. Optimization libraries for OpenCV:
sudo apt-get install libatlas-base-dev gfortran
11. Optional libraries:
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
In case you want to use a virtual environment of Python for installation:
1. Install virtualenv if you don’t have:
pip3 install virtualenv
2. Create a new virtual environment if you don’t have one:
mkdir environment_directory
cd environment_directory
virtualenv -p python3 environment_name
Substitute the directory name to your own customized one and use another Python interpreter if needed.
3. Activate the virtual environment:
source environment_name/bin/activate
Building OpenCV
Now we proceed to install the OpenCV library. First, let’s download the source:
cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
Next, we start building (You may change the installation path, contrib module path and python executable path respectively):
cd opencv-4.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv420.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/username/path/to/python/lib/python3.7/site-packages \
-D OPENCV_EXTRA_MODULES_PATH=/home/username/opencv_contrib-4.2.0/modules \
-D PYTHON_EXECUTABLE=/home/username/path/to/virtualenv/bin/python3 \
-D BUILD_EXAMPLES=ON ..
If you want also to use CUDNN you must include those flags (to set the correct value of CUDA_ARCH_BIN you must visit https://developer.nvidia.com/cuda-gpus and find the Compute Capability CC of your graphic card):
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 \
When I was installing I had another issue to enable CUDA while building. The correct summary printed on the screen:
-- NVIDIA CUDA: YES (ver 10.0, CUFFT CUBLAS NVCUVID FAST_MATH)
-- NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75
-- NVIDIA PTX archs:
However, I didn’t get this output in the summary because there are unsupported architectures of your graphic card (1080Ti) with the minimum requirements from OpenCV. So I have to manually change the configuration file CMakeList.txt
located in opencv > modules > dnn > CMakeList.txt
and set the minimum version to the one you have (Note that the correct functioning of this module will be compromised):
CUDA_ARCH_BIN:STRING=6.0 6.1 7.0 7.5
Now we can build again and we should get the correct summary output:
-- NVIDIA CUDA: YES (ver 10.0, CUFFT CUBLAS NVCUVID FAST_MATH)
-- NVIDIA GPU arch: 60 61 70 75
-- NVIDIA PTX archs:
Finally, we proceed with the compilation (use nproc
to know the number of CPU cores:
make -j16
sudo make install
And, Include the libs in your environment:
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
If you want to have available opencv python bindings in the system environment you should copy the created folder during the installation of OpenCV -D OPENCV_PYTHON3_INSTALL_PATH=/home/username/path/to/python/lib/python3.7/site-packages
into the dist-packages
folder of the target python interpreter:
sudo cp -r /home/username/path/to/python/lib/python3.7/site-packages/cv2 /path/to/target/python3.6/dist-packages
Change the configuration file in the new interpreter:
sudo nano /path/to/target/python3.6/dist-packages/cv2/config-3.6.py
```
PYTHON_EXTENSIONS_PATHS = [
os.path.join('/path/to/target/python3.6/dist-packages/cv2', 'python-3.6')
] + PYTHON_EXTENSIONS_PATHS
```
Now the OpenCV with CUDA should be installed on the target environment!