编辑:首先尝试新的 pip 方法:
Windows: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 是可能的!方法如下。
这些步骤是从以下位置复制(并稍作修改)的:
先决条件
在您的系统上安装所需的依赖项并选择性地安装/更新一些库:
# 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
构建 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 文件将尝试检测要构建的各种 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 版本所在的位置。
测试一下!
好的,让我们试试吧!
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)