慚愧,以前一直沒在linux下編譯過opencv,也沒用過純命令行版的cmake,如今咬牙編譯一次。其實感受還湊合。html
opencv官網文檔仍是那麼爛:https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.htmljava
按這個安裝,仍是會缺不少選項。python
只好參考好幾個blog大概增長下面這些,應該就沒啥問題了linux
比較有用的參考:https://github.com/BVLC/caffe/wiki/OpenCV-3.3-Installation-Guide-on-Ubuntu-16.04c++
卡了2天,這真是個巨坑!在和caffe裏才發現 https://github.com/BVLC/caffe/issues/1559git
若是已經安裝了anaconda,那麼which python3 會顯示路徑是anaconda下的,連帶的LD_LIBRARY等等路徑,都會優先找這裏,直接致使WITH_GDAL編譯opencv時,libtiff.so也會找到anaconda路徑裏, 結果就是WITH_GDAL死活make不過(WITH_TIFF + BUILD_TIFF能夠,可是沒了直接的GDAL支持。)github
解決辦法:web
sudo xed ~/.profile編程
確保 註釋掉了 這行ubuntu
#PATH="$HOME/anaconda3/bin:$PATH"
而後
sudo xed ~/.bashrc
確保最後幾行 都是註釋掉的
#PATH="/home/machinelearning/anaconda3/bin:$PATH"
# added by Anaconda3 installer
# export PATH="/home/machinelearning/anaconda3/bin:$PATH"
而後註銷一下,確認一下python3的路徑
which python3
顯示這樣就OK了
若是不肯意細分依賴庫,直接參考這裏 Installation OpenCV 3.4.1 on Ubuntu 17.10
sudo apt install -y \ x264 \ mesa-utils \ libgtk2.0-dev \ libxvidcore-dev \ yasm \ libxine2-dev \ libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \ libv4l-dev \ libfaac-dev \ libmp3lame-dev \ libopencore-amrnb-dev \ libtheora-dev \ libvorbis-dev \ ffmpeg \ libpng-dev \ libdc1394-22-dev \ qt5-default \ libtiff5-dev \ libeigen3-dev libeigen3-doc \ tesseract-ocr \ tesseract-ocr-jpn \ vtk6 \ tcl-vtk6 \ python-vtk6 \ libgflags-dev \ libleptonica-dev \ libtesseract-dev \ gphoto2 \ liblapacke-dev \ libgoogle-glog-dev \ libprotobuf-dev \ libprotoc-dev \ protobuf-compiler \ ccache \ libgphoto2-dev \ libavresample-dev \ libvtk6-dev \ libvtk6-qt-dev \ libatlas-base-dev \ gfortran
也能夠細分一下各依賴庫
sudo apt-get install --assume-yes libgdal-dev libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libtiff5-dev
注意這裏,libgdal-dev是必裝的,由於編譯時帶着gdal編譯了。但tiff比較糾結,
opencv裏用tiff都是4.0 而這裏安裝的是5(ubuntu 在14.04以後就沒有libtiff4了), 在編譯gdal的時候可能會報錯。因此後面編譯選項裏要有2個和tiff有關的選項。
sudo apt-get install libeigen3-dev
若是你開發的是命令行程序並不顯示任何圖像,或者顯示的圖片很簡單,那麼不須要轉換到 cv::ogl 下。
若是你的應用耗費了大量時間在圖片的顯示上,或是但願擁有高質量的界面系統,那麼你能夠藉助 cv::ogl::Texture2D 加速圖像的渲染。
若是你開發的是加強現實應用,你確定已經擁有了本身的三維渲染模塊,能夠考慮與 cv::ogl::Buffer 整合。
若是你已經在使用 CUDA 模塊,對於渲染的時候數據須要回傳到 CPU 表示畫蛇添足,那麼你可使用 CUDA 與 OpenGL 的協同功能去除多餘的數據傳輸。
另外一方面,若是你不是 OpenCV 的用戶可是你正在開發虛擬現實應用,你能夠考慮將視覺計算引入到你的系統中,實現相似 HoloLens 的設備。
sudo apt-get install doxygen
sudo apt-get install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev sudo apt-get install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils vtk6 sudo apt-get install --assume-yes liblapacke-dev libopenblas-dev libgdal-dev checkinstall
sudo apt-get install libatlas-base-dev
SET(Open_BLAS_INCLUDE_SEARCH_PATHS$ENV{OpenBLAS_HOME}$ENV{OpenBLAS_HOME}/include/opt/OpenBLAS/include/usr/local/include/openblas/usr/include/openblas/usr/local/include/openblas-base/usr/include/openblas-base/usr/local/include/usr/include/usr/include/x86_64-linux-gnu)SET(Open_BLAS_LIB_SEARCH_PATHS$ENV{OpenBLAS}cd$ENV{OpenBLAS}/lib$ENV{OpenBLAS_HOME}$ENV{OpenBLAS_HOME}/lib/opt/OpenBLAS/lib/usr/local/lib64/usr/local/lib/lib/openblas-base/lib64//lib//usr/lib/openblas-base/usr/lib64/usr/lib/usr/lib/x86_64-linux-gnu)
直接複製運行
cmake ~/opencv\
-DCMAKE_BUILD_TYPE=Release\
-DCMAKE_INSTALL_PREFIX=/usr/local\
-DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules\
-DBUILD_DOCS=ON\
-DBUILD_EXAMPLES=ON\
-DWITH_TBB=ON\
-DBUILD_TIFF=ON -DWITH_TIFF=ON\
-DWITH_GDAL=ON\
-DWITH_LIBV4L=ON\
-DPYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3\
-DWITH_CUDA=ON -DWITH_CUBLAS=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES"\
-DWITH_OPENCL=OFF\
-DWITH_VTK=OFF\
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_xfeatures2d=OFF
-- General configuration for OpenCV 4.0.0-pre =====================================
-- Version control: 3.4.3-249-g80610ca05
--
-- Extra modules:
-- Location (extra): /home/machinelearning/opencv_contrib/modules
-- Version control (extra): 3.4.3-56-g5c362968
--
-- Platform:
-- Timestamp: 2018-09-11T03:10:03Z
-- Host: Linux 4.15.0-34-generic x86_64
-- CMake: 3.12.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 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (4 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (10 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /usr/bin/c++ (ver 7.3.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 -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-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 -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-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 -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-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 -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release):
-- Linker flags (Debug):
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab ximgproc xobjdetect xphoto
-- Disabled: js python2 world xfeatures2d
-- Disabled by dependency: sfm
-- Unavailable: cnn_3dobj cvv java matlab ovis viz
-- Applications: tests perf_tests examples apps
-- Documentation: doxygen python
-- Non-free algorithms: NO
--
-- GUI:
-- GTK+: YES (ver 3.22.30)
-- GThread : YES (ver 2.56.2)
-- GtkGlExt: NO
--
-- Media I/O:
-- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
-- TIFF: build (ver 42 - 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 1.7.1)
-- GDAL: YES (/usr/lib/libgdal.so)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
-- PFM: YES
--
-- Video I/O:
-- DC1394: YES (ver 2.2.5)
-- FFMPEG: YES
-- avcodec: YES (ver 57.107.100)
-- avformat: YES (ver 57.83.100)
-- avutil: YES (ver 55.78.100)
-- swscale: YES (ver 4.8.100)
-- avresample: YES (ver 3.7.0)
-- GStreamer:
-- base: YES (ver 1.14.1)
-- video: YES (ver 1.14.1)
-- app: YES (ver 1.14.1)
-- riff: YES (ver 1.14.1)
-- pbutils: YES (ver 1.14.1)
-- libv4l/libv4l2: 1.14.2 / 1.14.2
-- v4l/v4l2: linux/videodev2.h
--
-- Parallel framework: TBB (ver 2017.0 interface 9107)
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Lapack: YES (/usr/lib/x86_64-linux-gnu/liblapack.so /usr/lib/x86_64-linux-gnu/libcblas.so /usr/lib/x86_64-linux-gnu/libatlas.so)
-- Eigen: YES (ver 3.3.4)
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- NVIDIA CUDA: YES (ver 9.2, CUFFT CUBLAS NVCUVID)
-- NVIDIA GPU arch: 30 35 37 50 52 60 61 70
-- NVIDIA PTX archs:
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.6.5)
-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.5)
-- numpy: /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.15.1)
-- packages path: lib/python3.6/dist-packages
--
-- Python (for build): /usr/bin/python3
-- Pylint: /usr/local/bin/pylint (ver: 3.6.5, checks: 160)
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Matlab: NO
--
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/machinelearning/opencv_build
若是是屏蔽了anaconda 應該就不會報錯了。
基本上大部分視頻,圖片格式都支持這是最基本的。我用到的特性都標紅了。注意tiff那行,是build,若是編譯選項沒有強制ON
則會是 /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9) 那麼make時走到gdal的時候,會報錯。
參考http://answers.opencv.org/question/35642/libtiff_40-link-errors/
只要最後不是這樣,若是有各類提示,好比libopencl.so怎麼被hide之類的,或者python3沒找到,就卸載點包。再試試,應該是乾乾淨淨的沒有任何警告纔對。
別忘了每次清空當前文件夾
最後就是漫長的編譯過程
make -j8
開8個進程並行編譯!
別忘了
sudo make install
當年在學校,一直在win下用cmake-gui編譯opencv2.X. 和如今比起來,其實仍是linux下用cli編譯更舒服。
首先,安裝各類包直接apt,不須要本身處處下載zip解壓,安裝exe
而後,其實所有複雜性都在cmake 那句話裏了。在文本編輯器裏編輯好,用好續行符。其實沒什麼難的。可是要吃透每一個-D選項,確實是google一番,特別是配置出錯的時候,尤爲是python那一堆路徑。
不耐心+恐懼感,會致使不敢去學CLI。其實無非只是心魔而已。
充分感覺《unix編程藝術》:
文本化接口: cmake 命令行1行代碼-> CMakeLists.txt 67k ->makefile 500k 能夠根據print出來的配置信息,檢查makefile是否包含了本身指望的特性。 實現的是用一行代碼,若干個-D配置項,不斷生成配置文件,用較少的代碼去生成更多的代碼。
生成原則/自動化/CLI接口: 避免手寫makefile這樣的配置文件,而是用cli生成它