编辑:首先try 新的pip方法:
视窗:pip3 install opencv-python opencv-contrib-python
Ubuntu:sudo apt install python3-opencv
或者继续下面的构建说明
注意:最初的问题是询问OpenCV+Python 3.3+Windows.从那时起,Python 3.5已经发布.此外,我在大多数开发中都使用Ubuntu,所以很遗憾,这个答案将集中在这个设置上
OpenCV 3.1.0+Python 3.5.2+Ubuntu 16.04是可能的!下面是方法.
这些步骤复制(并稍加修改)自:
Prerequisites
安装所需的依赖项,并可选地在系统上安装/更新一些库:
# Required dependencies
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# Dependencies for Python bindings
# If you use a non-system copy of Python (eg. with pyenv or virtualenv), then you probably don't need to do this part
sudo apt install python3.5-dev libpython3-dev python3-numpy
# Optional, but installing these will ensure you have the latest versions compiled with OpenCV
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Building OpenCV
CMake旗帜
有几个标志和选项可以调整OpenCV的构建.可能有关于它们的全面文档,但以下是一些可能有用的有趣标志.它们应包含在cmake
命令中:
# Builds in TBB, a threading library
-D WITH_TBB=ON
# Builds in Eigen, a linear algebra library
-D WITH_EIGEN=ON
使用非系统级Python版本
如果您有多个Python版本(例如,使用pyenv或virtualenv),那么您可能需要针对某个Python版本进行构建.默认情况下,OpenCV将为系统版本的Python构建.您可以通过将这些参数添加到脚本后面看到的cmake
命令中来改变这一点.实际值将取决于您的设置.我用pyenv
:
-D PYTHON_DEFAULT_EXECUTABLE=$HOME/.pyenv/versions/3.5.2/bin/python3.5
-D PYTHON_INCLUDE_DIRS=$HOME/.pyenv/versions/3.5.2/include/python3.5m
-D PYTHON_EXECUTABLE=$HOME/.pyenv/versions/3.5.2/bin/python3.5
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1
CMake Python错误消息
CMakeLists文件将try 检测要构建的各种Python版本.如果你在这里有不同的版本,它可能会混淆.上述参数可能只"修复"了一个版本Python的问题,而不是另一个版本.如果你只关心那个特定的版本,那么就没什么好担心的了.
不幸的是,对于我来说就是这样,我还没有研究如何解决其他Python版本的问题.
安装脚本
# Clone OpenCV somewhere
# I'll put it into $HOME/code/opencv
OPENCV_DIR="$HOME/code/opencv"
OPENCV_VER="3.1.0"
git clone https://github.com/opencv/opencv "$OPENCV_DIR"
# This'll take a while...
# Now lets checkout the specific version we want
cd "$OPENCV_DIR"
git checkout "$OPENCV_VER"
# First OpenCV will generate the files needed to do the actual build.
# We'll put them in an output directory, in this case "release"
mkdir release
cd release
# Note: This is where you'd add build options, like TBB support or custom Python versions. See above sections.
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local "$OPENCV_DIR"
# At this point, take a look at the console output.
# OpenCV will print a report of modules and features that it can and can't support based on your system and installed libraries.
# The key here is to make sure it's not missing anything you'll need!
# If something's missing, then you'll need to install those dependencies and rerun the cmake command.
# OK, lets actually build this thing!
# Note: You can use the "make -jN" command, which will run N parallel jobs to speed up your build. Set N to whatever your machine can handle (usually <= the number of concurrent threads your CPU can run).
make
# This will also take a while...
# Now install the binaries!
sudo make install
默认情况下,install
脚本将把Python绑定放在某个系统位置,即使您已经指定了要使用的自定义Python版本.修复方法很简单:在本地site-packages
中添加绑定的符号链接:
ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so $HOME/.pyenv/versions/3.5.2/lib/python3.5/site-packages/
第一条路径将取决于要构建的Python版本.第二个问题取决于定制版Python的位置.
Test it!
好的,让我们试试吧!
ipython
Python 3.5.2 (default, Sep 24 2016, 13:13:17)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import cv2
In [2]: img = cv2.imread('derp.png')
i
In [3]: img[0]
Out[3]:
array([[26, 30, 31],
[27, 31, 32],
[27, 31, 32],
...,
[16, 19, 20],
[16, 19, 20],
[16, 19, 20]], dtype=uint8)