First of all install update and upgrade your system:
sudo apt-get update
sudo apt-get upgradeThen, install required libraries:
Developers tools:
sudo apt-get install build-essential cmake pkg-config unzip yasm git gfortranmage I/O libs
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-devVideo Libs - FFMPEG, GSTREAMER, x264 and so on.
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-devCameras programming interface libs
sudo apt-get install libdc1394-22 libdc1394-22-dev libxine-dev libv4l-dev v4l-utilsGTK lib for the graphical user functionalites coming from OpenCV highghui module
sudo apt-get install libgtk-3-dev libgtk2.0-devPython libraries for python2 and python3:
sudo apt-get install python-dev python-pip python3-dev python3-pip
$ sudo -H pip2 install -U pip numpy
$ sudo -H pip3 install -U pip numpyParallelism library C++ for CPU
sudo apt-get install libtbb-devOptimization libraries for OpenCV
sudo apt-get install libatlas-base-dev gfortranOptional 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 doxygenDownload porject
git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 3.4.2
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4.2
cd ..
cd opencv
mkdir build
cd buildcmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_QT=OFF ..with python3.6 lib (I also set CUDA_ARCH_BIN="7.5" because I use RTX2060, u can find yours at https://developer.nvidia.com/cuda-gpus)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_C_EXAMPLES=OFF \
  -D INSTALL_PYTHON_EXAMPLES=OFF \
  -D BUILD_EXAMPLES=OFF \
  -D BUILD_opencv_java=OFF \
  -D BUILD_opencv_xfeatures2d=OFF \
  -D BUILD_opencv_python3=ON \
  -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES --expt-relaxed-constexpr" \
  -D CUDA_ARCH_BIN="7.5" \
  -D WITH_CUDA=ON \
  -D ENABLE_FAST_MATH=1 \
  -D CUDA_FAST_MATH=1 \
  -D WITH_CUBLAS=1 \
  -D WITH_EIGEN=ON \
  -D BUILD_opencv_sfm=OFF \
  -D WITH_QT=ON \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
  -D PYTHON3_EXECUTABLE=$(which python3.6) \
  -D PYTHON_DEFAULT_EXECUTABLE=$(which python3.6) \
  -D PYTHON3_INCLUDE_DIR=$(python3.6 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
  -D PYTHON3_INCLUDE_DIR2=$(python3.6 -c "from os.path import dirname; from distutils.sysconfig import get_config_h_filename; print(dirname(get_config_h_filename()))") \
  -D PYTHON3_LIBRARY=$(python3.6 -c "from distutils.sysconfig import get_config_var;from os.path import dirname,join ; print(join(dirname(get_config_var('LIBPC')),get_config_var('LDLIBRARY')))") \
  -D PYTHON3_NUMPY_INCLUDE_DIRS=$(python3.6 -c "import numpy; print(numpy.get_include())") \
  -D PYTHON3_PACKAGES_PATH=$(python3.6 -c "import site; print(site.getsitepackages()[0])") \
  ..(In case you do not want to include include CUDA:)
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=OFF -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D WITH_QT=OFF -D BUILD_SHARED_LIBS=OFF ..Before the compilation you must check that CUDA has been enabled in the configuration summary printed on the screen.
--   NVIDIA CUDA
--     Use CUFFT:                   YES
--     Use CUBLAS:                  YES
--     USE NVCUVID:                 NO
--     NVIDIA GPU arch:             20 30 35 37 50 52 60 61
--     NVIDIA PTX archs:
--     Use fast math:               YESIf it is fine proceed with the compilation (Use nproc to know the number of cpu cores):
nproc
make -j`nproc`
sudo make installInclude the libs in your environment
sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig