Ubuntu16.04LTS上用cmake编译安装OpenCV3

Posted lx17746071609

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu16.04LTS上用cmake编译安装OpenCV3相关的知识,希望对你有一定的参考价值。

一、源码准备:opencv、cmake

1、opencv官网 https://opencv.org/ 下载源码包:opencv-3.4.0.tar.gz

2、cmake官网 https://cmake.org/ 下载源码包:cmake-3.10.2.tar.gz

 

二、安装cmake

1、解压源码、进入解压后的目录

    $ tar -zxvf cmake-3.10.2.tar.gz

    $ cd cmake-3.10.2

 

2、读README.rst文档,按说明操作:

 

解读:首先要有C++编译器,而且要支持C++11标准的(g++版本太低会出错,请自行升级);然后运行$ ./bootstrap 脚本进行配置,可以使用一些项如“--help”、“--prefix”(安装目录); bootstrap成功后,执行$ make  $ make install。

总之,就三个命令:

$ ./bootstrap    

$ make    

$ sudo make install

安装完成,检测下: 

$ cmake --version

 

搞定,就是这么简单。

 

PS:据说还有一种更简单粗暴的方法:

$ sudo apt-get install cmake


三、安装依赖库

$ sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff4-dev libswscale-dev libjasper-dev


四、安装Opencv

1、解压源码、进入解压后的目录

    $ tar -zxvf opencv-3.4.0.tar.gz

    $ cd opencv-3.4.0

 

2、创建编译文件夹build,进入build

    $ mkdir build

    $cd build

 

3、执行以下命令,CMAKE_INSTALL_PREFIX=路径,"..": CMakeLists.txt路径(解压后的opencv中)


    $ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

 

这一步,在3.2.0版本时出了点意外,会出现以下错误:

-- ICV: Downloading ippicv_linux_20151201.tgz...
CMake Error at 3rdparty/ippicv/downloader.cmake:73 (file):
file DOWNLOAD HASH mismatch

for file: [/root/library/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz]
expected hash: [808b791a6eac9ed78d32a7666804320e]
actual hash: [d41d8cd98f00b204e9800998ecf8427e]
status: [1;"Unsupported protocol"]

Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:243 (include)
cmake/OpenCVFindLibsPerf.cmake:37 (include)
CMakeLists.txt:558 (include)


CMake Error at 3rdparty/ippicv/downloader.cmake:77 (message):
ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.
Status=1;"Unsupported protocol"
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:243 (include)
cmake/OpenCVFindLibsPerf.cmake:37 (include)
CMakeLists.txt:558 (include)


-- Configuring incomplete, errors occurred!
See also "/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeOutput.log".
See also "/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeError.log".


百度查了下:安装opencv的过程中要下载ippicv_linux_20151201.tgz,由于网络的原因,这个文件经常会下载失败。

解决方法: 

    1.手动下载 ippicv_linux_20151201.tgz ; 可以百度一下,一大堆。

    2.创建 opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ 目录

   不要照抄!!! 该目录根据输出的错误信息选择创建,如上:(特意将那么多行的错误信息贴出来,就是为了说明这点)


for file: [/root/library/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz]
    3.将下载的文件放置该目录下;

    4.重新执行 cmake 命令即可。

 

4、编译  (未知错误:Ubuntu1204出错,1404正常;猜测可能是依赖库的版本问题)

    $ make

 

5、安装

        $ make install

 

6、配置opencv的环境变量

    $ vi /etc/ld.so.conf.d/opencv.conf      (文件或为空,新创建)

    添加:/usr/local/lib

    $ ldconfig        //使生效

 

7、配置bash

    $ vi /etc/bash.bashrc

    文末添加以下两行:

    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

    export PKG_CONFIG_PATH

    $ source /etc/bash.bashrc    //使生效

 

五、测试

使用自带测试用例,在samples/cpp/example_cmake下


有兴趣的可以阅读下example_cmake下 CMakeLists.txt 是如何写的,里面有大量注释。

    $ cmake .
    $ make

    $ ./opencv_example

 


1、无摄像头环境:

Built with OpenCV 3.4.0
No capture

 

 

2、有摄像头环境:

Built with OpenCV 3.4.0
Capture is opened
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != __null) in Mat, file /root/library/opencv/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp, line 500
terminate called after throwing an instance of ‘cv::Exception‘
  what():  /root/library/opencv/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat

已放弃 (核心已转储)

 

运行出错:打开摄像头超时!!!

尚未找到原因,先搁置一边。

此问题已解决,详见【虚拟机Ubuntu打开摄像头失败及OpenCV打开摄像头超时的问题】!!!

解决问题后的效果:

技术图片

---------------------
作者:曾哥哥_zeng
来源:CSDN
原文:https://blog.csdn.net/qq_30155503/article/details/79419206

以上是关于Ubuntu16.04LTS上用cmake编译安装OpenCV3的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 16.04 LTS 下配置OpenCV与Qt

Ubuntu 16.04LTS下内核编译与安装

[Android 编译] Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程

Ubuntu 16.04 编译VTK7.1

在 Ubuntu 16.04 LTS 上安装 Python 3.6.0

[Android 编译] Ubuntu 16.04 LTS 成功编译 Android 6.0 源码教程