ubuntu22.04 OpenCV4.6.0(c++)环境配置

ubuntu22.04 OpenCV4.6.0(c++)环境配置

前置依赖安装

sudo apt install -y g++ make wget unzip
sudo apt install -y libssl-dev build-essential
sudo apt install -y pkg-config

CMake安装

方法一(源码安装)

下载地址:https://cmake.org/download/
ubuntu22.04 OpenCV4.6.0(c++)环境配置_第1张图片
进入cmake目录cd cmake-***

执行:

./bootstrap

编译构造:

make

安装:

sudo make install

检查:
1.查看cmake版本: cmake --version
2.查看cmake位置: which cmake

方法二(省时)

sudo apt-get install -y cmake

下载OpenCV-4.6.0

官网链接
ubuntu22.04 OpenCV4.6.0(c++)环境配置_第2张图片

下载依赖

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

可能遇到的问题

下载依赖报错,镜像源问题。

下列软件包有未满足的依赖关系:
libc6-dev : 依赖: libc6 (= 2.27-3ubuntu1.6) 但是 2.35-0ubuntu3 正要被安装
依赖: libc-dev-bin (= 2.27-3ubuntu1.6)

参考博客:最新ubuntu22.04 下列软件包有未满足的依赖关系 解决方案

1.sudo gedit /etc/apt/sources.list
2.将如下内容复制进去:

#阿里源
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-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports 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

3.更新镜像源

sudo apt-get update
sudo apt-get upgrade

4.重复安装尝试

编译安装OpenCV

  1. 创建build目录
cd opencv-4.6.0
mkdir -p build
cd build

在这里插入图片描述

  1. 执行cmake命令:
cmake -DCMAKE_BUILD_TYPE=Release \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local ..
  1. 编译:
sudo make -j 4

使用4个线程进行编译,可以一定程度上加快编译速度

  1. 安装:
sudo make install

环境配置

参考博客

  1. 打开文件:
    sudo gedit /etc/ld.so.conf.d/opencv.conf
  2. 写入:
    /usr/local/lib
  3. 更新系统共享链接库:
    sudo ldconfig
  4. 打开bashrc文件:
    sudo gedit /etc/bash.bashrc
  5. 写入如下内容:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
  1. 更新,source /etc/bash.bashrc

配置pkg-config环境(补充)

opencv4.pc文件的默认路径:/usr/local/lib/pkgconfig/opencv4.pc.若此目录下没有,可以使用以下命令搜索 sudo find / -iname opencv4.pc
执行sudo gedit /etc/profile.d/pkgconfig.sh
可能为空文件,在文件中加入PKG_CONFIG_PATH下面一行代码

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

保存并退出,source激活source /etc/profile

验证是否安装成功

pkg-config --modversion opencv4

如下图,安装成功:
ubuntu22.04 OpenCV4.6.0(c++)环境配置_第3张图片

编写例程测试

新建文件夹digital
创建文件:

touch CMakeLists.txt
mkdir -p build
mkdir -p img
mkdir -p src

源码放在src目录下
ubuntu22.04 OpenCV4.6.0(c++)环境配置_第4张图片

demo.cpp

#include
#include 
#include

using namespace std;
using namespace cv;

Mat src;

int main(int argc, char ** argv)
{
        src = imread("/home/luffy/digital/img/luffy14.jpg");//这里是你的图片
        if (src.empty())
        {
		cout << "没有读取到图像" << endl;
		return -1;
        }
        imshow("hello", src);
        waitKey(0);
        return 0;
}

编写CMakeLists.txt文件

供参考:

cmake_minimum_required(VERSION 2.8)
project( digital )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( demo src/demo.cpp )
add_executable( dctimg src/dctimg.cpp )
add_executable( rgbdctimg src/rgbdctimg.cpp )
target_link_libraries( demo ${OpenCV_LIBS} )
target_link_libraries( dctimg ${OpenCV_LIBS} )
target_link_libraries( rgbdctimg ${OpenCV_LIBS} )

编译运行

cd build
cmake ..
make
./demo # 执行

ubuntu22.04 OpenCV4.6.0(c++)环境配置_第5张图片

Over

你可能感兴趣的:(C++,c++,ubuntu,linux)