Install OpenCV 4 on Ubuntu 18.04

In this tutorial you will learn how to install OpenCV 4 on Ubuntu 18.04. The OpenCV4 is more further optimizations, C++11 support, more compact modules and many improvements to the Deep Neural Network(DNN) module.

1. Install OpenCV4 dependencies on Ubuntu:

First thing you need to do is openning your terminal. Alternatively, you may also SSH into your vps.

1
2
3
4
5
# Update your system and clean it.
sudo apt-get update
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get upgrade
1
2
# Install the developer tools:
sudo apt-get install -y build-essential cmake unzip pkg-config

Next, Install a handful of image and video I/O libraries.

1
2
3
4
# These libraries enable us to load image from disk as well as read video file.
sudo apt-get install -y libjepg-dev libpng-dev libtiff-dev
sudo apt-get install -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get -y install libxvidcore-dev libx264-dev

Install GTK for our GUI backend.

1
sudo apt-get install libgtk-3-dev

Followed by installing two packages which contain mathematical optimizations for OpenCV:

1
sudo apt-get install libatlas-base-dev gfortran python3-dev

2. Download OpenCV4:

Download both OpenCV and OpenCV_contrib. You should be installing the OpenCV library with the additional contib modules as well.

1
2
3
4
5
# OpenCV
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.1.zip

# OpenCV Contrib
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.1.zip

From there, let’s unzip the archives:

1
2
3
4
5
unzip opencv.zip
unzip opencv_contrib.zip

mv opencv-4.1.1 opencv
mv opencv_contrib-4.1.1 opencv_contrib

3. Configure Python3 for OpenCV 4:

Download the latest pip:

1
2
3
4
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

sudo pip3 install numpy

4. CMake and compile OpenCV 4 for Ubuntu:

Now let’s run CMake to configure the OpenCV 4 build:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cd opencv # the opencv path.
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ # The opencv_contrib path
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN="5.3" \ # See more details(https://developer.nvidia.com/cuda-gpus)
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D BUILD_opencv_python3=ON ..

Now ready to compile OpenCV 4:

1
make -j8

Note: In the make command above, the -j8 argument specifies that I have 8 cores cpu for compilation. You should update the command to use the number of cores on your environment for faster compile.

And from there, let’s install OpenCV :

1
2
sudo make install 
sudo ldconfig

5. Test your OpenCV install on Ubuntu:

1
2
3
4
5
6
python

>>> import cv2
>>> cv2.__version__
'4.1.1'
>>>quit()
Mikoy Chinese wechat
Subscribe my public wechat account by scanning.

--------------------   ENDING   --------------------