ubuntu 源码安装 opencv + opencv_contrib

教程

  • 安装
  • 问题
  • 现成下载

安装

安装前一定先看一遍官方教程(Installation in Linux,opencv_contrib)和以下全文,尤其是最后的问题
以opencv 4.2.0版本为例,home下的Downloads文件夹里有opencv-4.2.0opencv_contrib-masteropencv_need三个文件夹,分别存放着opencv 4.2.0的源码、opencv contrib的源码和问题三中自己手动下载的所有文件

#安装所有必须的软件和依赖项。如果显示E: Unable to locate package xxxx,把镜像源更换为清华的应该就能解决。
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
#可选项。若libjasper-dev不能安装,参考问题一。除了python的两个,其他的我全装了(都是处理各种图片格式的库)
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

#获取源文件,git或者用IDM直接下载后传给linux解压
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

#进入opencv的文件夹
cd opencv-4.2.0/
mkdir build
cd build

#如果报错,在-D后加个空格;
#-DOPENCV_EXTRA_MODULES_PATH=后面跟的是opencv_contrib的路径,因为我的opencv_contrib-master和opencv-4.2.0两个文件夹在同一个文件夹下
#-DBUILD_opencv_java和-DBUILD_opencv_python是用来选择是否要java和python的
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-master/modules -DBUILD_opencv_java=OFF -DBUILD_opencv_python=OFF ..
#若显示	-- Configuring done
#		-- Generating done
#则进行下一步,-j7表示多线程
make -j7 
sudo make install

问题

问题一:安装可选依赖包libjasper-dev的时候,显示E: Unable to locate package libjasper-dev

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo add-apt-repository --remove "deb http://security.ubuntu.com/ubuntu xenial-security main"
#其中libjasper1是libjasper-dev的依赖包

参考教程:Ubuntu18.04下安装OpenCv依赖包libjasper-dev无法安装的问题

问题二:如果在cmake编译的时候,显示No package ‘gtk±3.0’ found
ubuntu 源码安装 opencv + opencv_contrib_第1张图片

#以防万一,我觉得还是装了比较好
sudo apt-get install libgtk-3-dev

问题三:如果在cmake编译的时候,需要到Github下载一些文件,但是下载不下来(终端代理可能也没用,反正我不行),显示Download failed: 35; "SSL connect error",例下图
ubuntu 源码安装 opencv + opencv_contrib_第2张图片

  • 方法一:需要手动下载每一个文件,并修改cmake文件中的文件路径。步骤如下:
  1. 打开下载所需文件对应的cmake文件。文件路径一般在报错下面的Call Stack (most recent call first)紧跟的第一行。例如
    ubuntu 源码安装 opencv + opencv_contrib_第3张图片

  2. 找到cmake文件中的需要下载文件的URL,例如"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"。其中的${IPPICV_COMMIT}在同一个文件中有定义,为一长串的字母数字组合的字符串。最后,在URL的最后加上文件名,同样也有定义,针对不同的系统与环境会有所不同;或者在cmake编译的时候看它的输出也行。
    得到最终的下载连接为 "https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz"
    ubuntu 源码安装 opencv + opencv_contrib_第4张图片

  3. IDM下载完后传给Linux,放到特定的文件夹下,比如/home/orz/Downloads/opencv_need/

  4. 把所有的.cmake文件里的"https://。。。"改成"file:///home/orz/Downloads/opencv_need/"

"file://{刚刚手动下载的IPP文件的上一级目录}/"

最后,我将自己所需手动下载的所有东西整理成了一个表格,仅供参考:

文件名 .cmake文件位置 下载连接
IPPICV /home/orz/Downloads/opencv-4.2.0/3rdparty/ippicv/ippicv.cmake https://raw.githubusercontent.com/opencv/opencv_3rdparty/32e315a5b106a7b89dbed51c28f8120a48b368b4/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz
boostdesc_bgm.i /home/orz/Downloads/opencv_contrib-master/modules/xfeatures2d/cmake/download_boostdesc.cmake https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm.i
boostdesc_bgm_bi.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_bi.i
boostdesc_bgm_hd.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_bgm_hd.i
boostdesc_binboost_064.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_064.i
boostdesc_binboost_128.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_128.i
boostdesc_binboost_256.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_binboost_256.i
boostdesc_lbgm.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/34e4206aef44d50e6bbcd0ab06354b52e7466d26/boostdesc_lbgm.i
vgg_generated_48.i /home/orz/Downloads/opencv_contrib-master/modules/xfeatures2d/cmake/download_vgg.cmake https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_48.i
vgg_generated_64.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_64.i
vgg_generated_80.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_80.i
vgg_generated_120.i 同上 https://raw.githubusercontent.com/opencv/opencv_3rdparty/fccf7cd6a4b12079f73bbfb21745f9babcd4eb1d/vgg_generated_120.i
face_landmark_model.dat /home/orz/Downloads/opencv_contrib-master/modules/face/CMakeLists.txt https://raw.githubusercontent.com/opencv/opencv_3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/face_landmark_model.dat

PS: 尝试了opencv 3.4.x对应的opencv_contrib,发现所有需要手动下载的文件跟opencv 4.x的一致,所以opecv_need文件夹直接共用就好了。

参考教程:手动安装OpenCV下的IPP加速库

  • 方法二:对于不需要的dnnvgg,可以在cmake编译的时候,在命令后面加上BUILD_opencv_*参数,比如:
#我没用过这个方法,不知道行不行得通
cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules -DBUILD_opencv_legacy=OFF <opencv_source_directory>

参考教程:opencv_contrib

问题四:报错Duplicated modules NAMES has been found,如下图
ubuntu 源码安装 opencv + opencv_contrib_第5张图片
因为版本不匹配!!!opencv contrib也是分版本的!!!在从github上下载opencv contrib的时候,需要选择banch。master指的是最新的版本,即opencv 4.x,3.4应该指的是opencv 3.4.x的(不懂了,那3.4.x之前的版本咋办?),如图:
ubuntu 源码安装 opencv + opencv_contrib_第6张图片

现成下载

我把最终的OpenCV打包了,需要的自取:
链接:https://pan.baidu.com/s/1p4wP1kJUmmzXlXghKsIaRg 提取码:604w
链接:https://pan.baidu.com/s/1d6A8pZ3jeOCO5L-1XFgSug 提取码:5kml

  • 使用方法:
  1. 先安装所需的其他软件和依赖包
#安装所有必须的软件和依赖项
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
#可选项。若libjasper-dev不能安装,参考问题一。除了python的两个,其他的我全装了(都是处理各种图片格式的库)sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
  1. 将压缩包解压到任意位置,然后进入opencv-x.x.x文件夹中的build文件夹,执行命令
#如果报错,在-D后加个空格;-DOPENCV_EXTRA_MODULES_PATH=后面跟的是opencv_contrib的路径,因为我的opencv_contrib-master和opencv-4.2.0两个文件夹在同一个文件夹下
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-master/modules -DBUILD_opencv_java=OFF -DBUILD_opencv_python=OFF .. 
#若显示	-- Configuring done
#		-- Generating done
#则进行下一步,-j7表示多线程
make -j7 
sudo make install

PS压缩包内的Read.me与这里有点出入,以这里的为准。因为更新网盘压缩包里的文件太麻烦了。

你可能感兴趣的:(ubuntu 源码安装 opencv + opencv_contrib)