opencv3.4.0 源碼編譯安裝

本文講解Ubuntu16.04下用源碼安裝OpenCV3.4.0,並實現兩種版本的opencv的相互切換,以及編譯文件如何寫。其實不用安裝那麼多的依賴,並且也不需要開啓那麼多功能,根據實際項目去編譯吧。

源碼下載

[下載OpenCV3.4.0](https://github.com/opencv/opencv/archive/3.4.0.zip)
[opencv_contrib](https://github.com/opencv/opencv_contrib/releases/tag/3.4.0)

官方安裝教程:
https://docs.opencv.org/3.4.0/d7/d9f/tutorial_linux_install.html

這是cmake之後的輸出,會顯示那些編譯了,那些沒有編譯。

-- General configuration for OpenCV 3.4.0 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2020-04-18T03:46:26Z
--     Host:                        Linux 4.15.0-96-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 (3 files):          + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (2 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (9 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 -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -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 -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 -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:          dl m pthread rt
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python2 python_bindings_generator shape stitching superres ts video videoio videostab viz
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3
--     Applications:                tests perf_tests apps
--     Documentation:               YES (/usr/bin/doxygen 1.8.11)
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.18.9)
--       GThread :                  YES (ver 2.48.2)
--       GtkGlExt:                  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:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
-- 
--   Video I/O:
--     DC1394:                      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:                YES (ver 2.1.0)
--     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)
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     YES
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2017.0.3 [2017.0.3]
--            at:                   /home/q/packages/opencv-3.4.0/build/3rdparty/ippicv/ippicv_lnx
--     Intel IPP IW:                sources (2017.0.3)
--               at:                /home/q/packages/opencv-3.4.0/build/3rdparty/ippicv/ippiw_lnx
--     Lapack:                      YES (/usr/lib/liblapack.so /usr/lib/libcblas.so /usr/lib/libatlas.so)
--     Eigen:                       YES (ver 3.2.10)
--     Custom HAL:                  NO
-- 
--   NVIDIA CUDA:                   NO
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/q/packages/opencv-3.4.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:                        NO
-- 
--   Install to:                    /usr/local/opencv-3.4.0
-- -----------------------------------------------------------------

安裝依賴

#!/bin/bash
set  -e
# 構建系統
sudo apt-get install build-essential  -y 
sudo apt-get install checkinstall  -y 
sudo apt-get install cmake  -y
sudo apt-get install pkg-config  -y
sudo apt-get install yasm  -y
sudo apt-get install gfortran  -y
# 圖片庫
sudo apt-get install libjpeg8-dev  -y
sudo apt-get install libjasper-dev  -y
sudo apt-get install libpng12-dev  -y
sudo apt-get install libtiff5-dev  -y
# 視頻庫
sudo apt-get install libavcodec-dev  -y
sudo apt-get install libavformat-dev  -y
sudo apt-get install libswscale-dev  -y
sudo apt-get install libdc1394-22-dev  -y
sudo apt-get install x264  -y
sudo apt-get install v4l-utils  -y
# 播放器庫
sudo apt-get install libxine2-dev  -y

sudo apt-get install libgstreamer0.10-dev  -y
sudo apt-get install libgstreamer-plugins-base0.10-dev  -y

# GUI庫
sudo apt-get install qt5-default libgtk2.0-dev  -y
# 運算庫
sudo apt-get install libtbb-dev libatlas-base-dev  -y

# 音頻庫
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev  -y
# 語音識別庫
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev  -y

# Optional dependencies
# Protobuf

sudo apt-get install libprotobuf-dev protobuf-compiler  -y

# Google的日誌和命令行標識處理庫
sudo apt-get install libgoogle-glog-dev libgflags-dev  -y

# 用來訪問處理各種攝像頭的庫文件 
sudo apt-get install libeigen3-dev libhdf5-dev doxygen  -y


sudo apt-get install python-dev  -y 
sudo apt-get install python-numpy  -y

sudo apt-get install -y libopencv-dev 
sudo apt-get install -y git 
sudo apt-get install -y libdc1394-22 
sudo apt-get install -y unzip
sudo apt-get install -y libgtk-3-dev


sudo apt-get install -y libopenblas-dev
sudo apt-get install -y liblapacke-dev
sudo apt-get install python-opencv
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

sudo apt-get install -y ccache

sudo sudo apt-get install libavresample-dev libgphoto2-dev 

sudo apt install -y libv4l-dev*



exit 0

然後在終端執行:
sudo apt-get update
sudo sh opencv_dependenc.sh
就可以一次性配置好了

編譯

在這裏插入圖片描述
上面這張圖片是開啓安裝了很多的東西

cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-3.4.0/ -DINSTALL_PYTHON_EXAMPLES=ON -DINSTALL_C_ESAMPLES=ON -DWITH_TBB=ON -DWITH_V4L=ON -D WITH_QT=ON WITH_OPENGL=ON -DOPENCV_EXTRA_MODULES_PATH=/home/q/packages/opencv_contrib-3.4.0/modules/ -DWITH_VTK=ON -DBUILD_EXAMPLES=ON ..

如果有些東西下載不了,手動下載

– IPPICV: Download: ippicv_2017u3_lnx_intel64_general_20170822.tgz
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20170822/ippicv

默認下載路徑
opencv-3.4.0/.cache/ippicv/4e0352ce96473837b1d671ce87f17359-ippicv_2017u3_lnx_intel64_general_20170822.tgz

xfeatures2d/boostdesc: Download: boostdesc_bgm.i

在這裏插入圖片描述q@omen:~/packages/opencv-3.4.0/.cache/xfeatures2d/boostdesc
在這裏插入圖片描述
q@omen:~/packages/opencv-3.4.0/.cache/xfeatures2d/vgg
放在這個路徑下面

如果多個版本就指定另一個安裝路徑,默認情況:cmake和make編譯OpenCV源碼,指明編譯生成的所有lib文件都會被安裝到/usr/local目錄,但是爲了便於管理我們多創建一層目錄名字就是“opencv-3.4.0”後面編譯生成的東西都會放在這個路徑下面,便於查找和管理。就是 CMAKE_INSTALL_PREFIX=/usr/local/opencv-3.4.0

CMakeLists.txt

存在多版本OpenCV時,需要找到所需版本對應的OpenCVConfig.cmake文件,並將其路徑添加到工程的CMakeLists.txt中。
示例如下:

set(OpenCV_DIR "CMAKE_INSTALL_PREFIX")
OpenCV_DIR 就是你剛纔給CMAKE_INSTALL_PREFIX指定的安裝地址
FIND_PACKAGE( OpenCV 2.4 REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )

查看opencv版本

pkg-config –modversion opencv  
pkg-config –cflags opencv  
pkg-config –libs opencv

系統安裝兩個版本的opencv 在使用的時候,在CMakeLists.txt文件中指定就好
唯一的區別就是路徑。
默認路徑:/usr/local/
指定路徑:/usr/local/opencv-3.4.0

默認這麼寫
FIND_PACKAGE( OpenCV 3.4.0 REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
FIND_PACKAGE找默認路徑下的OpenCV

指定這樣寫

set( OpenCV_DIR /usr/local/opencv-3.4.0)
find_package(OpenCV 3.4.0)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.9)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.9 not found.")
   endif()
endif()
include_directories(${OPENCV_INCLUDE_DIR})
MESSAGE("OPENCV_INCLUDE_DIR " ${OPENCV_INCLUDE_DIR})
target_link_libraries(vo_simulate ${OpenCV_LIBS})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章