一、安装环境
虚拟机:
Ubuntu 22.04
opencv
版本:
opencv-4.5.2
、
opencv_contrib-4.5.2
、
cvui-2.9.0-beta
二、编译opencv和opencv_contrib
1、源码下载:
opencv
:
https://github.com/opencv/opencv
链接
2
:
https://opencv.org/releases/
github下载
![](https://img-blog.csdnimg.cn/direct/410859dd2443465597dfba3a174f4200.png)
![](https://img-blog.csdnimg.cn/direct/3a33165eb4c9437c893f64893f0ab4ef.png)
opencv_contrib
:
https://github.com/opencv/opencv_contrib
注意事项
:opencv_contrib
和
opencv
版本
要对应
2、安装前准备
在自己虚拟机的主目录中创建一个
opencv_build
,将两个源码包解压的文件夹放入
其中:
mkdir ~/opencv_build && cd ~/opencv_build/opencv-4.5.2/mkdir buildcd build
安装依赖项
安装
g++, cmake, make ,wget, unzip
sudo apt install -y g ++sudo apt install -y cmakesudo apt install -y makesudo apt install -y wgetsudo apt install -y unzip
安装opencv依赖的基本库
sudo apt-get install build-essential libgtk2.0-dev libgtk-3-devlibavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
3、编译安装
cmake
配置
sudo cmake -DCMAKE_BUILD_TYPE = RELEASE \-DCMAKE_INSTALL_PREFIX = /usr/local \-DOPENCV_EXTRA_MODULES_PATH = /home/pjy/opencv_build/opencv_contrib-4.5.2/modules .. \- DWITH_CUDA = 1 \- DWITH_GTK = ON \-DENABLE_FAST_MATH = 1 \- DCUDA_FAST_MATH = 1 \- DWITH_CUBLAS = 1 \-DOPENCV_GENERATE_PKGCONFIG = ON \- DWITH_GSTREAMER = ON \..
-DOPENCV_EXTRA_MODULES_PATH=
/home/pjy/opencv_build/
opencv_contrib-4.5.2/modules
加粗部分换成自己的路径
-DOPENCV_EXTRA_MODULES_PATH=
/home/pjy/opencv_build/opencv_contrib-
4.5.2
/modules
指明
opencv_contrib
路径,这样好一起编译
如下图就成功:
make
编译
opencv
和
opencv_contrib
版本号一定要对应,不然编译会报错
sudo make -j8
编译完成后,安装
sudo make install
4、环境配置
配置
pkg-config
环境
sudo gedit /etc/profile.d/pkgconfig.sh
可能为空文件,在文件中加入
export PKG_CONFIG_PATH = /usr/local/lib/pkgconfig: $PKG_CONFIG_PATH
保存并退出,source激活
source /etc/profile
验证是否成功
// 查看版本,有输出 4.5.2 就成功pkg-config --modversion opencv4// 查看库pkg-config --libs opencv4
输出类似下面这样即为成功
配置动态库环境
sudo gedit /etc/ld.so.conf.d/opencv4.conf
文件末尾加上
/usr/local/lib
使配置生效
sudo ldconfig
5、demo测试
打开新终端,进入到
example_cmake
目录
cd ~/opencv_build/opencv-4.5.2/samples/cpp/example_cmakecmake .make./opencv_example
6、最后在qt的.pro中加入头文件路径和库就可以在qt中使用了
# 添加 opencv 头文件INCLUDEPATH += /usr/local/include/opencv4INCLUDEPATH += /usr/local/include/opencv4/opencv2# 添加 opencv 库文件LIBS += -L /home/geclab/buildroot-2016.11/output/host/usr/arm-none-linux-gnueabi/sysroot/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping-lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
我一般都是这样写
#添加opencv 的头文件 ARM-OpenCV 环境
INCLUDEPATH += /opencv4-arm/include
INCLUDEPATH += /opencv4-arm/include/opencv4
INCLUDEPATH += /opencv4-arm/include/opencv4/opencv2
#添加OpenCV库文件
LIBS += -L/opencv4-arm/lib -lopencv_world#Windows OpenCV 环境
#添加头文件
INCLUDEPATH += E:\OpenCV\include
INCLUDEPATH += E:\OpenCV\include\opencv2
#添加库文件
LIBS += E:\OpenCV\x64\mingw\lib\libopencv_*.a
三、cvui的安装
1、源码下载:
https://github.com/Dovyski/cvui
2、编译安装
mkdir -p ~/cvui-2.9.0-beta/buildcmake .makesudo make install
3、查看是否安装成功
ls /usr/local/include
四、参考文章