ubuntu18 github安装opencv4.2.0

1.环境准备

安装git,增加ssh key

1. sudo apt install git

2. ssh-keygen -t rsa -C "xx@xx.com"

3. 打开github添加key

拷贝.ssh/id_rsa.pub中内容添加到github页面

找到opencv github代码仓,fork个人仓

opencv:https://github.com/opencv/opencv

opencv_contrib:https://github.com/opencv/opencv_contrib

下载github代码时,需要主要opencv和opencv_contrib版本要保持一致,通过tag标签控制下载版本。

Fork完成后进入个人仓,获取gitclone路径:

gitclone下载opencv代码到本地

git clone --branch 【tag标签】【代码仓路径】

git clone --branch 4.2.0 git@github.com:xx/opencv.git

git clone --branch 4.2.0 git@github.com:xx/opencv_contrib.git

2.编译构建opencv

安装依赖库

sudo apt-get install build-essential 
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

解决下文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 apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

编译opencv

提前下载ippicv文件,否则执行cmake的时候会报错

https://github.com/opencv/opencv_3rdparty/tree/ippicv/master

git clone https://gitee.com/theonly-so/opencv_3rdparty.git -b ippicv/master_20180723
cd opencv
mkdir 3rdlibs && cp opencv_3rdparty/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz opencv/3rdlibs

vim opencv/3rdparty/ippicv/ippicv.cmake
#修改“https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}ippicv/”为
"file:/home/qq/workspace/opencv_study/src_codes/opencv/3rdlibs"

效果: 

解决编译过程中缺少boostdesc_bgm.i文件问题

#下载离线文件
https://gitee.com/xu_huncan/opencv_3rdparty/tree/contrib_xfeatures2d_boostdesc_20161012/
https://gitee.com/xu_huncan/opencv_3rdparty/tree/contrib_xfeatures2d_vgg_20160317/
https://gitee.com/xu_huncan/opencv_3rdparty/tree/contrib_face_alignment_20170818/

#拷贝离线文件到编译目录
cd opencv
mkdir -p build/downloads/xfeatures2d
cp boots*.i build/downloads/xfeatures2d/
cp vgg*.i build/downloads/xfeatures2d/

执行cmake 

cd opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES CMAKE_INSTALL_PREFIX=/usr/local/opencv -D WITH_GTK=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

cmake参数解释:
#CMAKE_BUILD_TYPE=Release                  生成发布版本
#OPENCV_GENERATE_PKGCONFIG=YES             创建 PKGCONFIG文件
#CMAKE_INSTALL_PREFIX =/usr/local/opencv   安装路径
#WITH_GTK=ON                               ubuntu 上显示窗口必须加上这个

sudo make -j8

编译完成无报错,执行安装操作

sudo make install

安装完成,修改配置

#添加opencv lib路径
sudo vi /etc/ld.so.conf.d/opencv.conf
#末尾添加/usr/local/lib, 保存后退出,执行ldconfig生效
sudo ldconfig

#设置环境变量
sudo vi /etc/bash.bashrc
#末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
#添加完成后保存,soure生效
source /etc/bash.bashrc

3.测试opencv是否安装成功

进入opencv/samples/cpp目录下,使用opencv自带的例子测试

cd opencv/samples/cpp/example_cmake
cmake .
make
./opencv_example

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页