OpenCV Installation

$ sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y

The required packages are build-essential, cmake, git, python, libgtk2.0-dev, pkg-config, libavcodec-dev, libavformat-dev, and libswscale-dev.

The others are optional.

Python

$ sudo apt install -y python-dev python3-dev python3-pip &&\
  python3 -m pip install -U pip &&\
  python3 -m pip install --user numpy

GCC, CMake, git

$ sudo apt install -y build-essential cmake git

GUI, GL

$ sudo apt install -y libgtk2.0-dev libgtk-3-dev libqt4-dev &&\
  sudo apt install -y mesa-utils libgl1-mesa-dri libqt4-opengl-dev

config

$ sudo apt install -y pkg-config

FFmpeg libraries

$ sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev

parallelism library

$ sudo apt install -y libtbb2 libtbb-dev

Image codec

$ sudo apt install -y libjasper-dev ;\
  sudo apt install -y libjpeg-dev libtiff5-dev &&\
  sudo apt install -y libpng12-dev || sudo apt install -y libpng-dev

When the installation of libjasper-dev fails, download source from https://github.com/mdadams/jasper and build according to the following command.

If you skip installing libjasper-dev, it will be built during building OpenCV.

$ sudo apt install -y freeglut3-dev &&\
  git clone https://github.com/mdadams/jasper.git &&\
  cd jasper/build &&\
  cmake .. &&\
  sudo make install

Video codec

$ sudo apt install -y libxvidcore-dev &&\
  sudo apt install -y libx264-dev &&\
  sudo apt install -y libxine2-dev &&\
  sudo apt install -y libv4l-dev v4l-utils

Streming

$ sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

IEEE 1394 digital camera

$ sudo apt install -y libdc1394-22-dev

Mathmatic

$ sudo apt install -y gfortran libatlas-base-dev libeigen3-dev libhdf5-serial-dev

Getting OpenCV Source Code

$ cd &&\
  git clone https://github.com/opencv/opencv.git &&\
  git clone https://github.com/opencv/opencv_contrib.git

The opencv_contrib is optional. If you download the source using the above commands, it will be the latest version. If you want to download a previous version, see below.

First, clone OpenCV as above.

Second, check branches or tags.

$ git branch -a
 
$ git tag -l --sort=-v:refname

Third, select branch or tag about a version you want.

Fourth, checkout the version you selected.

$ cd ~/opencv &&\
  git checkout tags/4.1.0
 
$ cd ~/opencv_contrib &&\
  git checkout tags/4.1.0

CMake

If you want to optimize OpenCV using OpenCL, clink this link before cmake.
$ cd ~/opencv &&\
  mkdir build &&\
  cd build &&
  cmake ..\
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DOPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules \
    -DINSTALL_PYTHON_EXAMPLES=OFF \
    -DINSTALL_C_EXAMPLES=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_NEW_PYTHON_SUPPORT=ON \
    -DPYTHON3_EXECUTABLE=$(which python3) \
    -DOPENCV_GENERATE_PKGCONFIG=ON

Checking memory size and swapon

Make sure the memory size(Mem + Swap) is at least 3 GB before building.

$ free -ht
              total        used        free      shared  buff/cache   available
Mem:           1.8G        635M        219M         29M        1.0G        1.2G
Swap:            0B          0B          0B
Total:         1.8G        635M        219M

If you don't have enough memory like above, increase the swap. If you want to use -j[N-jobs] option with make, need more than 3 GB memory size.

Set as many sizes of swapfile as it needs to compile. And register it as swap.

$ sudo fallocate -l 1.2G /swapfile
$ sudo chmod 600 /swapfile &&\
  sudo mkswap /swapfile &&\
  sudo swapon /swapfile

build

$ make

If memory and disk size are enough, run make -j $(expr $(expr $(nproc) \* 6) \/ 5) instead of make for quick compilation.

Core Disk RAM Swap Command
4 core(C1+) 16 GB 0.8 GB 2.4 GB make -j4
4 core(C2) 16 GB 1.7 GB 1.6 GB make -j4
6 core(N2) 16 GB 1.8 GB 1.0 GB make
6 core(N2) 16 GB 1.8 GB 1.5 GB make -j7
6 core(N2) 16 GB 3.6 GB 0 GB make -j7
8 core(XU4) 16 GB 1.9 GB 1.4 GB make -j9

After compilation is complete, release the swap and delete the swapfile.

$ sudo swapoff /swapfile &&\
  sudo rm /swapfile

To install libraries, execute the following command from 'build' directory

$ sudo make install &&\
  sudo ldconfig
$ g++ [input.cpp] -o [output] $(pkg-config opencv4 --libs --cflags)

virtual environment

Create a virtual environment.

$ python3 -m venv [venv_path]

Check user's cv2 package path.

$ sudo find / -name cv2 -type d
/usr/local/lib/python3.6/dist-packages/cv2

Copy python cv2 package and paste it in the virtual environment package path.

$ sudo cp -r /usr/local/lib/python3.6/dist-packages/cv2 [venv_path]/lib/python3.6/site-packages/

Activate the virtual environment.

$ source [venv_path]/bin/activate

When using OpenCV-python, numpy and matplotlib are used together. (numpy is required, matplotlib is optional.)

$ python3 -m pip install -U pip &&\
  python3 -m pip install numpy matplotlib

To test if OpenCV module is imported properly, execute the command below.

$ python3 -c "import cv2 as cv"