如何编译 opencv3 和 opencv_contrib(Linux)

1. 下载 Source code zip 压缩包

从下面网址,选择 opencv 3.3.0 Source code 下载
https://github.com/opencv/opencv/releases

从下面网址,选择下载与 opencv 3.3.0 对应的 opencv_contrib 3.3.0
https://github.com/opencv/opencv_contrib/releases

注意:opencv和opencv_contrib 版本必须保持一致,否则可能会出现编译错误。

2. 准备安装目录,并解压源码

2.1 准备安装目录
本人准备将 opencv 安装到 ~/libs/opencv-trunk/release/installed 目录,没有安装到默认目录,其好处,一是节省根目录空间(前提是 /home 和 / 不是一个分区);二是可以安装多个版本的 opencv,详见参考资料 How to Have Multiple Versions of OpenCV Side by Side 。

$ mkdir -p ~/libs/opencv-trunk/release/installed

2.2 解压 opencv zip 压缩包
将下载的 zip 压缩包复制到 ~/opencv 目录,并解压。

$ cd ~/opencv
$ unzip opencv-3.3.0.zip
$ unzip opencv_contrib-3.3.0.zip

3. 配置

$ cd ~/opencv/opencv-3.3.0
$ mkdir build && cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/home/klchang/libs/opencv-trunk/release/installed -DCMAKE_BUILD_TYPE="Release" -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.3.0/modules ..

注意,

1)这里的配置是将 opencv 3.3.0 和 opencv_contrib 3.3.0 一起编译,如果只需编译 opencv 3.3.0,把 "-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.3.0/modules" 去掉即可;
2)如果需要更改安装位置或按默认位置安装,则 需要更改 "-DCMAKE_INSTALL_PREFIX="后的目录,或将此项配置删除。

4. 编译并安装

$ make -j4
$ make install

注:在重新编译时,无需执行 make clean,因为这样会节省再次编译的时间,编译系统会自动利用已经编译好的中间文件,而对于编译通过的部分,无需再重新编译。

5. 问题与解决方法

问题一: 

fatal error: unsupported/Eigen/MatrixFunctions: No such file or directory

解决方法:

复制 eigen-3.3.4/unsupported 目录到 /usr/local/include,然后删除 unsupported/ 目录下,除 Eigen/ 目录外,所有的目录和文件。

$ sudo cp -r eigen-3.3.4/unsupported /usr/local/include
$ cd /usr/local/include/unsupported
$ shopt -s extglob
$ sudo rm -rf !(Eigen)

然后,重新编译 opencv 。

问题二:

opencv_contrib/modules/ximgproc/src/dtfilter_cpu.inl.hpp:72:31: error: ‘traits’ has not been declared
 CV_Assert(guide.type() == traits::Type::value);

opencv_contrib/modules/ximgproc/src/dtfilter_cpu.inl.hpp:72:52: error: expected primary-expression before ‘>’ token
 CV_Assert(guide.type() == traits::Type::value);

opencv_contrib/modules/ximgproc/src/dtfilter_cpu.inl.hpp:72:53: error: ‘::value’ has not been declared
 CV_Assert(guide.type() == traits::Type::value);

解决方法:

这是由于编译的 opencv 版本与 opencv_contrib 版本不一致造成的,下载相同版本的 opencv 和 opencv_contrib,然后重新编译。

问题三:

/usr/bin/ld: /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_32 against `__pthread_key_create@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libgflags.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/sfm/CMakeFiles/opencv_sfm.dir/build.make:319: recipe for target 'lib/libopencv_sfm.so.3.3.0' failed
make[2]: *** [lib/libopencv_sfm.so.3.3.0] Error 1
CMakeFiles/Makefile2:13259: recipe for target 'modules/sfm/CMakeFiles/opencv_sfm.dir/all' failed
make[1]: *** [modules/sfm/CMakeFiles/opencv_sfm.dir/all] Error 2
Makefile:147: recipe for target 'all' failed
make: *** [all] Error 2

解决方法:

重新配置并安装 gflags library 。将 gflags 配置为 同时编译静态库和动态库,因为 opencv_contrib 的源文件编译过程中,同时使用其静态库和动态库。

$ cd gflags-master
$ mkdir build && cd build
$ cmake -DBUILD_SHARED_LIBS=ON -DBUILD_STATIC_LIBS=ON ..
$ make
$ sudo make install

然后,重新编译 opencv。

问题四:

[ 98%] Building CXX object modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o
Linking CXX shared library ../../lib/libopencv_sfm.so
/usr/bin/ld: cannot find -lgflags_static
collect2: error: ld returned 1 exit status
modules/sfm/CMakeFiles/opencv_sfm.dir/build.make:318: recipe for target 'lib/libopencv_sfm.so.3.3.0' failed
make[2]: *** [lib/libopencv_sfm.so.3.3.0] Error 1
CMakeFiles/Makefile2:13259: recipe for target 'modules/sfm/CMakeFiles/opencv_sfm.dir/all' failed
make[1]: *** [modules/sfm/CMakeFiles/opencv_sfm.dir/all] Error 2
Makefile:147: recipe for target 'all' failed
make: *** [all] Error 2

解决方法:

同上。

6. 参考资料

[1] Installation in Linux
http://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
[2] How to Have Multiple Versions of OpenCV Side by Side
http://code.litomisky.com/2014/03/09/how-to-have-multiple-versions-of-the-same-library-side-by-side/

7. 附录:本人使用源码包版本及百度网盘下载 

OpenCV 3.3.0 - https://pan.baidu.com/s/1bp2cq67

opencv_contrib 3.3.0 - https://pan.baidu.com/s/1nuKy4bB

eigen 3.3.4 - https://pan.baidu.com/s/1kUXtMEr

gflags 2.2.1 - https://pan.baidu.com/s/1miC9jTI

glog 0.3.3 - https://pan.baidu.com/s/1gfktlPD

ceres-solver 1.13.0 - https://pan.baidu.com/s/1c1Z9WPU

你可能感兴趣的:(opencv,linux,计算机视觉)