OpenCV-Python官方文档中文翻译3:Install OpenCV-Python in Fedora在Fedora上安装OpenCV-Python

Install OpenCV-Python in Fedora

Goals

In this tutorial

  • We will learn to setup OpenCV-Python in your Fedora system. Below steps are tested for Fedora 18 (64-bit) and Fedora 19 (32-bit).

在此教程

  • 我们将学习如何在Fedora系统安装OpenCV-Python。以下步骤测试于64位的Fedora18和32位的Fedora19。

Introduction

OpenCV-Python can be installed in Fedora in two ways, 1) Install from pre-built binaries available in fedora repositories, 2) Compile from the source. In this section, we will see both.

有两种方式可以在Fedora上安装OpenCV-Python。1.从Fedora存储库中用预构建的二进制文件安装。2.从源文件编译。在这一节,我们将用两种。

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.

另一件重要的事是所要求的其他库。OpenCV-Python仅需要Numpy库(除了其他依赖关系外,我们将在后面讨论)。但在这个教程中,我们也要用Matplotlib 来完成一些简单而漂亮的作图(与OpenCV比起来效果更棒)。Matplotlib是可选的,但高度推荐安装。相似地,我们来看一看IPython ,这是一个交互式的Python终端,也是高度推荐安装的。

Installing OpenCV-Python from Pre-built Binaries从预构建的二进制文件安装OpenCV-Python

Install all packages with following command in terminal as root.//以ROOT身份从终端用下面的命令行安装所有的包。

$ yum install numpy opencv*

Open Python IDLE (or IPython) and type following codes in Python terminal.//打开Python集成开发环境(或IPython),在终端输入下面的代码。

​ >>> import cv2 as cv

​ >>> print( cv.version )

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

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

It is quite easy. But there is a problem with this. Yum repositories may not contain the latest version of OpenCV always. For example, at the time of writing this tutorial, yum repository contains 2.4.5 while latest OpenCV version is 2.4.6. With respect to Python API, latest version will always contain much better support. Also, there may be chance of problems with camera support, video playback etc depending upon the drivers, ffmpeg, gstreamer packages present etc.

这很简单。但有一个问题。yum仓库可能不总是能包含最新版本的OpenCV。举个例子,在写这个教程的时候,yum仓库有2.4.5版本但是最新的版本是2.4.6 。关于Python应用程序接口,最新的版本总是会包含更多的支持。另外,取决于现在的驱动,ffmpeg,gstreamer包等等,相机支持,视频播放等等都可能出现问题。

So my personal 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做出贡献,你也需要这种方法。

Installing 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 compulsory, some are optional. Optional dependencies, you can leave if you don’t want.

从源代码编译最开始可能有一点复杂,但是一旦你成功了,这里就没有什么复杂的了。

首先我们安装依赖项。一些是强制的,一些是可选的。可选依赖项,如果你不想要可以跳过。

Compulsory Dependencies强制依赖项

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

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

​ yum install cmake

​ yum install python-devel numpy

​ yum install gcc gcc-c++

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

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

​ yum install gtk2-devel

​ yum install libdc1394-devel

​ yum install ffmpeg-devel

​ yum install gstreamer-plugins-base-devel

Optional Dependencies可选择依赖项

Above dependencies are sufficient to install OpenCV in your fedora 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 😃

以上依赖项足以在你的Fedora计算机上安装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 these formats.

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

​ yum install libpng-devel

​ yum install libjpeg-turbo-devel

​ yum install jasper-devel

​ yum install openexr-devel

​ yum install libtiff-devel

​ yum install libwebp-devel

Several OpenCV functions are parallelized with Intel’s Threading Building Blocks (TBB). But if you want to enable it, you need to install TBB first. ( Also while configuring installation with CMake, don’t forget to pass -D WITH_TBB=ON. More details below.)

一些OpenCV功能和英特尔的线性构建模块(TBB)并行的。但是如果你想启用它,需要先安装TBB(另外在用CMake配置安装的时候,别忘了用-D WITH_TBB=ON 。下面还有更多细节。)

​ yum install tbb-devel

OpenCV uses another library Eigen for optimized mathematical operations. So if you have Eigen installed in your system, you can exploit it. ( Also while configuring installation with CMake, don’t forget to pass -D WITH_EIGEN=ON. More details below.)

OpenCV用另一个库Eigen来优化数学操作。如果你的系统安装了Eigen库,你可以利用它。(另外在用CMake配置安装的时候,别忘了用-D WITH_EIGEN=ON 。下面还有更多细节。)

​ yum install eigen3-devel

If you want to build documentation ( Yes, you can create offline version of OpenCV’s complete official documentation in your system in HTML with full search facility so that you need not access internet always if any question, and it is quite FAST!!! ), you need to install Doxygen (a documentation generation tool).

如果你想构建documenation(你可以使用完整的搜素功能以HTML格式在你的系统创建离线的OpenCV的完整官方文档,这样,你有什么问题的话也不需要网络,并且相当快速)。你需要安装Doxygen(一种文档生成工具)。

​ yum install doxygen

Downloading OpenCV

Next we have to download OpenCV. You can download the latest release of OpenCV from sourceforge site. Then extract the folder.

Or you can download latest source from OpenCV’s github repo. (If you want to contribute to OpenCV, choose this. It always keeps your OpenCV up-to-date). For that, you need to install Git first.

接下来我们必须下载OpenCV.你可以从 sourceforge site下载最新版本。然后解压缩文件夹。

或者你可以从OpenCV的github repo下载最新的源代码。(如果你想要对OpenCV做出贡献,选择这种方式,因为这样你的OpenCV将一直更新)。为此,你需要先安装GIT.

​ yum install git

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

It will create a folder OpenCV in home directory (or the directory you specify). The cloning may take some time depending upon your internet connection.

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

它会在主目录(或你指定的目录)创建一个OpenCV文件夹。克隆会花一些时间,这取决于你的网络连接状态。

现在打开一个终端窗口并导航到下载的OpenCV文件夹。创建一个新的构建文件夹并导向它。

​ mkdir build

​ cd build

Configuring and Installing配置和安装

Now we have installed 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. Below command is normally used for configuration (executed from build folder).

现在我们已经安装好了需要的依赖库,让我们开始安装OpenCV。安装需要用CMake来配置。它阐明了哪一个模块需要安装,安装路径,哪一个额外的库要被用到,文档和例子是否要被编译等等。下面的命令通常被用来配置(从构建文件夹来执行)。

​ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local …

It specifies that build type is “Release Mode” and installation path is /usr/local. Observe the -D before each option and … at the end. In short, this is the format:

它阐明了构建模式是“Release Mode”,并且安装路径是 /usr/local。仔细观察 在每个选项之前的-D和 在末尾的… 。简而言之,这就是格式:

​ cmake [-D ] [-D ] …

You can specify as many flags you want, but each flag should be preceded by -D.

你可以指定任意数量的标志,但是每一个标志前面必须有-D。

So in this tutorial, we are installing OpenCV with TBB and Eigen support. We also build the documentation, but we exclude Performance tests and building samples. We also disable GPU related modules (since we use OpenCV-Python, we don’t need GPU related modules. It saves us some time).

(All the below commands can be done in a single cmake statement, but it is split here for better understanding.)

所以在这个教程中,我们在TBB 和 Eigen的支持下安装OpenCV。我们也构建了文档,但是不包括性能测试和构建示例。我们也不使用GPU相关模块(因为我们使用的是OpenCV-Python,我们不需要GPU的相关模块。这也节约了我们的时间)。

  • Enable TBB and Eigen support:

    启用TBB和Eigen支持:

    cmake -D WITH_TBB=ON -D WITH_EIGEN=ON …

  • Enable documentation and disable tests and samples

    启用文档,禁用测试和示例。

    cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF …

  • Disable all GPU related modules.

    禁用所有GPU 相关模块。

    cmake -D WITH_OPENCL=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF …

  • Set installation path and build type

    设置安装路径和构建类型。

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local …

    Each time you enter cmake statement, it prints out the resulting configuration setup. In the final setup you got, make sure that following fields are filled (below is the some important parts of configuration I got). These fields should be filled appropriately in your system also. Otherwise some problem has happened. So check if you have correctly performed above steps.

    每次你输入Cmake语句,它都会打印出结果配置设置。在完成的最终设置中,保证填写以下字段(以下是我获得的一些重要的配置)。这些字段也应该在你的系统中被合适地填写。否则会出现问题。所以检查你是否正确地操作了以上步骤。

    – GUI:

    – GTK+ 2.x: YES (ver 2.24.19)

    – GThread : YES (ver 2.36.3)

    – Video I/O:

    – DC1394 2.x: YES (ver 2.2.0)

    – FFMPEG: YES

    – codec: YES (ver 54.92.100)

    – format: YES (ver 54.63.104)

    – util: YES (ver 52.18.100)

    – swscale: YES (ver 2.2.100)

    – gentoo-style: YES

    – GStreamer:

    – base: YES (ver 0.10.36)

    – video: YES (ver 0.10.36)

    – app: YES (ver 0.10.36)

    – riff: YES (ver 0.10.36)

    – pbutils: YES (ver 0.10.36)

    – V4L/V4L2: Using libv4l (ver 1.0.0)

    – Other third-party libraries:

    – Use Eigen: YES (ver 3.1.4)

    – Use TBB: YES (ver 4.0 interface 6004)

    – Python:

    – Interpreter: /usr/bin/python2 (ver 2.7.5)

    – Libraries: /lib/libpython2.7.so (ver 2.7.5)

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

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

    Many other flags and settings are there. It is left for you for further exploration.

    还有很多其他地标志和设置在这里。它们留给你进行更进一步的操作。

Now you build the files using make command and install it using make install command. make install should be executed as root.

现在你用make命令行构建文件并用make install命令行安装。应该以root的身份来执行make install。

​ make

​ su

​ make install

Installation is over. All files are installed in /usr/local/ folder. But to use it, your Python should be able to find OpenCV module. You have two options for that.

安装完毕。所有的文件都被安装到/usr/local/文件夹。但想要用它,你的Python应该要能够找到OpenCV模块。你有两种选择达到那一步。

  1. Move the module to any folder in Python Path//将模块移动到Python路径的任意文件夹。

    : Python path can be found out by entering

    import sys; print(sys.path)
    

    在Python终端输入 import sys;print(sys.path)可以找到Python的路径。

    in Python terminal. It will print out many locations.会打印出很多位置。

    Move /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example,//将/usr/local/lib/python2.7/site-packages/cv2.so 移动到任一文件夹。举个例子,

    su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages

    But you will have to do this every time you install OpenCV.但每次你安装OpenCV都必须操作一遍。

  2. Add /usr/local/lib/python2.7/site-packages to the PYTHON_PATH
    It is to be done only once. Just open/.bashrc and add following line to it, then log out and come back.

    export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

    Thus OpenCV installation is finished. Open a terminal and try ‘import cv2 as cv’.

    将/usr/local/lib/python2.7/site-packages 移动到PYTHON_PATH:只需完成一次。打开/.bashrc并加入下一行,然后注销返回。

    export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

    这样OpenCV的安装就完成了。打开终端,试一下“import cv2 as cv

To build the documentation, just enter following commands:

为了构建文档,输入下面命令:

​ make doxygen

Then open opencv/build/doc/doxygen/html/index.html and bookmark it in the browser.

然后打开opencv/build/doc/doxygen/html/index.htm并在浏览器里加入书签。

Additional Resources

Exercises

  1. Compile OpenCV from source in your Fedora machine.

    在你的Fedora电脑里从源文件编译OpenCV。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值