Linux-ubuntu16.04 Python3.5配置OpenCV3.2的方法

1.OpenCV下载

首先创建一个空的文件夹,进入文件夹执行如下命令,如我创建的文件夹是opencv-python

cd opencv-python
git clone https://github.com/opencv/opencv.git

也可以直接进入网址OpenCV_Download下载

使用git下载后应有一个名字为opencv的文件夹,进入文件夹执行如下命令:

cd opencv
mkdir build
cd build

2.编译opencv源码

使用cmake编译opencv源码,在编译前请确认所有的依赖项已经正确安装,可执行如下命令:

sudo apt-get install build-essential
sudo apt-get 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

如果项目已经安装会自动略过,如果项目没有安装则会安装。

在使用cmake编译阶段系统是64位还是32位在这个地方执行的命令是不同的

我的电脑比较渣,装的是32位的系统,这里以32位系统的编译命令为主,同样介绍64位的编译命令,理论上除了文件路径其他应该一样,但是没有经过验证,请慎重!!!

以上执行完毕后,32位执行如下命令进行编译

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/i386-linux-gnu/python3.5m PYTHON_LIBRARY = /usr/bin/i386-linux-gnu-python3/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS = /usr/local/lib/python3.5/dist-packages/numpy/core/include/ ..

对于64位系统,cmake命令中的可选项即PYTHON_INCLUDE_DIR等在官方指导文件中可以通过下列提示具体查看

PYTHON2(3)_EXECUTABLE = <path to python>
PYTHON_INCLUDE_DIR = /usr/include/python<version>
PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python<version>
PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython<version>.so
PYTHON2(3)_NUMPY_INCLUDE_DIRS = /usr/lib/python<version>/dist-packages/numpy/core/include/

参考网上其他人给出的编译命令,将64位系统编译命令写出,但是未经本人验证,如果有已经配置成功的同学请给我留言,以便完善。

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

64位或32位系统编译opencv只在cmake命令有差别,cmake执行完之后分别执行如下命令

make
sudo make install

如果没有出错的话,至此使用python3开发opencv的开发环境就已经配置完成了。写个小程序试一下:

示例代码

import cv2
srcImage = cv2.imread('lena.jpg')
cv2.imshow('Python3', srcImage)
cv2.waitKey(0)

运行结果如下:

Linux-ubuntu16.04 Python3.5配置OpenCV3.2的方法

相关推荐