1.进入系统下载opencv
下载地址:Releases - OpenCV
选择sources下载相应版本
解压文件,一版放到home/(用户名)/下
2.安装Ubuntu下的opencv需要的依赖项
安装最新的CMake,输入命令
sudo apt-get update
sudo apt-get upgrade
sudo apt install cmake
安装opencv依赖项
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
3.编译和安装opencv
进入到opencv文件夹,输入命令
mkdir build
cd build
编译,安装
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j4
sudo make install
4.环境配置
打开/etc/ld.so.conf
sudo gedit /etc/ld.so.conf
在文件中加上一行
include /usr/local/lib
打开 /etc/bash.bashrc 文件
sudo gedit /etc/bash.bashrc
末尾加上
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
再输入命令
source /etc/bash.bashrc
5.检验
输入命令
pkg-config opencv --modversion
如果最后一步报错为:
Package opencv was not found in the pkg-config search path.
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
sudo vim opencv.pc
如果出现
sudo: vim:找不到命令
输入下命令安装vim
sudo apt install vim
vim模式下 a 插入代码 esc 退出到上一层 :wq 保存退出
首先建一个新的文件夹:pkgconfig
然后cd到这个文件夹下,新建opencv.pc文件;
再打开opencv.pc文件,文件内输入
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.5.2
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
其中version是你的opencv版本。
最后:wq保存退出。
转到 opencv-4.5.2/sample/cpp/example_cmake 目录下,打开终端
cmake .
make
./opencv_example
如果摄像头打开,左上角出现 Hello OpenCV 则证明安装成功
ubuntu下Qt配置opencv
1.新建一个mainwindow项目,在.pro项目文件中添加
INCLUDEPATH += /usr/local/include/opencv4/opencv2 \
/usr/local/include/opencv4
LIBS += /usr/local/lib/libopencv_*
2.mian.cpp文件改为
#include "mainwindow.h"
#include
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Mat image = imread("/home/tuya/img_data/barbara.bmp");
imshow("test",image);
waitKey(0);
return a.exec();
}
编译运行,如果出现 “cannot open shared object file: No such file or directory”,这是因为编译器找不到安装后lib的路径,在终端输入命令
gedit /etc/ld.so.conf.d/opencv.conf
创建一个opencv.conf文件,在文件中添加lib路径
/usr/local/lib
保存,再输入下命令,即可解决
sudo ldconfig -v
重新编译运行项目
ubuntu opencv卸载
在ubuntu终端命令行输入以下命令进入安装opencv的build目录并进行卸载操作。
sudo make uninstall