Ubuntu,如何为python3安装OpenCV?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37188623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ubuntu, how to install OpenCV for python3?
提问by chasep255
I want to install OpenCV for python3 in ubuntu 16.04. Fist I tried running sudo apt-get install python3-opencv
which is how I pretty much install all of my python software. This could not find a repository. The install does work however if I do sudo apt-get install python-opencv
this issue with this is that by not adding the three to python it installs for python 2 which I do not use. I would really perfer not to have to build and install from source so is there a way I can get a repository? I also tried installing it with pip3 and it could not find it either.
我想在 ubuntu 16.04 中为 python3 安装 OpenCV。拳头我尝试运行sudo apt-get install python3-opencv
,这就是我几乎安装所有python软件的方式。这找不到存储库。安装确实有效,但是如果我解决sudo apt-get install python-opencv
这个问题是因为没有将这三个添加到 python 中,它会为我不使用的 python 2 安装。我真的宁愿不必从源代码构建和安装,所以有没有办法获得存储库?我也尝试用 pip3 安装它,但也找不到它。
回答by Vtik
Well this will be a lengthy answer, so let's start :
好吧,这将是一个冗长的答案,让我们开始吧:
Step 1: Install prerequisites :Upgrade any pre-installed packages:
步骤 1:安装先决条件:升级任何预安装的软件包:
$ sudo apt-get update
$ sudo apt-get upgrade
Install developer tools used to compile OpenCV 3.0:
安装用于编译 OpenCV 3.0 的开发者工具:
$ sudo apt-get install build-essential cmake git pkg-config
Install libraries and packages used to read various image and videos formats from disk:
安装用于从磁盘读取各种图像和视频格式的库和包:
$ sudo apt-get install libjpeg8-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
Install GTK so we can use OpenCV's GUI features:
安装 GTK 以便我们可以使用 OpenCV 的 GUI 功能:
$ sudo apt-get install libgtk2.0-dev
Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:
安装用于优化 OpenCV 内部各种功能的包,例如矩阵运算:
$ sudo apt-get install libatlas-base-dev gfortran
Step 2: Setup Python (Part 1)
第 2 步:设置 Python(第 1 部分)
Let's download pip , a Python package manager, installed for Python 3:
让我们下载为 Python 3 安装的 Python 包管理器 pip :
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
Let's use our fresh pip3 install to setup virtualenv and virtualenvwrapper :
让我们使用新的 pip3 安装来设置 virtualenv 和 virtualenvwrapper :
$ sudo pip3 install virtualenv virtualenvwrapper
Now we can update our ~/.bashrc file (place at the bottom of the file):
现在我们可以更新我们的 ~/.bashrc 文件(位于文件底部):
# virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
$ mkvirtualenv cv
Step 2: Setup Python (Part 2)
第 2 步:设置 Python(第 2 部分)
we'll need to install the Python 3.4+ headers and development files:
我们需要安装 Python 3.4+ 头文件和开发文件:
$ sudo apt-get install python3.4-dev
OpenCV represents images as NumPy arrays, so we need to install NumPy into our cv virtual environment:
OpenCV 将图像表示为 NumPy 数组,因此我们需要将 NumPy 安装到我们的 cv 虚拟环境中:
$ pip install numpy
Step 3: Build and install OpenCV 3.0 with Python 3.4+ bindings
第 3 步:使用 Python 3.4+ 绑定构建和安装 OpenCV 3.0
$ cd ~
$ git clone https://github.com/opencv/opencv.git
$ cd opencv
$ git checkout 3.0.0
$ cd ~
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib
$ git checkout 3.0.0
Time to setup the build:
设置构建的时间:
$ cd ~/opencv
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Let's start OpenCV compile process :
让我们开始 OpenCV 编译过程:
$ make -j4
Assuming OpenCV 3.0 compiled without error, you can now install it on your system:
假设 OpenCV 3.0 编译没有错误,你现在可以在你的系统上安装它:
$ sudo make install
$ sudo ldconfig
Step 4: Sym-link OpenCV 3.0
第 4 步:符号链接 OpenCV 3.0
If you've reached this step, OpenCV 3.0 should now be installed in /usr/local/lib/python3.4/site-packages/
.
如果你已经到了这一步,OpenCV 3.0 现在应该安装在/usr/local/lib/python3.4/site-packages/
.
Here, our OpenCV bindings are stored under the name cv2.cpython-34m.so
在这里,我们的 OpenCV 绑定存储在名称下 cv2.cpython-34m.so
However, in order to use OpenCV 3.0 within our cv virtual environment, we first need to sym-link OpenCV into the site-packages directory of the cv environment, like this: (Be sure to take note of cv2.cpython-34m.so
)
但是,为了在我们的 cv 虚拟环境中使用 OpenCV 3.0,我们首先需要将 OpenCV 符号链接到 cv 环境的 site-packages 目录中,如下所示:(一定要注意cv2.cpython-34m.so
)
$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so
Notice how I am changing the name from cv2.cpython-34m.so to cv2.so — this is so Python can import our OpenCV bindings using the name cv2 .
请注意我如何将名称从 cv2.cpython-34m.so 更改为 cv2.so — 这样 Python 就可以使用名称 cv2 导入我们的 OpenCV 绑定。
Step 5: Test out the OpenCV 3.0 and Python 3.4+ install
第 5 步:测试 OpenCV 3.0 和 Python 3.4+ 安装
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.0.0'
Hope that helps. Also, credit to Adrian Rosebrock on his post. It worked for me as a charm.
希望有帮助。此外,感谢 Adrian Rosebrock 在他的帖子中。它对我来说是一种魅力。
回答by Trevor
I found this:
我找到了这个:
https://pypi.python.org/pypi/opencv-python
https://pypi.python.org/pypi/opencv-python
OpenCV on wheels
轮子上的 OpenCV
'Unofficial OpenCV packages for Python.'
“用于 Python 的非官方 OpenCV 包。”
Installation was painless for Ubuntu 16.04
Ubuntu 16.04 的安装很轻松
pip3 install opencv-python
Check the installation
检查安装
python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
Not sure why this wasn't mentioned. Perhaps it is newly available?
不知道为什么没有提到这一点。也许它是新的?
回答by Robin
Assuming that you installed Python3.x, I resolved it using the following:
假设您安装了 Python3.x,我使用以下方法解决了它:
1: Install side packages required for OpenCV with Ubuntu (only validated for: Ubuntu 16.04):
1:使用 Ubuntu 安装 OpenCV 所需的侧包(仅针对:Ubuntu 16.04 进行验证):
apt-get update
apt-get install -y libglib2.0.0 libsm6
apt-get install libxext6
apt-get install -y libxrender-dev
2: Install OpenCV on python3.x:
2:在python3.x上安装OpenCV:
pip3 install opencv-contrib-python
回答by k26dr
Using conda inside a python3 environment:
在 python3 环境中使用 conda:
First install conda in a python3 environment and activate it if you haven't yet:
首先在python3环境中安装conda,如果你还没有激活它:
conda create --name py3k python=3
source activate py3k
Now you can install opencv in the conda environment:
现在你可以在 conda 环境中安装 opencv:
pip install pillow
conda install -c menpo opencv3=3.1.0
To import in Python:
在 Python 中导入:
import cv2
回答by makoulis
This is because you have multiple installations of python in your machine.You should make python3 the default, because by default is the python2.7
这是因为你的机器上安装了多个python。你应该把python3设为默认,因为默认是python2.7
回答by Js541
sudo pip3 install opencv-python opencv-contrib-python
回答by karel
Credit is due to the accepted answer for being correct for Ubuntu 16.04, however in Ubuntu 18.04 and later Python 3 bindings for the OpenCV (Open Computer Vision) library can be installed from the default Ubuntu repositories with the following command:
归功于对 Ubuntu 16.04 正确的公认答案,但是在 Ubuntu 18.04 和更高版本中,可以使用以下命令从默认的 Ubuntu 存储库安装 OpenCV(开放计算机视觉)库的 Python 3 绑定:
sudo apt install python3-opencv
The Open Computer Vision Library is a collection of algorithms and sample code for various computer vision problems. The library is compatible with IPL (Intel's Image Processing Library) and, if available, can use IPP (Intel's Integrated Performance Primitives) for better performance.
开放计算机视觉库是各种计算机视觉问题的算法和示例代码的集合。该库与 IPL(英特尔的图像处理库)兼容,并且如果可用,可以使用 IPP(英特尔的集成性能基元)以获得更好的性能。