Ubuntu系统入门教程-ubuntu20.04安装opencv4.5和opencv-contrib-4.5
Ubuntu系统入门教程-ubuntu20.04安装opencv4.5和opencv-contrib-4.5
说明:
- 本教程主要介绍如何在Ubuntu 20.04 上安装opencv4.5和opencv-contrib-4.5
- 测试环境:Ubuntu 20.04.3LTS + opencv4.5 + opencv-contrib-4.5
操作步骤
- 查看本机的opencv版本
$ opencv_version
4.2.0
由于此前安装了ROS Noetic,故自带了opencv4.2.0,现在源码安装opencv4.5.0
安装依赖项
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev build-essential mlocate
$ sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
$ sudo apt update
$ sudo apt install libjasper1 libjasper-dev
- 下载opencv4.5
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.0 opencv_contrib
$ mv opencv_contrib opencv
- 编译
$ cd ~/opencv && mkdir build && cd build
$ cmake ..
$ sudo make
$ sudo make install
- 将opencv库添加到系统路径
$ sudo vim /etc/ld.so.conf.d/opencv.conf
## 此前没有装过的opencv的话,其会新建一个文件,添加以下内容后保存退出
/usr/local/lib
- 保存后执行以下命令使配置路径生效
$ sudo ldconfig
- 之后配置bash
$ sudo vim /etc/bash.bashrc
## 添加以下内容在文本末尾后保存退出
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
- 保存,执行如下命令使得配置生效
$ source /etc/bash.bashrc
- 然后更新配置
$ sudo updatedb
- 测试是否安装成功
$ opencv_version
4.5.0
$ python3 -c "import cv2; print(cv2.__version__)"
4.5.0
参考资料
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号