Summary
Ubuntu 16.04 has packages for OpenCV v2.4, while Ubuntu 18.10 has packages for OpenCV v3.2. The OpenCV project released v4.1.0 in April 2019. I couldn't find a PPA with anything newer than v3.4, which is too old for what I needed. So here are my build steps for getting OpenCV v4.1.0 built on Ubuntu 16.04 and Ubuntu 18.10.
Dependencies
I was already using VMs for development purposes, so it is possible this list is missing a few packages I manually added since I originally installed Ubuntu. As far as I can tell this is a good starting point for what is needed to build OpenCV v4.1.0 from source.
First step is to remove any older versions of OpenCV:
sudo apt-get purge libopencv-\* opencv-\*
Next, we install all of the necessary dependencies. There is a minor package name difference between the two versions of Ubuntu.
Ubuntu 16.04:
sudo apt-get install \
build-essential cmake doxygen graphviz libgtk2.0-dev libcanberra-gtk-module \
libtesseract-dev libleptonica-dev libopenblas-dev libatlas-dev \
liblapack-dev liblapacke-dev libhdf5-dev libeigen3-dev libdc1394-22-dev \
libavcodec-dev libavformat-dev gstreamer1.0-libav gstreamer1.0-plugins-bad \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libswscale-dev libtbb2 libjpeg-dev libpng-dev libtiff-dev libwebp-dev \
python-dev python3-dev python-numpy python3-numpy python-bs4 python3-bs4
Ubuntu 18.10:
sudo apt-get install \
build-essential cmake doxygen graphviz libgtk2.0-dev libcanberra-gtk-module \
libtesseract-dev libleptonica-dev libopenblas-dev libatlas-base-dev \
liblapack-dev liblapacke-dev libhdf5-dev libeigen3-dev libdc1394-22-dev \
libavcodec-dev libavformat-dev gstreamer1.0-libav gstreamer1.0-plugins-bad \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libswscale-dev libtbb2 libjpeg-dev libpng-dev libtiff-dev libwebp-dev \
python-dev python3-dev python-numpy python3-numpy python-bs4 python3-bs4
If you want to include CUDA support and plan on configuring OpenCV using WITH_CUDA=ON, then you'll need the following additional packages:
sudo apt-get install nvidia-cuda-dev nvidia-cuda-toolkit
At the time I'm writing this post (May 2019), CUDA support requires the use of GCC v6. There is an additional step described further below to force OpenCV to be compiled with GCC v6.
OpenCV Source Code
Next, we download and extract the OpenCV v4.1.0 source code, which comes in 2 parts:
mkdir -p ~/src
cd ~/src/
wget --continue --show-progress https://github.com/opencv/opencv/archive/4.1.0.zip --output-document=opencv-4.1.0.zip
wget --continue --show-progress https://github.com/opencv/opencv_contrib/archive/4.1.0.zip --output-document=opencv_contrib-4.1.0.zip
unzip opencv-4.1.0.zip
unzip opencv_contrib-4.1.0.zip
cd opencv-4.1.0
mkdir build
cd build
There are many optional parts to OpenCV which may be individually turned on or off during the build process. I won't try to list them all, but I'll include a few keys ones:
cmake \
-DCPACK_BINARY_DEB=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
-DEXTRA_MODULES_VCSVERSION=4.1.0 \
-DOPENCV_VCSVERSION=4.1.0 \
-DWITH_TBB=ON \
-DWITH_IPP=ON \
-DWITH_CUDA=OFF \
-DWITH_OPENCL=ON \
-DWITH_LIBV4L=ON \
-DWITH_OPENMP=ON \
-DWITH_NVCUVID=OFF \
-DWITH_CSTRIPES=ON \
-DWITH_GSTREAMER=ON \
-DBUILD_opencv_dnn=ON \
-DBUILD_opencv_python2=ON \
-DBUILD_opencv_python3=ON \
-DBUILD_PERF_TESTS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_DOCS=ON \
..
If you want support for CUDA (NVIDIA GPU) then you'll need to change -DWITH_CUDA= and -DWITH_NVCUVID= from OFF to ON and include some additional command-line parameters to cmake. This will force cmake to use GCC v6, which is a requirement when enabling support for CUDA:
cmake \
-DWITH_CUDA=ON \
-DWITH_NVCUVID=ON \
-DCMAKE_C_COMPILER=/usr/bin/gcc-6 \
-DCMAKE_CXX_COMPILER=/usr/bin/g++-6 \
...etc... (see above)
If you attempt to enable support for CUDA without forcing the compiler to be v6, you'll see errors such as this:
In file included from /usr/include/host_config.h:50,
from /usr/include/cuda_runtime.h:78,
from :
/usr/include/crt/host_config.h:121:2: error: #error -- unsupported GNU version! gcc versions later than 6 are not supported!
#error -- unsupported GNU version! gcc versions later than 6 are not supported!
^~~~~
The meaning behind some of the OpenCV configuration flags:
- TBB: Threading Building Blocks: C++ template library for task parallelism.
- IPP: Integrated Performance Primitives: highly optimized Intel CPU instructions.
- CUDA: NVIDIA GPU for parallel processing.
- OPENCL: Cross-platform framework.
- OPENMP: Multitasking API.
- NVCUVID: NVIDIA video decoder.
- CSTRIPES: Parallel C/C++ language extension.
- GSTREAMER: Cross-platform multimedia framework.
Building OpenCV
Next, we build OpenCV, temporarily install it, create the various packages for it, and remove the temporary installation. You can stop after sudo make install but in my case not only do I prefer the .deb files, I also needed them to install OpenCV v4.1.0 on multiple computers:
make -j16
sudo make install
cpack -G DEB
sudo make uninstall
You should now have a series of .deb files to install:
ls -lh *.deb
sudo dpkg -i OpenCV-4.1.0-x86_64-*.deb