ubuntu16.04+opencv3.4.3+opencv_contrib-3.4.3

1.安装依赖项

[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

2.下载opencv源码
https://opencv.org/releases/page/3/
找到3.4.3下载Sources

然后解压

$ unzip opencv-3.4.3.zip

3.开始编译

$ cd opencv-3.4.3
$ mkdir build
$ cd build
$ cmake ..
$ make               // 时间比较漫长
$ sudo make install  // 必须加sudo

4.将OpenCV的库添加到路径

$ sudo gedit /etc/ld.so.conf.d/opencv.conf 

执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加

/usr/local/lib

执行如下命令使得刚才的配置路径生效:

$ sudo ldconfig 

5.配置bash

$ sudo gedit /etc/bash.bashrc

在末尾追加:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH

保存,执行如下命令使得配置生效:

$ source /etc/bash.bashrc

更新:

$ sudo updatedb

6.小程序测试
找到 opencv-3.4.3/samples/cpp/example_cmake 目录下,官方已经给出了一个cmake的example,我们可以拿来测试下。按顺序执行:

$ cmake .
$ make
$ ./opencv_example

即可看到打开了摄像头,在左上角有一个hello opencv ,即表示配置成功。

7.编译opencv_contrib-3.4.3
去github上下载对应的opencv_contrib版本
https://github.com/opencv/opencv_contrib/releases?after=3.4.4

下载完成,解压,将名字改为opencv_contrib并放入opencv-3.4.3中
编译,参照opencv编译

$ cd opencv-3.4.3
$ mkdir build //如果之前已经编译过opencv,这一步省略
$ cd build
$ cmake -D OPENCV_EXTRA_MODULES_PATH=/xxxpath/opencv-3.4.3/opencv_contrib/modules/ ..
$ make // 时间比较漫长,或者make -j8
$ sudo make install // 必须加sudo

中间有一些error
error1:opencv_contrib/modules/xfeatures2d/src/缺少以下文件,网上搜索下载,放进该目录中
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
boostdesc_lbgm.i
vgg_generated_48.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_120.i

error2:

/xxxpath/opencv-3.4.3/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
/xxxpath/opencv-3.4.3/modules/stitching/src/precomp.hpp:91:42: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
打开这两个文件,修改如下(/xxxpath/为自己电脑中实际的路径)
//#  include "opencv2/xfeatures2d/cuda.hpp"
#  include "/xxxpath/opencv-3.4.3/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp"

/xxxpath/opencv-3.4.3/modules/stitching/src/matchers.cpp:52:35: fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录
打开这个文件,修改如下
//#include "opencv2/xfeatures2d.hpp"
#include "/xxxpath/opencv-3.4.3/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp"

/xxxpath/opencv-3.4.3/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp:43:43: fatal error: opencv2/xfeatures2d/nonfree.hpp: 没有那个文件或目录
打开这个文件,修改如下
//#include "opencv2/xfeatures2d/nonfree.hpp"
#include "/xxxpath/opencv-3.4.3/opencv_contrib/modules/xfeatures2d/include/opencv2/xfeatures2d/nonfree.hpp"

然后再次进行
$ make // 时间比较漫长,或者make -j8
$ sudo make install // 必须加sudo

再次操作一遍第四步(4.将OpenCV的库添加到路径)不知道是否必要

参照
https://docs.opencv.org/3.4.3/d7/d9f/tutorial_linux_install.html
https://www.jianshu.com/p/f646448da265

posted on 2020-12-09 15:55  JJ_S  阅读(325)  评论(0编辑  收藏  举报