OpenCV3.4.x版本+Opencv_contrib+Ubuntu16.04安装记录

文章目录

    前言
        安装过程
            安装前的必备包
                安装依赖包
                处理图像所需的包
                处理视频所需包
                opencv功能优化
                部分依赖包
                可选依赖
            编译和安装
            运行测试
    安装过程命令总结

前言

Opencv发展至今,已经到了4.0版本稳定版的出现,但是项目上,很多人还是估计喜欢3版本,4.0版本才刚刚出来,而且相应的学习书籍还没出现,所以本人还是喜欢用3版本的opencv,而且因为多台电脑开发,所以有时候opencv需要多装许多次,而且遇到的坑也多,故而记录一下,怕以后忘记了,又各种查官方的文档。
有一个opencv的安装指南,在官网中找到的。感觉还不错,也是ubuntu16.04的,不过配置繁多,烟花缭乱,而且综合自己以前编译成功时的安装项,然后两者结合产生了本文,不需要像对方一样,这么的多,而且本人也不是需要安装太多,里面有部分大家看了博文,如果觉得有需要安装则安装。
下面是按照步骤一步步说明怎么安装,并且还有代码进行测试,另外如果不想看这么多的文字内容,直接想复制粘贴进行命令行安装,则查看命令总结部分。
安装过程

博主在其中安装的过程,采用的安装样例,是3.4.1和其对应的opencv扩展版本,其实个人认为,对于其他opencv3.4版本二者安装过程其实是一样的。
你需要下载opencv3.4.1和opencv_contrib 3.4.1,然后对其解压,这些基础命令和操作则不概述。
将安装包解压到某一自己指定的目录,记为{Opencv_Origin_Dir},目前我指定的目录解压到了,/home/tanqiwei/Documents/environment,所以{Opencv_Origin_Dir}对应就是/home/tanqiwei/Documents/environment/opencv-3.4.1

tanqiwei@ubuntu:~/Documents/environment$ pwd
/home/tanqiwei/Documents/environment
tanqiwei@ubuntu:~/Documents/environment$ ls
opencv-3.4.1         opencv_contrib-3.4.1

安装前的必备包

这些安装不算十分完全,我只安装自己够用就成的某些包。
安装一些必要的库,还有cmake,git,g++。

sudo apt-get install build-essential
sudo apt-get install cmake git g++

安装依赖包

安装一些依赖包。

sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install checkinstall yasm libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils

处理图像所需的包

sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

处理视频所需包

sudo apt-get install libxvidcore-dev libx264-dev ffmpeg

opencv功能优化

sudo apt-get install libatlas-base-dev gfortran

部分依赖包

sudo apt-get install libopencv-dev  libqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.4 libcholmod3.0.6
sudo apt-get install python-dev python-numpy

可选依赖

sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

编译和安装

进入OpenCV的源码解压目录,{Opencv_Origin_Dir},我的是/home/tanqiwei/Documents/environment/opencv-3.4.1
我的opencv_contrib目录和其同级,/home/tanqiwei/Documents/environment/opencv_contrib-3.4.1均在/home/tanqiwei/Documents/environment下然后在{Opencv_Origin_Dir}下运行

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules -D PYTHON_EXECUTABLE=/usr/bin/python3.5 -D BUILD_EXAMPLES=ON ..

这里面过多参数都是属于cmake的范畴,我这里不去描述,大概就是表示opencv应该安装在哪里,扩展包在何处,需要开启什么功能。

其实编译过程中会发现,自行下载IPPICV,tiny-dnn等等。

   IPPICV是个链接的免费子库,如果想要禁用IPP加速,CMake的时候,加上-D WITH_IPP=OFF。

其实很多可能可选的从cmake的编译输出看来我们并没有安装,比如java,VTK等等,看下面这种类型的输出就知道了,到时候你只需要对应安装,然后修改CMake编译命令,一般来说,opencv编译过程中,自发也会去寻找这些东西。
 

-- Checking for module 'gstreamer-base-1.0'
--   No package 'gstreamer-base-1.0' found
-- Checking for module 'gstreamer-video-1.0'
--   No package 'gstreamer-video-1.0' found
-- Checking for module 'gstreamer-app-1.0'
--   No package 'gstreamer-app-1.0' found
-- Checking for module 'gstreamer-riff-1.0'
--   No package 'gstreamer-riff-1.0' found
-- Checking for module 'gstreamer-pbutils-1.0'
--   No package 'gstreamer-pbutils-1.0' found


-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
-- Could NOT find Pylint (missing:  PYLINT_EXECUTABLE)
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN)
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file

-- 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.

-- CERES support is disabled. Ceres Solver for reconstruction API is required.
-- Module opencv_ovis disabled because OGRE3D was not found
-- Caffe:   NO
-- Protobuf:   NO

-- Checking for modules 'tesseract;lept'
--   No package 'tesseract' found
--   No package 'lept' found

最后会列出其编译后的模块列表。

--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cvv 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 python_bindings_generator reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn_modern java matlab ovis python2 python3 viz
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   GUI:
--     QT:                          YES (ver 5.5.1)
--       QT OpenGL support:         YES (Qt5::OpenGL 5.5.1)
--     GTK+:                        NO
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so)
--     VTK support:                 NO
--
--   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:                        build (ver encoder: 0x020e)
--     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 0.10.36)
--       video:                     YES (ver 0.10.36)
--       app:                       YES (ver 0.10.36)
--       riff:                      YES (ver 0.10.36)
--       pbutils:                   YES (ver 0.10.36)
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     YES
--
--   Parallel framework:            TBB (ver 4.4 interface 9002)
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Intel IPP:                   2017.0.3 [2017.0.3]
--            at:                   /home/tanqiwei/Documents/environment/opencv-3.4.1/build/3rdparty/ippicv/ippicv_lnx
--     Intel IPP IW:                sources (2017.0.3)
--               at:                /home/tanqiwei/Documents/environment/opencv-3.4.1/build/3rdparty/ippicv/ippiw_lnx
--     Lapack:                      YES (/usr/lib/liblapack.so /usr/lib/libcblas.so /usr/lib/libatlas.so)
--     Eigen:                       YES (ver 3.2.92)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
--
--   NVIDIA CUDA:                   NO
--
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/tanqiwei/Documents/environment/opencv-3.4.1/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python (for build):            /usr/bin/python3
--
--   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/tanqiwei/Documents/environment/opencv-3.4.1/build

我们可以发现,我们编译已经成功,可以进行下一步,即make,但是值得注意的是,如果用多核make可能会报错,为了保险起见,我还是原始的make命令,不加-j。

make

OpenCV3.4.x版本+Opencv_contrib+Ubuntu16.04安装记录_第1张图片

然后安装。

sudo make install

最后最好开启重启一次,本人曾安装过后,虚拟机重启直接奔溃,无法进入系统内部,主要原因不太清楚,但是进入到了某种图形模式,说是图形模式损坏,之后只好从备份的快照中恢复,当然也有当时可能装少了部分必要依赖项的可能性也说不定,建议安装的机器内存要大一点,4GB为一般,6GB不错,8GB很好。
运行测试

我们运行例子进行测试。你可以选择任意例子,这里我选择在我的github的opencv例子进行测试。

git clone https://github.com/tanqiwei/myOpencvStudyCode.git

大概几M的内容,然后进入myOpencvStudyCode/LearningOpencv3/chapter2/example2.1
接着按下面命令

mkdir build
cd build
cmake ..
make
./example2_1 ../data/test.jpg

OpenCV3.4.x版本+Opencv_contrib+Ubuntu16.04安装记录_第2张图片

你可以发现运行成功,故而咱们安装顺利。会发现显示图片窗口,按ESC键退出。
重启后,发现还能开启,说明虚拟机的16.04的系统安装就成功了。

安装过程命令总结

# 安装及下载,该操作不解释,都放在一个统一目录下
# 我的是/home/tanqiwei/Documents/environment
# 也就是在environment文件夹里有opencv-3.4.1和opencv_contrib-3.4.1两个文件夹

# 安装必备库,cmake,git,g++
sudo apt-get install build-essential
sudo apt-get install cmake git g++
# 安装依赖项
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install checkinstall yasm libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils
# 处理图像所需的包
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
# 处理视频所需的包
sudo apt-get install libxvidcore-dev libx264-dev ffmpeg
# opencv功能优化
sudo apt-get install libatlas-base-dev gfortran
# 某些依赖包
sudo apt-get install libopencv-dev  libqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.4 libcholmod3.0.6
sudo apt-get install python-dev python-numpy
# 可选依赖项
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# 进入opencv源码目录,注意opencv和opencv_contrib同级,
# 即都属于同一个主目录下,我的目录为/home/tanqiwei/Documents/environment,
# 下面有opencv-3.4.1和opencv_contrib-3.4.1


mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.1/modules -D PYTHON_EXECUTABLE=/usr/bin/python3.5 -D BUILD_EXAMPLES=ON ..
make
sudo make install
 

 

你可能感兴趣的:(OpenCV)