美文网首页
Ubuntu16.04 install OpenCV with

Ubuntu16.04 install OpenCV with

作者: Goet | 来源:发表于2018-07-06 14:56 被阅读0次

OpenCV是一个非常强大的计算机视觉方面的库,支持C++、python、java等语言,是CV领域的机器学习常用的工具。平常的工作中涉及到视频流的处理,就需要在编译OpenCV的时候添加ffmpeg的支持。下面介绍如何在Ubuntu16.04中编译安装OpenCV (for python)。

一、安装python3.5及numpy并配置为默认版本

1.安装
# apt-get install python3.5
# apt-get install python3-dev
# apt-get instal python3-numpy
2.配置

利用update-alternatives将python3.5配置为默认版本,首先需要把两个版本的python分别添加进去。

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

然后把python3.5设置为默认。

# update-alternatives --config python

选择python3.5对应的号码即可。在命令行输入python发现版本号为3.5,然后import numpy,不报错即证明numpy安装成功。

二、安装ffmpeg

先到官网下载安装包ffmpeg.org/download.html
这里推荐下载2.8版本(根据官方描述貌似2.8对Ubuntu16.04也就是Xenial Xerus兼容性做得最好)。

解压、配置、编译、安装

解压:

# tar -jxvf ffmpeg-2.8.14.tar.bz2
# cd ffmpeg-2.8.14

配置:

# ./config --enable-shared --prefix=/usr

到这里出现报错:

yasm/nasm not found or too old. Use --disable-yasm for a crippled build.

If you think configure made a mistake, make sure you are using the latest version from 
Git.  If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing
list or IRC #ffmpeg on irc.freenode.net. Include the log file "config.log" produced by 
configure as this will help solve the problem.

发现没有安装yasm,所以安装yasm。

# apt-get install yasm

重复配置的命令,然后编译:

# make

会出现一些warning跟note,应该不影响编译,只要没有报错就OK。
安装:

# make install

检查是否安装成功:

# ffmpeg -version

三、安装OpenCV

1.安装一些依赖

根据官方所说,需要安装以下依赖:

  • compiler
# apt-get install build-essential
  • required
# apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  • optional
# apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
2.下载安装OpenCV

OpenCV Download Page
选择一个合适的版本点击Sources下载源码,我选择的是3.3.0版本。解压后进入解压目录,输入以下命令:

# mkdir build
# cd build

然后最最最重要的步骤——配置,cmake的时候如果要编译python库、包含ffmpeg等等,就需要一系列的参数:

# cmake -D CMAKE_BUILD_TYPE=Release -D WITH_FFMPEG=ON -D CMAKE_INSTALL_PREFIX=/usr/local PYTHON3_EXECUTABLE = /usr/bin/python3 PYTHON_INCLUDE_DIR = /usr/include/python3.5 PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python3.5m PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS = /usr/lib/python3/dist-packages/numpy/core/include/ ..

有路径不相同的请自行修改。配置完之后注意要检查FFMPEG一项后面一定要是YES,否则证明配置不成功,这个时候继续编译并安装的话是没有ffmpeg的哦~


配置结果部分截图

然后执行以下命令:

# make -j4 //四核CPU所以是-j4
# make install

到这里,安装过程就结束啦,可以用python import cv2 测试一下是否成功。

相关文章

网友评论

      本文标题:Ubuntu16.04 install OpenCV with

      本文链接:https://www.haomeiwen.com/subject/yyipuftx.html