Ubuntu 20.04安装配置opencv4.2.0及踩坑
所需各种文件链接在此
链接:https://pan.baidu.com/s/1kf_cljseuUbq7MqzBg-TQw
提取码:ooh2
一、在安装配置opencv之前如果没换源的建议换源,不然安装各种依赖库应该会很慢。
换源
1.备份 sudo cp -ra /etc/apt/sources.list /etc/apt/sources.list.bak
2.打开文件 sudo vi /etc/apt/sources.list
3.复制源(这是ubuntu20.04的阿里源,需要其它的请到官网找)
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
4.保存退出
5.sudo apt-get update
6.sudo apt-get upgrade
二、opencv4.2.0配置
1.安装下面的所有依赖包
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff5-dev libswscale-dev pkg-config
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev git
若有些库安装不了:
E: Unable to locate package …
执行:
sudo add-apt-repository “deb http://security.ubuntu.com/ubuntu xenial-security main”
sudo apt-get update
然后再执行安装不了的那些库
2.解压
将源码包放到Ubuntu某目录下(创建一个opencv文件夹),解压
sudo unzip opencv_contrib-4.2.0.zip
sudo unzip opencv-4.2.0.zip
将ippicv_2019_lnx_intel64_general_20180723.tgz这个文件一同放到该目录,防止后面执行cmake因为网络无法下载
得到解压目录:opencv-4.2.0
cd opencv-4.2.0/
创建目录build并进入build
sudo mkdir build && cd build
执行
sudo cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/ -D OPENCV_EXTRA_MODULES_PATH=/home/hzb/opencv/opencv_contrib-4.2.0/modules/ ..
CMAKE_BUILD_TYPE:指定编译发布版本
CMAKE_INSTALL_PREFIX:指定生成的库文件目录等
OPENCV_EXTRA_MODULES_PATH :指定opencv_contrib-4.2.0存放位置
OPENCV_GENERATE_PKGCONFIG=YES:生成pc文件
编译
make
出错1:opencv-4.2.0/opencv_contrib-4.2.0/modules/xfeatures2d/src/boostdesc.cpp:654:37: fatal error: boostdesc_bgm.i:
解决:将上面的百度网盘链接里所有.i文件复制到opencv_contrib-4.2.0/modules/xfeatures2d/src/
出错2:opencv-4.2.0/opencv_contrib-4.2.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp:7:82: fatal error: features2d/test/test_detectors_invariance.impl.hpp:
解决:
上图这三个文件拷贝到/home/hzb/opencv/opencv_contrib-4.2.0/modules/xfeatures2d/test,并修改/home/hzb/opencv/opencv_contrib-4.2.0/modules/xfeatures2d/test目录下的test_features2d.cpp和test_rotation_and_scale_invariance.cpp(两个文件都要改)里面的头文件
成功后
执行sudo make install
三、配置环境
1.在/etc/ld.so.conf.d/目录下创建opencv.conf
sudo vi /etc/ld.so.conf.d/opencv.conf
将库路径“/usr/local/lib”写入opencv.conf文件,保存。
执行命令使其生效:
sudo ldconfig
2.配置bash:
在/etc/bash.bashrc的末尾加上以下两行:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
pkg-config opencv4 --modversion #查看版本
/usr/local/lib/pkgconfig为opecv4.pc文件的路径
四、测试
进入/home/hzb/opencv/opencv-4.2.0/samples/cpp/example_cmake目录
执行sudo cmake ./
sudo make
在该目录下产生opencv_example文件
执行./opencv_example
情景1.无摄像头
情景2.无摄像头
错误1:[ WARN:0] global /home/xgl/opencv-4.3.0/modules/videoio/src/cap_v4l.cpp (1004) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
解决:虚拟机->设置->USB控制器,更改一下兼容性,如果是2.0,则更改为3.0。
错误2:Failed to load module “canberra-gtk-module”
解决:sudo apt install libcanberra-gtk-module -y