How to install OpenCV 3.x in Linux

本文提供了在Linux系统中安装OpenCV 3.x的详细指南,包括依赖包的安装,从sourceforge下载源码或通过Git克隆,以及使用CMake进行编译和安装。涉及的依赖包有GCC、CMake、Git、GTK+、pkg-config、Python和Numpy等。安装流程涵盖创建临时目录、配置CMake参数、构建和安装OpenCV,以及可选的文档生成和测试运行。

Installation in Linux


依赖包安装

  • GCC 4.4.x or later
  • CMake 2.8.7 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

以上包可以使用终端,按照下面的命令安装;或者使用Synaptic Manager安装:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
 

方式1


OpenCV源代码下载

可以使用 sourceforge 上最新稳定版的OpenCV,或者 从 Git repository上下载.

下载最近稳定版OpenCV下载

 Git分支下载OpenCV

Launch Git client and clone OpenCV repository 

 If you need modules from OpenCV contrib repository then clone it too.


In Linux it can be achieved with the following command in Terminal:

cd ~/<my_working _directory>
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git 

CMake命令行模式编译


  1. Create a temporary directory, which we denote as <cmake_build_dir>, where you want to put the generated Makefiles, project files as well the object files and output binaries and enter there.

    For example

    1 cd ~/opencv
    2 mkdir build
    3 cd build
  2. Configuring. Run cmake [<some optional parameters>] <path to the OpenCV source directory>

    For example

    1 cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

    or cmake-gui

    • set full path to OpenCV source code, e.g. /home/user/opencv
    • set full path to <cmake_build_dir>, e.g. /home/user/opencv/build
    • set optional parameters
    • run: “Configure”
    • run: “Generate”
  3. Description of some parameters
    • build type: CMAKE_BUILD_TYPE=Release\Debug
    • to build with modules from opencv_contrib set OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib/modules/>
    • set BUILD_DOCS for building documents
    • set BUILD_EXAMPLES to build all examples
  4. [optional] Building python. Set the following python parameters:
    • PYTHON2(3)_EXECUTABLE = <path to python>
    • PYTHON_INCLUDE_DIR = /usr/include/python<version>
    • PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python<version>
    • PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython<version>.so
    • PYTHON2(3)_NUMPY_INCLUDE_DIRS = /usr/lib/python<version>/dist-packages/numpy/core/include/
  5. [optional] Building java.
    • Unset parameter: BUILD_SHARED_LIBS
    • It is useful also to unset BUILD_EXAMPLES, BUILD_TESTS, BUILD_PERF_TESTS - as they all will be statically linked with OpenCV and can take a lot of memory.
  6. Build. From build directory execute make, recomend to do it in several threads

    For example

    1 make -j7 # runs 7 jobs in parallel
  7. [optional] Building documents. Enter <cmake_build_dir/doc/> and run make with target "html_docs"

    For example

    1 cd ~/opencv/build/doc/
    2 make -j7 html_docs
  8. To install libraries, from build directory execute
    1 sudo make install
  9. [optional] Running tests

    For example

    1 git clone https://github.com/Itseez/opencv_extra.git
    • set OPENCV_TEST_DATA_PATH environment variable to <path to opencv_extra/testdata>.
    • execute tests from build directory.

    For example

    1 <cmake_build_dir>/bin/opencv_test_core


Note

If the size of the created library is a critical issue (like in case of an Android build) you can use the install/strip command to get the smallest size as possible. The  stripped  version appears to be twice as small. However, we do not recommend using this unless those extra megabytes do really matter.

方式2

 编译工具安装


终端下, root 权限执行以下命令:

apt-get install cmake cmake-gui


 启动cmake-gui

以OpenCV3.1为例子,如上图设置源代码路径(比如:/media/Data1/install_pkg/opencv-3.1.0)编译结果路径(比如:/media/Data1/install_pkg/build_opencv3.1)

然后点击“Configure”按钮。选择使用Makefile生成的系统平台。



点击“Finish”,等待(中间可能存在下载依赖包的过程,会比较慢)环境测试完成,进行编译配置设置。


根据需要设置:

1)编译后的安装路径

2)编译的类型,静态库或者动态库形式

3)选择需要安装的包或者模块,是否支持GPU等

点击“generate”生成Makefile。


然后,终端下转到/media/Data1/install_pkg/build_opencv3.1下,执行以下命令:make进行编译;然后,执行make install 进行安装(root目录的话需要root权限)。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值