编辑:首先尝试新的点子方法:
视窗:
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!这是如何做。
这些步骤是从以下位置复制(并稍作修改)的:
- http://docs.opencv.org/3.1.0/d7/d9f/tutorial_linux_install.html
- https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html#install-opencv-python-in-fedora
安装所需的依赖项,还可以选择在系统上安装/更新一些库:
构建OpenCVCMake标志# Required dependenciessudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavprec-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 partsudo apt install python3.5-dev libpython3-dev python3-numpy# Optional, but installing these will ensure you have the latest versions compiled with OpenCVsudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
有几个标记和选项可调整您的OpenCV的构建。可能有关于它们的综合文档,但是这里有一些有趣的标志可能有用。它们应包含在
cmake命令中:
使用非系统级别的Python版本# Builds in TBB, a threading library-D WITH_TBB=ON# Builds in Eigen, a linear algebra library-D WITH_EIGEN=ON
如果您有多个版本的Python(例如,使用pyenv或virtualenv),则可能要针对某个Python版本进行构建。默认情况下,OpenCV将为系统的Python版本构建。您可以通过将这些参数添加到
cmake脚本后面的命令中来进行更改。实际值将取决于您的设置。我用
pyenv:
CMake Python错误消息-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
CMakeLists文件将尝试检测要构建的各种Python版本。如果这里有其他版本,则可能会感到困惑。上面的参数只能“解决”一个版本的Python的问题,而不能“解决”另一个版本的问题。如果您只关心该特定版本,则无需担心。
不幸的是,我就是这种情况,我还没有研究如何解决其他Python版本的问题。
安装脚本# Clone OpenCV somewhere# I'll put it into $HOME/pre/opencvOPENCV_DIR="$HOME/pre/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 wantcd "$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 releasecd 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自定义版本所在的位置。
测试一下!OK,让我们尝试一下!
ipythonPython 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 cv2In [2]: img = cv2.imread('derp.png')iIn [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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)