##必要なパッケージをインストール
sudo apt install gcc-6 g++-6 cmake build-essential git pkg-config ccache \
unzip ecm extra-cmake-modules fcitx-libs-dev libfcitx-qt5-1 \
mesa-utils libgtk2.0-dev libgtk-3-dev ffmpeg \
libavcodec-dev libavformat-dev libswscale-dev \
libjpeg-dev libpng-dev libtiff5-dev x264 libxvidcore-dev yasm \
libxine2-dev libv4l-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev \
libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev \
x264 v4l-utils ffmpeg libdc1394-22 libdc1394-22-dev libtiff5-dev \
qt5-default libeigen3-dev libeigen3-doc tesseract-ocr tesseract-ocr-jpn \
vtk6 tcl-vtk6 python-vtk6 libgflags-dev autoconf automake libtool \
autoconf-archive libleptonica-dev libtesseract-dev gphoto2 liblapacke-dev \
libgoogle-glog-dev libprotobuf-dev libprotoc-dev protobuf-compiler \
libgphoto2-dev libvtk6-dev libvtk6-qt-dev liblapack-dev libatlas-base-dev
##OpenCVダウンロード
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv
mkdir build
cd build
##ビルド準備
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON ..
##ビルド
make -j $(nproc)
##インストール
sudo make install
sudo ldconfig
##インストール確認
Python起動
python
import cv2
cv2.cuda.getCudaEnabledDeviceCount()
0より大きければ成功
##参考
https://qiita.com/Fal318/items/ef1bfe6e69622a563615