需要准备的基本材料(请先看完整个安装过程再进行下面的操作):
一、到Opecv-Release的Github项目上下载最新的Opencv版本,注意---基于python2.7,可选用 OpenCV2.3/2.4 以及 OpenCV3.X,我们选取版本3.3:
下载地址:https://github.com/opencv/opencv/releases/tag/3.3.0
二、安装GUN GCC编译器,对于Ubuntu16.04版本的Linux操作系统这款编译器是自带的
三、安装Cmake跨平台编译工具(CMake 可以编译源代码、制作程序库、产生适配器(wrapper)、还可以用任意的顺序建构执行档)安装的方法:sudo apt-get install cmake
我已经安装好了,就提示无需更新,无需安装
四、安装python2.7,在ubuntu中一般来说是自带python2.7的,如果没有安装,请参看博客中其他的教程
五、安装numpy,在安装好python之后建议您安装pip,安装完pip之后就可以使用pip来安装各种各样的库了,pip install numpy
我已经安装好了,就提示无需更新,无需安装
六、安装pkg-config指令(注意,这里的安装很重要,例如libgtk2.0-dev软件包涉及到了opencv中的imshow等窗口调用能否使用的问题):
sudo apt-get install pkg-config libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
开始安装Opencv For Python:
1、找到下载的Opencv3.3的目录:
2、执行下面的指令(在执行指令的过程之前,我们可以切换到su超级权限下进行操作,也可以不切换到su权限下,切换到那个用户,以后就只能在那个用户下调用对应的cv2的图像处理库):
tar -xzvf opencv-3.3.0.tar.gz cd opencv-3.3.0/ cd .. mkdir my_build_dir cd my_build_dir cmake ../opencv-3.3.0 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local make # make -j4表示开4个线程来进行编译 make install
解压之后的opencv-3.3.0的目录:
Cmake的过程和结果如下:
建立的my_build_dir经过 cmake ../opencv-3.3.0 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local之后的结果
在my_build_dir中执行make和make install即可
编写代码:
运行结果:
安装过程之中碰到的问题:
"FATAL: In-source builds are not allowed.
You should create separate directory for build files."
意思就是说不要在源代码内部直接编译程序,以免污染源代码,所以在opencv-2.4.13目录之外建立文件夹my_build_dir,然后在这个目录里面执行cmake。
详细问题参照下面的链接:
https://stackoverflow.com/questions/45518317/in-source-builds-are-not-allowed-in-cmake
Ubuntu下卸载重装Opencv的教程参考:
http://www.cnblogs.com/txg198955/p/5990295.html
注:安装之后可能遇到的问题
Question-1:
简单的意思就是调用了window相关的程序,但是没有安装GTK相关的包,函数function不能实现,需要安装libgtk2.0-dev 和 pkg-config。
如果没有使用到opencv的显示接口的话,基本的opencv的函数是能够使用的,在通过numpy的相关的数学运算包转化一下数据格式就可以使用matlibplot等包显示输出图像了。
如果想解决上述的问题,就一定记得安装libgtk2.0-dev和pkg-config之后在cmake ---》make ---》 make install.Opencv就行了。
Question-2:
我们发现在安装libgtk2.0-dev的软件包的时候,很难安装成功,全是提示说安装包和其他的依赖的安装包存在版本不兼容的问题,如下图所示:
# apt-get install libgtk2.0-dev Reading package lists... Done Building dependency tree Reading state information... Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: libgtk2.0-dev: Depends: libgtk2.0-0 (= 2.20.0-0ubuntu4) but 2.20.1-0ubuntu2.1 is to be installed Depends: libglib2.0-dev (>= 2.21.3) but it is not going to be installed Depends: libpango1.0-dev (>= 1.20) but it is not going to be installed Depends: libatk1.0-dev (>= 1.13.0) but it is not going to be installed Depends: libcairo2-dev (>= 1.6.4-6.1) but it is not going to be installed Depends: libx11-dev (>= 2:1.0.0-6) but it is not going to be installed Depends: libxext-dev (>= 1:1.0.1-2) but it is not going to be installed Depends: libxinerama-dev (>= 1:1.0.1-4.1) but it is not going to be installed Depends: libxi-dev (>= 1:1.0.1-4) but it is not going to be installed Depends: libxrandr-dev (>= 1:1.2.99) but it is not going to be installed Depends: libxcursor-dev but it is not going to be installed Depends: libxfixes-dev (>= 1:3.0.0-3) but it is not going to be installed Depends: libxcomposite-dev (>= 1:0.2.0-3) but it is not going to be installed Depends: libxdamage-dev (>= 1:1.0.1-3) but it is not going to be installed E: Broken packages
将较新的库降级为满足依赖关系的库,按照提示逐步操作即可。
这里我们就需要使用如下的指令来完成安装:
sudo aptitude install libgtk2.0-dev
运行后会出现一大堆消息,给出第一个解决方案一般是保留这些库文件,让选择y/n/q?,这里选择“n”;然后会继续找解决方案,有时会超时找不到,点击y继续找,直到给出得解决方案是“降级”这些库文件,选择“y”,然后等待结束就完了。
这里我们看到提示:Setting up libgtk2.0-dev(2.24.25-3+deb8u2) ...
完成后可以再输入“sudo apt-get install libgtk2.0-dev”进行测试,出现已经安装对应得库文件,说明安装成功了。(此方法同样适用于安装opencv时其他不能正常安装的预备库文件)
Finished!