ubuntu 18.04編譯opencv3.4.3 with python3.6 cuda9.2 gdal


慚愧,之前一直沒在linux下編譯過opencv,也沒用過純命令行版的cmake,現在咬牙編譯一次。其實感覺還湊合。

 

opencv官網文檔還是那么爛:https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

按這個安裝,還是會缺很多選項。

只好參考好幾個blog大概增加下面這些,應該就沒啥問題了

比較有用的參考:https://github.com/BVLC/caffe/wiki/OpenCV-3.3-Installation-Guide-on-Ubuntu-16.04

 

1確保屏蔽anaconda

卡了2天,這真是個巨坑!在和caffe里才發現 https://github.com/BVLC/caffe/issues/1559

如果已經安裝了anaconda,那么which python3 會顯示路徑是anaconda下的,連帶的LD_LIBRARY等等路徑,都會優先找這里,直接導致WITH_GDAL編譯opencv時,libtiff.so也會找到anaconda路徑里, 結果就是WITH_GDAL死活make不過(WITH_TIFF + BUILD_TIFF可以,但是沒了直接的GDAL支持。)

解決辦法:

sudo xed ~/.profile

確保 注釋掉了 這行

#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了

 

2 Before Installation

如果不願意細分依賴庫,直接參考這里  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

也可以細分一下各依賴庫

 圖片格式和gdal

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有關的選項。 

eigen

sudo apt-get install libeigen3-dev

opengl

如果你開發的是命令行程序並不顯示任何圖像,或者顯示的圖片很簡單,那么不需要轉換到 cv::ogl 下。

如果你的應用耗費了大量時間在圖片的顯示上,或是希望擁有高質量的界面系統,那么你可以借助 cv::ogl::Texture2D 加速圖像的渲染。

如果你開發的是增強現實應用,你肯定已經擁有了自己的三維渲染模塊,可以考慮與 cv::ogl::Buffer 整合。

如果你已經在使用 CUDA 模塊,對於渲染的時候數據需要回傳到 CPU 表示多此一舉,那么你可以使用 CUDA 與 OpenGL 的協同功能去除多余的數據傳輸。

另一方面,如果你不是 OpenCV 的用戶但是你正在開發虛擬現實應用,你可以考慮將視覺計算引入到你的系統中,實現類似 HoloLens 的設備。

 
sudo apt-get install libgl1-mesa-dev libglut-dev
 
但我實際測試結果是, opencv github 的master 4.0.0pre 無論用gtk還是qt5.11 只要開了opengl,在編譯到highgui的時候必然報錯(cv::Mat 什么的),暫時無解
考慮到我暫時不需要渲染3d物體,也不特別在意cpu-gpu數據copy的代價,所以不帶opengl編譯了。
 

文檔:

sudo apt-get install doxygen

TBB及其他

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
 

lapack

sudo apt-get install libatlas-base-dev
需要在opencv源碼包里修改。在opencv/cmake/OpenCVFindOpenBLAS.cmake里增加2個路徑
 
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
)
 
 
 
大概每個人編譯opencv選項都不太一樣吧,
這次不像當年還用intel編譯器+mkl了,稍微少了點折騰。但是又帶上了gdal(以及顯式使用tiff),因為要處理地圖
 關了opencl和xfeatures2d是因為make的時候報錯,且我用不上。
 
 在~新建一個opencv_build文件夾,cd進去。復制粘貼下面的配置,注意每個-D后面沒有空格,每個之間有空格 結尾用了續行符\
~/opencv 是opencv源碼位置, 指定了第3方庫的路徑~/opencv_contrib/modules
 然后就是python的一大堆路徑,指明我需要python3
然后就是cuda
其他的qt tbb gdal 都很簡單了。
 

3 cmake

直接復制運行 

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生成它


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM