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})

猜你喜欢

转载自blog.csdn.net/qq_21950671/article/details/105761963
今日推荐