openCV3.3.0 を Ubuntu 16.04.3 LTS にインストールするよ

せっかくなので可能な限り Not found の文字を叩き潰しに行く方針に切り替えてみた

 

必要なパッケージはインストール済としているよ

ここの記事とか見とくといい

shibafu3.hatenablog.com

openCVのソースをダウンロードするよ

形式は .zip なり tar.gzなりお好みで (普通は後者)

github.com

 

ダウンロードしたファイルを unzip コマンドだったり、

tarコマンドと、雰囲気で入力するオプションことxvzfとで解凍してあげましょう

 

opencv_contrib というものもあるのでそちらもダウンロードしましょう

wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.3.0.zip

で、openCVディレクトリに移動してそこでbuildディレクトリを作成。要は

cd opencv-<バージョン>

mkdir build

する。

 

CMakeのGUI版を使いたかったので、次のコマンドでインストール。

sudo apt-get install cmake-qt-gui

あともろもろ必要らしいパッケージを全部インストール。AnacondaとQtをインストールしている場合、WITH_QTオプションを有効化するといろんなファイルのディレクトリを聞いてくるが、よく確認して必要に応じて修正を入れること。

WITH_CUDAはNVIDIAの製品持ってないのでチェックを外す。

以下Ubuntu 16.04 版

sudo apt-get install cmake ecm extra-cmake-modules fcitx-libs-dev libfcitx-qt5-1 mesa-utils build-essential git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev x264 libxvidcore-dev yasm libjasper-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg libpng12-dev libdc1394-22 libdc1394-22-dev libpng12-dev libtiff5-dev libjasper-dev unzip qt5-default libeigen3-dev libeigen3-doc tesseract-ocr tesseract-ocr-jpn vtk6 tcl-vtk6 python-vtk6 libvtk5-dev libgflags-dev autoconf automake libtool autoconf-archive libleptonica-dev libtesseract-dev gphoto2 libgstreamer-plugins-base1.0-dev liblapacke-dev libgoogle-glog-dev libprotobuf-dev libprotoc-dev protobuf-compiler ccache python-vtk libgphoto2-dev libavresmple-dev libvtk6-dev libvtk6-qt-dev

2018/04/06 追記

Ubuntu 17.10版

sudo apt install cmake ecm extra-cmake-modules fcitx-libs-dev fcitx-libs-qt5 mesa-utils build-essential git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-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 unzip 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 ccache libgphoto2-dev libvtk6-dev libvtk6-qt-dev liblapack-dev libatlas-base-dev

 

また、いくつかシンボリックリンクをはらなきゃいけないらしい

sudo ln -s /usr/include/linux/lib4l1-videodev.h videodev.h

なんかエラーがやかましいので、apt-get したけど vtkは削除した。

-> なんとか対処してみた。

 

CMakeの変数を以下のように指定。

Tesseract_INCLUDE_DIR  :   /usr/include/tesseract

Tesseract_LIBRARY  :  /usr/lib/libtesseract.so.3

あとはQt関係の変数をお好みで編集すればOK、なはず。

あとで環境を再構築する際に問題が出たら、この記事に加筆修正を行う予定。

 

sys/videoio.h について 

長い間苦闘したが、Githubのissueにcontributerからこんな発言があった

Again, "sys/videoio.h not found" is not an error - it is just a check result. So this it is not a problem at all.

github.com

要は sys/videoio.h not found のエラーはガン無視して進めてOK 

 

うまくいくとnot foundが極限まで消えていく。

だれかテトリスとかぷよぷよみたいにNot Foundとかエラーを消すチャレンジをしてください

tiny_dnn の類

vtkの

libvtkRenderingPythonTkWidgets.so

がないというエラーは支障なさそうなので無視。

 

まっさら環境からスタートして。

Detected version of GNU GCC: 54 (504)
Looking for ccache - found (/usr/bin/ccache)
Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version "1.2.8", minimum required is "1.2.3")
Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.8")
Checking for module 'gtk+-3.0'
No package 'gtk+-3.0' found
Checking for module 'gtkglext-1.0'
No package 'gtkglext-1.0' found
Looking for linux/videodev.h
Looking for linux/videodev.h - found
Looking for linux/videodev2.h
Looking for linux/videodev2.h - found
Looking for sys/videoio.h
Looking for sys/videoio.h - not found
Checking for module 'libavresample'
No package 'libavresample' found
Checking for module 'libgphoto2'
No package 'libgphoto2' found
found Intel IPP (ICV version): 2017.0.2 [2017.0.2]
at: /home/<username>/Build/opencv-3.3.0/build/3rdparty/ippicv/ippicv_lnx
found Intel IPP IW binaries: 2017.0.2
at: /home/<username>/Build/opencv-3.3.0/build/3rdparty/ippicv/ippicv_lnx/../ippiw_lnx/
LAPACK(OpenBLAS): LAPACK_LIBRARIES: /usr/lib/libopenblas.so
LAPACK(OpenBLAS): Support is enabled.
Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
The imported target "vtkRenderingPythonTkWidgets" references the file
"/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so"
but this file does not exist. Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
"/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
but not all the files it references.

Found VTK ver. 6.2.0 (usefile: /usr/lib/cmake/vtk-6.2/UseVTK.cmake)
Caffe: NO
Protobuf: YES
Glog: YES
tiny-dnn: Download: v1.0.0a3.tar.gz
Looking for tiny_dnn.h
Looking for tiny_dnn.h - found
Found tiny-dnn in: /home/<username>/Build/opencv-3.3.0/build/3rdparty/tinydnn/tiny-dnn-1.0.0a3
Performing Test COMPILER_SUPPORTS_CXX11
Performing Test COMPILER_SUPPORTS_CXX11 - Success
Looking for pthread.h
Looking for pthread.h - found
Looking for pthread_create
Looking for pthread_create - found
Found Threads: TRUE
Performing Test COMPILER_HAS_SSE_FLAG
Performing Test COMPILER_HAS_SSE_FLAG - Success
Performing Test COMPILER_HAS_AVX_FLAG
Performing Test COMPILER_HAS_AVX_FLAG - Success
Performing Test COMPILER_HAS_AVX2_FLAG
Performing Test COMPILER_HAS_AVX2_FLAG - Success
Checking for one of the modules 'freetype2'
Checking for one of the modules 'harfbuzz'
freetype2: YES
harfbuzz: YES
Found HDF5: /usr/lib/x86_64-linux-gnu/hdf5/serial/lib/libhdf5.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libsz.so;/usr/lib/x86_64-linux-gnu/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so (found version "1.8.16")
Performing Test HAVE_CXX_WNO_INVALID_OFFSETOF
Performing Test HAVE_CXX_WNO_INVALID_OFFSETOF - Success
No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
Performing Test GFLAGS_IN_GOOGLE_NAMESPACE
Performing Test GFLAGS_IN_GOOGLE_NAMESPACE - Success
Checking SFM deps... TRUE
CERES support is disabled. Ceres Solver for reconstruction API is required.
Excluding from source files list: /home/<username>/Build/opencv-3.3.0/build/modules/imgproc/accum.neon.cpp
Torch importer has been enabled. To run the tests you have to install Torch ('th' executable should be available) and generate testdata using opencv_extra/testdata/dnn/generate_torch_models.py script.
Looking for tiny_dnn.h
Looking for tiny_dnn.h - found
Found tiny-dnn in: /home/<username>/Build/opencv-3.3.0/build/3rdparty/tinydnn/tiny-dnn-1.0.0a3
Performing Test HAVE_CXX_WNO_NON_VIRTUAL_DTOR
Performing Test HAVE_CXX_WNO_NON_VIRTUAL_DTOR - Success
freetype2: YES
harfbuzz: YES
Checking for modules 'tesseract;lept'
Found tesseract, version 3.04.01
Found lept, version 1.73
Tesseract: YES
xfeatures2d/boostdesc: Download: boostdesc_bgm.i
xfeatures2d/boostdesc: Download: boostdesc_bgm_bi.i
xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i
xfeatures2d/boostdesc: Download: boostdesc_binboost_064.i
xfeatures2d/boostdesc: Download: boostdesc_binboost_128.i
xfeatures2d/boostdesc: Download: boostdesc_binboost_256.i
xfeatures2d/boostdesc: Download: boostdesc_lbgm.i
xfeatures2d/vgg: Download: vgg_generated_48.i
xfeatures2d/vgg: Download: vgg_generated_64.i
xfeatures2d/vgg: Download: vgg_generated_80.i
xfeatures2d/vgg: Download: vgg_generated_120.i
No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
Checking SFM deps... TRUE
CERES support is disabled. Ceres Solver for reconstruction API is required.
Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE
Performing Test HAVE_CXX_WNO_UNUSED_BUT_SET_VARIABLE - Success

General configuration for OpenCV 3.3.0 =====================================
Version control: unknown

Extra modules:
Location (extra): /home/<username>/Build/opencv_contrib-3.3.0/modules
Version control (extra): unknown

Platform:
Timestamp: 2017-10-25T16:15:58Z
Host: Linux 4.10.0-37-generic x86_64
CMake: 3.5.1
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2
SSE4_1 (2 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (7 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

C/C++:
Built as dynamic libs?: YES
C++ Compiler: /usr/bin/c++ (ver 5.4.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release):
Linker flags (Debug):
ccache: YES
Precompiled headers: NO
Extra dependencies: /usr/lib/libopenblas.so gtk-x11-2.0 gdk-x11-2.0 pangocairo-1.0 atk-1.0 cairo gdk_pixbuf-2.0 gio-2.0 pangoft2-1.0 pango-1.0 fontconfig gthread-2.0 /usr/lib/x86_64-linux-gnu/libwebp.so /usr/lib/x86_64-linux-gnu/libjasper.so gstbase-1.0 gstreamer-1.0 gobject-2.0 glib-2.0 gstvideo-1.0 gstapp-1.0 gstriff-1.0 gstpbutils-1.0 dc1394 avcodec-ffmpeg avformat-ffmpeg avutil-ffmpeg swscale-ffmpeg vtkRenderingOpenGL vtkImagingHybrid vtkIOImage vtkCommonDataModel vtkCommonMath vtkCommonCore vtksys vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkDICOMParser vtkIOCore vtkmetaio /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libtiff.so vtkImagingCore vtkRenderingCore vtkCommonColor vtkFiltersExtraction vtkFiltersCore vtkFiltersGeneral vtkCommonComputationalGeometry vtkFiltersStatistics vtkImagingFourier vtkalglib vtkFiltersGeometry vtkFiltersSources vtkInteractionStyle vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOGeometry /usr/lib/x86_64-linux-gnu/libjsoncpp.so vtkFiltersTexture vtkRenderingFreeType /usr/lib/x86_64-linux-gnu/libfreetype.so vtkftgl vtkIOExport vtkRenderingAnnotation vtkImagingColor vtkRenderingContext2D vtkRenderingGL2PS vtkRenderingContextOpenGL /usr/lib/libgl2ps.so vtkRenderingLabel freetype harfbuzz /usr/lib/x86_64-linux-gnu/hdf5/serial/lib/libhdf5.so /usr/lib/x86_64-linux-gnu/libpthread.so /usr/lib/x86_64-linux-gnu/libsz.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libm.so /usr/lib/x86_64-linux-gnu/libglog.so /usr/lib/x86_64-linux-gnu/libgflags.so tesseract lept dl m pthread rt
3rdparty dependencies:

OpenCV modules:
To be built: core flann hdf imgproc ml objdetect phase_unwrapping photo plot reg surface_matching video viz xphoto bgsegm dnn dnn_modern face freetype fuzzy img_hash imgcodecs shape videoio xobjdetect highgui superres ts bioinspired dpm features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco optflow sfm stitching
Disabled: world contrib_world
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 cnn_3dobj cvv matlab

Non-free algorithms are enabled

GUI:
QT: NO
GTK+ 2.x: YES (ver 2.24.30)
GThread : YES (ver 2.48.2)
GtkGlExt: NO
OpenGL support: NO
VTK support: YES (ver 6.2.0)

Media I/O:
ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x0202)
PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.54)
TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.6)
JPEG 2000: /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
GDAL: NO
GDCM: NO

Video I/O:
DC1394 1.x: NO
DC1394 2.x: YES (ver 2.2.4)
FFMPEG: YES
avcodec: YES (ver 56.60.100)
avformat: YES (ver 56.40.101)
avutil: YES (ver 54.31.100)
swscale: YES (ver 3.1.101)
avresample: NO
GStreamer:
base: YES (ver 1.8.3)
video: YES (ver 1.8.3)
app: YES (ver 1.8.3)
riff: YES (ver 1.8.3)
pbutils: YES (ver 1.8.3)
OpenNI: NO
OpenNI PrimeSensor Modules: NO
OpenNI2: NO
PvAPI: NO
GigEVisionSDK: NO
Aravis SDK: NO
UniCap: NO
UniCap ucil: NO
V4L/V4L2: YES/YES
XIMEA: NO
Xine: NO
Intel Media SDK: NO
gPhoto2: NO

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Use Intel IPP: 2017.0.2 [2017.0.2]
at: /home/<username>/Build/opencv-3.3.0/build/3rdparty/ippicv/ippicv_lnx
Use Intel IPP IW: prebuilt binaries (2017.0.2)
Use Intel IPP Async: NO
Use VA: NO
Use Intel VA-API/OpenCL: NO
Use Lapack: YES (/usr/lib/libopenblas.so)
Use Eigen: YES (ver 3.2.92)
Use Cuda: NO
Use OpenCL: YES
Use OpenVX: NO
Use custom HAL: NO

OpenCL: <Dynamic loading of OpenCL library>
Include path: /home/<username>/Build/opencv-3.3.0/3rdparty/include/opencl/1.2
Use AMDFFT: NO
Use AMDBLAS: NO

Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.12)

Python 3:
Interpreter: /usr/bin/python3 (ver 3.5.2)

Python (for build): /usr/bin/python2.7

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Matlab: NO

Documentation:
Doxygen: NO

Tests and samples:
Tests: YES
Performance tests: YES
C/C++ Examples: NO

Install path: /usr/local

cvconfig.h is in: /home/<username>/Build/opencv-3.3.0/build
-----------------------------------------------------------------

Configuring done

 

<以下過去の内容>

次にcmake。オプションをちゃんと追加しないとえらい目にあう

INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules -D BUILD_EXAMPLES=ON -D WITH_CUDA=OFF -D BUILD_TIFF=ON ..

INSTALL_C_EXAMPLES=OFFは読んで字のごとくCのサンプルをインストールしないオプション。ぶっちゃけC版のサンプルっている?

INSTTALL_PYTHON_EXAMPLES=ONも上に同じ。

OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/については、さっきwgetでダウンロードして展開したファイルを参照する。そこら辺に転がってるオプションのコードだと、3.3.0のバージョン部分がかけててエラー吐くことがあるので注意。

BUILD_EXAMPLES=ON よくわからない。

WITH_CUDA=OFF もちろん僕はGPUなんていう大層なものを今はまだ持ってないのでOFFにした。持ってるならONにしよう。

BUILD_TIFF=ON こいつが結構曲者。このオプションがないとmakeのときに文句言ってくる。

make -j $(nproc)

sudo make install

して終了。めでたしめでたし。

あとはg++でコンパイルするときにオプションを忘れなければ未定義どうこうと文句を言われることはなくなる。

g++ -o <出力ファイル名> <コンパイル対象ファイル名>.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv`

 で黙ってていただく。

 

終了!優勝!お疲れ!解散!

こんどキネクト買って遊ぶぞ!

yad2kっていうの実際使ったけども結構強力そう…