OpenCV-Python官方文档中文翻译4:Install OpenCV-Python in Ubuntu在Ubuntu安装OpenCV-Python

Install OpenCV-Python in Ubuntu

Goals

In this tutorial We will learn to setup OpenCV-Python in Ubuntu System. Below steps are tested for Ubuntu 16.04 and 18.04 (both 64-bit).

OpenCV-Python can be installed in Ubuntu in two ways:

  • Install from pre-built binaries available in Ubuntu repositories
  • Compile from the source. In this section, we will see both.

Another important thing is the additional libraries required. OpenCV-Python requires only Numpy (in addition to other dependencies, which we will see later). But in this tutorials, we also use Matplotlib for some easy and nice plotting purposes (which I feel much better compared to OpenCV). Matplotlib is optional, but highly recommended. Similarly we will also see IPython, an Interactive Python Terminal, which is also highly recommended.

在这个教程我们将学习如何在Ubuntu系统安装OpenCV。以下步骤是在Ubuntu16.04 和 18.04系统(都是64位)上测试的。

有两种方式在Ubuntu上安装OpenCV-Python:

  • 从Ubuntu库里预构建的二进制文件安装
  • 从源代码编译。在这一节,我们将展示两种方法。

另一件重要的事是所需要的额外的库。OpenCV-Python只要求Numpy(除了其他依赖关系,我们将在后面讨论)。但是在这个教程,我们也要用Matplotlib 做一些简单和漂亮的图(我感到比OpenCV好很多)。Matplotlib 是可选的,但是高度推荐。相似的我们也会用到IPython,Python的一个交互终端,也是高度推荐的。

Installing OpenCV-Python from Pre-built Binaries

This method serves best when using just for programming and developing OpenCV applications.

Install package python-opencv with following command in terminal (as root user).

$ sudo apt-get install python-opencv

Open Python IDLE (or IPython) and type following codes in Python terminal.

import cv2 as cv

print(cv.version)

If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.

It is quite easy. But there is a problem with this. Apt repositories may not contain the latest version of OpenCV always. For example, at the time of writing this tutorial, apt repository contains 2.4.8 while latest OpenCV version is 3.x. With respect to Python API, latest version will always contain much better support and latest bug fixes.

So for getting latest source codes preference is next method, i.e. compiling from source. Also at some point in time, if you want to contribute to OpenCV, you will need this.

仅用于编程和开发OpenCV应用的时候,这个方法最有效。

在终端中用下面的命令行安装 python-opencv 库(作为ROOT用户)

$ sudo apt-get install python-opencv

打开Python的集成开发环境(或者IPython),在Python终端输入下面命令。

import cv2 as cv

print(cv.version)

如果结果打印出来没有任何错误,恭喜!!!你成功地安装了OpenCV-Python。

这相当简单。但是有一个问题。Apt库可能不总是能包含最新版本的OpenCV。举个例子,在写教程的这个时间,Apt库包含2.4.8,然而最新的OpenCV版本是3.x。至于Python的应用程序接口,最新的版本将总是包含更好的支持和最新的bug修复。

所以对于获得最新的源代码首选的下一种方法,从源代码编译。并且在某些时间点,如果你想要对OpenCV做出贡献,你也需要这样做。

Building OpenCV from source

Compiling from source may seem a little complicated at first, but once you succeeded in it, there is nothing complicated.

First we will install some dependencies. Some are required, some are optional. You can skip optional dependencies if you don’t want.

最初从源文件编译可能有一点复杂,但是你一旦成功了,就没什么复杂的了。

首先我们将安装一些依赖关系。有些是必须的,有些是可选的。如果你不想要的话可以跳过一些可选的依赖关系。

Required build dependencies

We need CMake to configure the installation, GCC for compilation, Python-devel and Numpy for building Python bindings etc.

我们需要CMake来配置安装,GCC用来编译,Python-develNumpy用来构建Python bindings等等。

sudo apt-get install cmake

sudo apt-get install gcc g++

to support python2:

sudo apt-get install python-dev python-numpy

to support python3:

sudo apt-get install python3-dev python3-numpy

Next we need GTK support for GUI features, Camera support (v4l), Media Support (ffmpeg, gstreamer) etc.

接下来我们需要 GTK来支持GUI特性,相机支持(v4l),媒体支持(ffmpeg,gstreamer)等等。

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

to support gtk2:

sudo apt-get install libgtk2.0-dev

to support gtk3:

sudo apt-get install libgtk-3-dev

Optional Dependencies

Above dependencies are sufficient to install OpenCV in your Ubuntu machine. But depending upon your requirements, you may need some extra dependencies. A list of such optional dependencies are given below. You can either leave it or install it, your call 😃

以上依赖项足以在你的Ubuntu计算机上安装OpenCV了。但是取决于你的需求你可能需要一些其他的依赖项。下面给出了这些可选依赖关系的列表。你可以跳过或者安装它们,取决于你:)

OpenCV comes with supporting files for image formats like PNG, JPEG, JPEG2000, TIFF, WebP etc. But it may be a little old. If you want to get latest libraries, you can install development files for system libraries of these formats.

OpenCV附带了支持像PNG,JPEG,JPEG2000等图片格式的支持文件。但是,可能有一点老旧。如果你想要安装最新的库,你可以为这些格式的系统库安装开发文件。

sudo apt-get install libpng-dev

sudo apt-get install libjpeg-dev

sudo apt-get install libopenexr-dev

sudo apt-get install libtiff-dev

sudo apt-get install libwebp-dev

  • Note

    If you are using Ubuntu 16.04 you can also install libjasper-dev to add a system level support for the JPEG2000 format.

    如果你用的是Ubuntu 16.04,你也可以安装 libjasper-dev 来添加一个JPEG2000的系统级别的支持。

Downloading OpenCV

To download the latest source from OpenCV’s GitHub Repository. (If you want to contribute to OpenCV choose this. For that, you need to install Git first)

从OpenCV的 GitHub Repository下载最新版的源代码。(如果你想要为OpenCV做出贡献,请选择此种方式。为此,你需要首先安装Git

$ sudo apt-get install git

$ git clone https://github.com/opencv/opencv.git

It will create a folder “opencv” in current directory. The cloning may take some time depending upon your internet connection.

它将在当前目录创建“opencv”文件夹。取决与你的网络连接状况,这个克隆将花费一些时间。

Now open a terminal window and navigate to the downloaded “opencv” folder. Create a new “build” folder and navigate to it.

现在打开一个终端窗口,导向下载的“opencv”文件夹。创建一个新的“build”文件夹并导向它。

$ mkdir build

$ cd build

Configuring and Installing

Now we have all the required dependencies, let’s install OpenCV. Installation has to be configured with CMake. It specifies which modules are to be installed, installation path, which additional libraries to be used, whether documentation and examples to be compiled etc. Most of this work are done automatically with well configured default parameters.

现在我们拥有所有需要的依赖项,让我们开始安装OpenCV。安装必须用CMake来配置。它明确了哪个模块需要安装,安装路径,哪一个额外库将被用到,文档和例程是否要编译等等。大多数的工作都是使用配置好的默认参数自动完成。

Below command is normally used for configuration of OpenCV library build (executed from build folder):

下面的命令行通常用来配置OpenCV库的构建(从build文件夹执行)

$ cmake ../

OpenCV defaults assume “Release” build type and installation path is “/usr/local”. For additional information about CMake options refer to OpenCV C++ compilation guide:

You should see these lines in your CMake output (they mean that Python is properly found):

OpenCV默认假设“Release”版本类型,安装路径是“/usr/local”。有关CMake选项的更多信息,请参考OpenCV C++ compilation guide:

– Python 2:

– Interpreter: /usr/bin/python2.7 (ver 2.7.6)

– Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)

– numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)

– packages path: lib/python2.7/dist-packages

– Python 3:

– Interpreter: /usr/bin/python3.4 (ver 3.4.3)

– Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)

– numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)

– packages path: lib/python3.4/dist-packages

Now you build the files using “make” command and install it using “make install” command.

现在你用“make”命令行构建文件,用“make install”命令行安装。

$ make

# sudo make install

Installation is over. All files are installed in “/usr/local/” folder. Open a terminal and try import “cv2”.

安装结束。所有的文件安装在"/usr/local/"文件夹。打开一个终端,尝试import cv2。

import cv2 as cv

print(cv.version)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值