LoginSignup
19

More than 3 years have passed since last update.

UbuntuでCUDAを有効にしたOpenCVをビルド

Last updated at Posted at 2019-05-11

使用した環境その1

  • OS:Ubuntu18.04.3 LTS
  • CPU:Intel Corei7-7700HQ
  • GPU:NVIDIA Quadro M1200

使用した環境その2(2020/8/15追記)

  • OS:Ubuntu20.04.1 LTS
  • CPU:Intel Corei7-7700HQ
  • GPU:NVIDIA Quadro M1200

必要なものをインストール

GPUのドライバ関係(18.04/20.04共通)

sudo apt update && sudo apt upgrade
sudo ubuntu-drivers autoinstall
sudo reboot
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.168-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.1.168-1_amd64.deb
sudo apt update 

sudo apt install cuda cuda-drivers

.bashrcに以下を追記

export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

CUDNNのインストール(18.04/20.04共通)

この記事を参考にインストールからインストール

再起動する(18.04/20.04共通)

sudo reboot

パッケージ(18.04)

sudo apt install python3-dev python3-pip gcc-6 g++-6 cmake build-essential git pkg-config ccache \
unzip ecm extra-cmake-modules mesa-utils libgtk-3-dev ffmpeg \
libavcodec-dev libavformat-dev libswscale-dev libtbb-dev libogre-1.9-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 \
vtk7 tcl-vtk7 python3-vtk7 libgflags-dev autoconf automake libtool \
autoconf-archive libleptonica-dev libtesseract-dev gphoto2 liblapacke-dev 
pip3 install numpy

パッケージ(20.04)

sudo apt install python3-dev python3-pip gcc-7 g++-7 cmake build-essential git pkg-config ccache \
unzip extra-cmake-modules mesa-utils libgtk-3-dev ffmpeg \
libavcodec-dev libavformat-dev libswscale-dev libtbb-dev libogre-1.9-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 \
vtk7 tcl-vtk7 python3-vtk7 libgflags-dev autoconf automake libtool \
autoconf-archive libleptonica-dev libtesseract-dev gphoto2 liblapacke-dev 
pip3 install numpy

メイン

OpenCVのソースをダウンロード(18.04/20.04共通)

cd ~/
git clone --depth=1 https://github.com/opencv/opencv.git
git clone --depth=1 https://github.com/opencv/opencv_contrib.git
cd opencv && mkdir build && cd build

Cmakeする(18.04)

CC=gcc-6 CXX=g++-6 cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
 -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_opencv_apps=ON -D BUILD_opencv_calib3d=ON -D BUILD_opencv_core=ON \
 -D BUILD_opencv_cudaarithm=ON -D BUILD_opencv_cudabgsegm=ON -D BUILD_opencv_cudacodec=ON \
 -D BUILD_opencv_cudafeatures2d=ON -D BUILD_opencv_cudafilters=ON -D BUILD_opencv_cudaimgproc=ON \
 -D BUILD_opencv_cudalegacy=ON -D BUILD_opencv_cudaobjdetect=ON -D BUILD_opencv_cudaoptflow=ON \
 -D BUILD_opencv_cudastereo=ON -D BUILD_opencv_cudawarping=ON -D BUILD_opencv_cudev=ON \
 -D BUILD_opencv_features2d=ON -D BUILD_opencv_flann=ON -D BUILD_opencv_highgui=ON -D BUILD_opencv_imgcodecs=ON \
 -D BUILD_opencv_imgproc=ON -D BUILD_opencv_ml=ON -D BUILD_opencv_objdetect=ON \
 -D BUILD_opencv_photo=ON -D BUILD_opencv_stitching=ON -D BUILD_opencv_superres=ON \
 -D BUILD_opencv_ts=ON -D BUILD_opencv_video=ON -D BUILD_opencv_videoio=ON -D BUILD_opencv_videostab=ON \
 -D WITH_1394=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D WITH_CUFFT=ON -D WITH_EIGEN=ON -D WITH_FFMPEG=ON \
 -D WITH_GDAL=OFF -D WITH_GPHOTO2=ON -D WITH_GIGEAPI=ON -D WITH_GSTREAMER=ON -D WITH_GTK=ON \
 -D WITH_INTELPERC=OFF -D WITH_IPP=ON -D WITH_IPP_A=OFF -D WITH_JASPER=ON -D WITH_JPEG=ON -D WITH_LIBV4L=ON \
 -D WITH_OPENCL=ON -D WITH_OPENCLAMDBLAS=OFF -D WITH_OPENCLAMDFFT=OFF -D WITH_OPENCL_SVM=OFF \
 -D WITH_OPENEXR=ON -D WITH_OPENGL=ON -D WITH_OPENMP=OFF -D WITH_OPENNI=OFF -D WITH_PNG=ON \
 -D WITH_PTHREADS_PF=OFF -D WITH_PVAPI=ON -D WITH_QT=ON -D WITH_TBB=ON -D WITH_TIFF=ON \
 -D WITH_UNICAP=OFF -D WITH_V4L=ON -D WITH_VTK=ON -D WITH_WEBP=ON -D WITH_XIMEA=OFF -D WITH_XINE=OFF \
 -D CUDA_NVCC_FLAGS=--expt-relaxed-constexpr -D CUDA_FAST_MATH=ON -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
 -D CUDA_HOST_COMPILER=/usr/bin/gcc-6 -D PYTHON_DEFAULT_EXECUTABLE=python3  ..

Cmakeする(20.04)

CC=gcc-7 CXX=g++-7 cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
 -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_opencv_apps=ON -D BUILD_opencv_calib3d=ON -D BUILD_opencv_core=ON \
 -D BUILD_opencv_cudaarithm=ON -D BUILD_opencv_cudabgsegm=ON -D BUILD_opencv_cudacodec=ON \
 -D BUILD_opencv_cudafeatures2d=ON -D BUILD_opencv_cudafilters=ON -D BUILD_opencv_cudaimgproc=ON \
 -D BUILD_opencv_cudalegacy=ON -D BUILD_opencv_cudaobjdetect=ON -D BUILD_opencv_cudaoptflow=ON \
 -D BUILD_opencv_cudastereo=ON -D BUILD_opencv_cudawarping=ON -D BUILD_opencv_cudev=ON \
 -D BUILD_opencv_features2d=ON -D BUILD_opencv_flann=ON -D BUILD_opencv_highgui=ON -D BUILD_opencv_imgcodecs=ON \
 -D BUILD_opencv_imgproc=ON -D BUILD_opencv_ml=ON -D BUILD_opencv_objdetect=ON \
 -D BUILD_opencv_photo=ON -D BUILD_opencv_stitching=ON -D BUILD_opencv_superres=ON \
 -D BUILD_opencv_ts=ON -D BUILD_opencv_video=ON -D BUILD_opencv_videoio=ON -D BUILD_opencv_videostab=ON \
 -D WITH_1394=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D WITH_CUFFT=ON -D WITH_EIGEN=ON -D WITH_FFMPEG=ON \
 -D WITH_GDAL=OFF -D WITH_GPHOTO2=ON -D WITH_GIGEAPI=ON -D WITH_GSTREAMER=ON -D WITH_GTK=ON \
 -D WITH_INTELPERC=OFF -D WITH_IPP=ON -D WITH_IPP_A=OFF -D WITH_JASPER=ON -D WITH_JPEG=ON -D WITH_LIBV4L=ON \
 -D WITH_OPENCL=ON -D WITH_OPENCLAMDBLAS=OFF -D WITH_OPENCLAMDFFT=OFF -D WITH_OPENCL_SVM=OFF \
 -D WITH_OPENEXR=ON -D WITH_OPENGL=ON -D WITH_OPENMP=OFF -D WITH_OPENNI=OFF -D WITH_PNG=ON \
 -D WITH_PTHREADS_PF=OFF -D WITH_PVAPI=ON -D WITH_QT=ON -D WITH_TBB=ON -D WITH_TIFF=ON \
 -D WITH_UNICAP=OFF -D WITH_V4L=ON -D WITH_VTK=ON -D WITH_WEBP=ON -D WITH_XIMEA=OFF -D WITH_XINE=OFF \
 -D CUDA_NVCC_FLAGS=--expt-relaxed-constexpr -D CUDA_FAST_MATH=ON -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.0 \
 -D CUDA_HOST_COMPILER=/usr/bin/gcc-7 ..

makeする(CPUに依存するけどi7-7700HQで1時間くらいかかった)(18.04/20.04共通)

make -j$(nproc)

インストール(18.04/20.04共通)

sudo make install

インストールが成功しているか確認

以下を実行し0より大きい値が返ってくれば成功

python3
import cv2
print(cv2.cuda.getCudaEnabledDeviceCount())

もし失敗してたら(18.04/20.04共通)

cd ~/opencv/
rm -rf build

でbuildフォルダを消してからCmakeからやり直してください

参考

https://qiita.com/shinohe/items/d41f77edac27be9cef57
https://qiita.com/gusugusu1018/items/2115343a8adce378ff4a
https://qiita.com/nakamura21/items/2b2ff6524710d167d618
https://qiita.com/yukoba/items/4733e8602fa4acabcc35

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19