Ubuntu_16.04安装OpenCV的方法

本文记录在Ubuntu16.04 LTS操作系统中从源代码编译安装OpenCV的过程。

安装必需的软件包

1
2
3
[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源代码

1
2
3
cd ~/<my_working_directory>
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

使用CMake构建OpenCV

创建临时的build目录

1
2
3
4
5
6
7
cd ~/opencv
# 查看并检出OpenCV最新发布版
git tag
git checkout 4.1.0

mkdir build
cd build

创建配置文件

1
2
3
4
5
$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
# 可选,如使用opencv_contrib, 采用如下命令生成编译配置文件
$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
# 可选,构建示例程序和文档
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_EXAMPLES=1 -D BUILD_DOCS=1 ..

构建OpenCV

1
make -j3 # runs 3 jobs in parallel

安装OpenCV

1
sudo make install

参考链接

  1. Installation in Linux, by opencv.
  2. 4.0.0 does not produce pkgconfig file, by opencv.
  3. Pkg-config,by wikipedia.