Dependencies:

sudo apt install cmake python-dev python-numpy gcc g++
sudo apt install python3-dev python3-numpy
sudo apt install build-essential libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

Install:

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

cd opencv_contrib/
git checkout 3.4.4
cd ../opencv/
git checkout 3.4.4

mkdir build
cd build

cmake \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ \
  -D OPENCV_ENABLE_NONFREE=True \
  -D CMAKE_BUILD_TYPE=Release \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D BUILD_opencv_java=OFF \
  -D WITH_CUDA=OFF \
  -D PYTHON3_EXECUTABLE=$(which python3) \
  -D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
  -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
  ..