ubuntu16.04 python3.5 Opencv3.2安装

1、安装所需的编译器、依赖等

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2、下载opencv源码
cd ~/ #如我的的是在~/Downloads/opencv
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

3、利用cmake编译构建opencv
     首先创建一个临时文件夹用于存放编译过程中的文件,要在下载opencv的文件夹内部创建,这样可以自动找到opencv的路径,我的是~/Downloads/opencv/opencv/build,随后通过如下命令进行cmake。
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_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-python3/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS = /usr/local/lib/python3.5/dist-packages/numpy/core/include/ ..


Ubuntu14.04, python3.4的cmake命令为:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local PYTHON3_EXECUTABLE = /usr/bin/python3 PYTHON_INCLUDE_DIR = /usr/include/python3.4m PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python3.4m PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython3.4m.so PYTHON3_NUMPY_INCLUDE_DIRS = /usr/local/lib/python3.4/dist-packages/numpy/core/include/ ..

cmake -D CMAKE_BUILD_TYPE=RELEASE -D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON_EXECUTABLE=$(which python3) -D PYTHON_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..


    随后进行make,命令make -j4,这里j4是指根据自己电脑的内核数,创建多个编译线程
    最后安装,sudo make install
4、完成后,通过如下验证:
python3
import cv2
cv2.__version__

另外的安装资料:

6.1.2 安装和使用OpenCV

作为最流行的视觉包,在Linux中安装OpenCV是非常方便的,大多数Linux的发行版都支持包管理器的安装,比如在Ubuntu 16.04 LTS中,只需要在终端中输入:

sudo apt-get install libopencv-dev

sudo apt-get install python-opencv (p是小写,csdn显示有问题)

当然也可以通过官网下载源码编译安装,第一步先安装各种依赖:

>> sudo apt install build-essential

>> sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

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

然后找一个clone压缩包的文件夹,把源码拿下来:

>> git clone opencv/opencv

然后进入OpenCV文件夹:

>> mkdir release

>> cd release

>> cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local

准备完毕,直接make并安装:

>> make

>> sudo make install

Windows下的安装也很简单,直接去OpenCV官网下载:

DOWNLOADS | OpenCV

执行exe安装后,会在<安装目录>/build/python/2.7下发现一个叫cv2.pyd的文件,把这个文件拷贝到\Lib\site-packages下,就可以了。Windows下如果只想在Python中体验OpenCV还有个更简单的方法是加州大学尔湾分校(University of California, Irvine)的Christoph Gohlke制作的Windows下的Python科学计算包网页,下载对应版本的wheel文件,然后通过pip安装:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

本书只讲Python下OpenCV基本使用,Python中导入OpenCV非常简单:

import cv2 
就导入成功了。


三、opencv的卸载
进入安装包的build文件执行:
make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
四、查看opencv版本
pkg-config --modversion opencv

对于opencv的安装最好直接在github上下载,地址在opencv的官网安装向导中可以找到,github一般包含2.4和3.3的最新版本,下载相应的版本后通过上述的方法进行安装。

你可能感兴趣的:(opencv)