ubuntu22.04OpenCV4.6.0(c++)环境配置-创新互联
ubuntu22.04 OpenCV4.6.0(c++)环境配置
前置依赖安装
文章标题:ubuntu22.04OpenCV4.6.0(c++)环境配置-创新互联
转载源于:http://pwwzsj.com/article/ccopgi.html
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/
进入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官网链接
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- 创建
build
目录
cd opencv-4.6.0
mkdir -p build
cd build
- 执行
cmake
命令:
cmake -DCMAKE_BUILD_TYPE=Release \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DCMAKE_INSTALL_PREFIX=/usr/local ..
- 编译:
sudo make -j 4
使用4个线程进行编译,可以一定程度上加快编译速度
- 安装:
sudo make install
环境配置参考博客
- 打开文件:
sudo gedit /etc/ld.so.conf.d/opencv.conf
- 写入:
/usr/local/lib
- 更新系统共享链接库:
sudo ldconfig
- 打开bashrc文件:
sudo gedit /etc/bash.bashrc
- 写入如下内容:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
- 更新,
source /etc/bash.bashrc
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
如下图,安装成功:
新建文件夹digital
创建文件:
touch CMakeLists.txt
mkdir -p build
mkdir -p img
mkdir -p src
源码放在src目录下
#include#include#includeusing 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 # 执行
Over
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章标题:ubuntu22.04OpenCV4.6.0(c++)环境配置-创新互联
转载源于:http://pwwzsj.com/article/ccopgi.html