Ubuntu 安装编译 opencv

Ubuntu 安装编译 opencv

安装环境准备

配置 Ubuntu 镜像源

由于部分镜像源并没有将软件包的版本更新到最新,国外官方镜像源访问与下载速度较慢,所以需要更新镜像源为以下几种:

阿里镜像源


sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update 

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

EOF"

清华大学镜像源


sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update 

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

EOF"

163镜像源


sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update 

deb http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ jammy main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ jammy-security main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ jammy-updates main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ jammy-backports main restricted universe multiverse

EOF"

中科大镜像源


sudo bash -c "cat << EOF > /etc/apt/sources.list && apt update 

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse

deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse

deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

EOF"

系统可以同时包括四种镜像源,确保能够及时搜索到所有的软件的最新版本

环境准备

安装 opencv Ubuntu 版本最低要求:18.04


# c、c++ 编译环境准备

sudo apt-get install gcc g++ cmake wget unzip

# 安装依赖

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

如果在安装过程中出现如下报错:


下列软件包有未满足的依赖关系:
libgtk2.0-dev : 依赖: libgdk-pixbuf2.0-dev (>= 2.21.0) 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。 

就是说当前镜像源网站的软件包版本不是最新版本,需要进行换源,保证新的镜像源有符合安装版本的安装包

下载与安装

下载opencv

opencv 可以从靠谱的镜像源进行下载,或者直接通过命令进行下载

opencv 下载镜像源:https://gitcode.net/opencv 这是 opencv 官方为国人提供的镜像下载网址(有条件的使用官网下载)

命令下载:

  
git clone https://gitcode.net/opencv/opencv  # 此网站为国内网站,可以放心下载

使用 unzip 命令解压 opencv,命令如下:


upzip xxx.zip # xxx是你的压缩包名称

解压完成如下图

然后将文件移动到 opencv 文件夹中


mv xxx opencv # xxx 是你的文件加名称

安装 opencv

创建 build 文件夹并进入

命令如下:


cd opencv
mkdir build
cd build

创建成功如下图:

使用 cmake 调配参数


sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

或者可以通过 cmake-gui 进行可视化安装,代码如下:(本篇不进行可视化安装演示,仅给出代码)


sudo apt-get install cmake-qt-gui
sudo cmake-gui

cmake 配置成功如图:

进行编译


sudo make -j线程数

编译完成标志,如下图:

编译完成就可以直接开始安装

sudo make install

安装完成如下图:

添加路径

用 vim 或 gedit 打开配置文件进行配置,配置文件路径:/etc/ld.so.conf


sudo vim /etc/ld.so.conf

# 打开后添加如下代码:

include /usr/local/lib

保存并关闭后,运行如下代码,确定并执行配置:


运行sudo ldconfig

环境配置

修改bash.bashrc文件:


sudo vim /etc/bash.bashrc

在文件末尾添加如下内容:


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

如下图所示:

image

然后执行如下命令,对该文件进行类似重载的操作:


source /etc/bash.bashrc
# 注意:次命令不能用 sudo 执行

安装完成

最后输入以下命令,如果成功显示 opencv 版本号,即编译配置成功:


pkg-config opencv --modversion

成功显示如下图

image

posted @ 2022-09-16 22:10  ppqppl  阅读(141)  评论(0编辑  收藏  举报