OpenCV还不能用于Python 3.3吗?我真的必须降级到Python 2.7才能使用它吗?我在网上没有找到太多关于它的信息,只有2012年的一些帖子说OpenCV还没有移植到Python 3中.x、 但现在是2014年,在try 安装最新的OpenCV 2.4之后.x并将cv2.pyd文件复制到C:\Program Files (x86)\Python333\Lib\site-packages这仍然会在Python IDLE中产生错误:

>>> import cv2
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cv2
ImportError: DLL load failed: %1 ist keine zulässige Win32-Anwendung.

推荐答案

编辑:首先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)

Python-3.x相关问答推荐

如何匹配字母,数字,短划线,逗号,但不是如果没有数字和字母?

PythonPandas -通过知道位置(Loc)而不是索引来删除行

TypeError:&Quot;Value&Quot;参数必须是标量、Dict或Series,但您传递了&Quot;Index&Quot;

在Pandas 数据帧中为小于5位的邮政编码添加前导零

以特定方式重新排列 pandas 数据框的列

在新数据帧上自动提取两个字符串 python 之间的相等性

如何将列表和字典逐行组合在一起

Pandas 在每组两个条件之间获得时间增量

判断 gekko 中的表达式

避免重复连续字符但不包括一个特定字符的正则表达式

总结基于条件的值,如果不匹配则保留当前值

在python中基于列表理解的条件下跳过元素

获取嵌套字典的所有键

Python 类型提示语法如何/为什么起作用?

在 Pandas 数据框中显示对图

如何在 Python3 中添加带有标志的命令行参数?

python 3的蓝牙库

为 Python 3 和 PyQt 构建可执行文件

在 Python 3 中调用 super() 的 4 种方法中的哪一种?

哪个更有效:Python 文档字符串还是类型提示?