sudo apt install -y g++ make wget unzip
sudo apt install -y libssl-dev build-essential
sudo apt install -y pkg-config
下载地址: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
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.重复安装尝试
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
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
#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;
}
供参考:
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