Ubuntu,如何为python3安装OpenCV? [英] Ubuntu, how to install OpenCV for python3?

查看:86
本文介绍了Ubuntu,如何为python3安装OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ubuntu 16.04中安装适用于python3的OpenCV.我试过运行sudo apt-get install python3-opencv的拳头,这就是我几乎安装所有python软件的方式.找不到存储库.如果我执行sudo apt-get install python-opencv,则安装确实有效,因为此问题是通过不将这三个添加到python中,它会为我不使用的python 2安装.我真的会认为不必从源代码进行构建和安装,所以有没有办法获取存储库?我还尝试使用pip3安装它,但也找不到它.

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.

推荐答案

这将是一个冗长的答案,所以让我们开始:

Well this will be a lengthy answer, so let's start :

第1步:安装必备组件: 升级所有预安装的软件包:

Step 1: Install prerequisites : Upgrade any pre-installed packages:

$ sudo apt-get update
$ sudo apt-get upgrade

安装用于编译OpenCV 3.0的开发人员工具:

Install developer tools used to compile 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

安装GTK,以便我们可以使用OpenCV的GUI功能:

Install GTK so we can use OpenCV’s GUI features:

$ sudo apt-get install libgtk2.0-dev

安装用于优化OpenCV内部各种功能的软件包,例如矩阵运算:

Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

$ sudo apt-get install libatlas-base-dev gfortran

第2步:设置Python(第1部分)

让我们为Python 3安装下载pip(Python包管理器)

Let’s download pip , a Python package manager, installed for Python 3:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

让我们使用全新的pip3安装程序来设置virtualenv和virtualenvwrapper:

Let’s use our fresh pip3 install to setup virtualenv and virtualenvwrapper :

$ sudo pip3 install virtualenv virtualenvwrapper

现在,我们可以更新〜/.bashrc文件(位于文件底部):

Now we can update our ~/.bashrc file (place at the bottom of the file):

# virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
$ mkvirtualenv cv

第2步:设置Python(第2部分)

我们需要安装Python 3.4+标头和开发文件:

we’ll need to install the Python 3.4+ headers and development files:

$ sudo apt-get install python3.4-dev

OpenCV将图像表示为NumPy数组,因此我们需要将NumPy安装到我们的简历虚拟环境中:

OpenCV represents images as NumPy arrays, so we need to install NumPy into our cv virtual environment:

$ pip install numpy

第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

是时候建立版本了:

$ 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 ..

让我们开始OpenCV编译过程:

Let's start OpenCV compile process :

$ make -j4

假设OpenCV 3.0编译没有错误,您现在可以将其安装在系统上:

Assuming OpenCV 3.0 compiled without error, you can now install it on your system:

$ sudo make install
$ sudo ldconfig

第4步:Sym-link OpenCV 3.0

如果您已完成此步骤,则现在应在/usr/local/lib/python3.4/site-packages/中安装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绑定以cv2.cpython-34m.so

Here, our OpenCV bindings are stored under the name cv2.cpython-34m.so

但是,为了在cv虚拟环境中使用OpenCV 3.0,我们首先需要将OpenCV符号链接到cv环境的site-packages目录中,如下所示:(请务必注意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)

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so

请注意,我是如何将名称从cv2.cpython-34m.so更改为cv2.so的-这是为了使Python可以使用名称cv2导入我们的OpenCV绑定.

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 .

第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.

这篇关于Ubuntu,如何为python3安装OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆