在 Linux 中安装
这些步骤已针对 Ubuntu 10.04 进行了测试,但也应适用于其他发行版。
必需的包
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
获取 OpenCV 源代码
您可以使用最新的稳定 OpenCV 版本,也可以从我们的 Git 存储库中获取最新的快照。
Getting the Latest Stable OpenCV Version
Go to our downloads page.
Download the source archive and unpack it.
Getting the Cutting-edge OpenCV from the Git Repository
Launch Git client and clone OpenCV repository. If you need modules from OpenCV contrib repository then clone it too.
例如
cd ~/<my_working_directory>
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
Building OpenCV from Source Using CMake
创建一个临时目录,我们记为 ,您要在其中放置生成的 Makefile、项目文件以及目标文件和输出二进制文件并进入那里。
例如
cd ~/opencv
mkdir build
cd build
Configuring. Run cmake [<some optional parameters>] <path to the OpenCV source directory>
例如
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”
部分参数说明
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
[可选] 构建 python。设置以下python参数:
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/
[optional] Building java.
未设置参数:BUILD_SHARED_LIBS
取消设置 BUILD_EXAMPLES、BUILD_TESTS、BUILD_PERF_TESTS 也很有用 - 因为它们都将与 OpenCV 静态链接,并且会占用大量内存。
建造。从build目录执行make,建议多线程执行
例如
make -j7 # runs 7 jobs in parallel
[optional] Building documents. Enter <cmake_build_dir/doc/> and run make with target “html_docs”
例如
cd ~/opencv/build/doc/
make -j7 html_docs
要安装库,从构建目录执行
sudo make install
[optional] Running tests
从 OpenCV 额外存储库中获取所需的测试数据。
例如
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.
例如
<cmake_build_dir>/bin/opencv_test_core
致谢:OpenCV 安装页面。