使用软件:
Qt 5.14.2
cmake 3.15.7
openCV 4.5.3
下载地址:download.qt.io
选择版本:Qt 5.14.2
Qt 5.14.2百度网盘下载地址:
链接:https://pan.baidu.com/s/1ycfiU34xCnBvbnSBoCrtIw
提取码:zj4c
Qt安装路径\Tools\mingw730_32\bin
CMake是一个跨平台的编译工具,OpenCV是使用C/C++编写的,官方网站提供的版本Qt不能直接使用,需要使用CMake重新编译Qt可用的版本。
各版本下载官网地址:https://cmake.org/files/
选择版本:cmake-3.15.7-win64-x64.msi(CMake版本选择无所谓)
cmake 3.15.7百度网盘下地址:
链接:https://pan.baidu.com/s/1WFWYT1IGdpCp4wlMziLZDA
提取码:x5jn
下载地址:https://opencv.org/releases/
选择版本:opencv 4.5.3
opencv 4.5.3百度网盘下载地址:
链接:https://pan.baidu.com/s/1vzkp3BfFZGmO0kDx3ThYVw
提取码:m2xo
3.0版本以后的OpenCV中,SURF和ORB等特征提取算法对应的库以opencv_contrib模块独立出来,因此推荐安装opencv_contrib模块。
下载地址:https://github.com/opencv/opencv_contrib/tags
选择版本:opencv_contrib 4.5.3(需要与OpenCV版本相同,否则编译时可能会报错)
opencv_contrib 4.5.3百度网盘下载地址:
链接:https://pan.baidu.com/s/1o4t-qugwC-_XMvV3Yx9Mkg
提取码:vuwc
为方便管理,把下载后的文件解压到OpenCV安装路径下。
Qt搭建OpenCV官方文档:
https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
搭建环境前确保MinGW环境变量和CMake环境变量已添加:
在OpenCV安装目录下,新建MinGW编译输出文件夹,命名随意,此处为opencv_build。
分别在“C”、“C++”中填写Qt安装路径下的相应的编译器路径:
Qt安装路径/Tools/mingw730_32/bin/gcc.exe
Qt安装路径/Tools/mingw730_32/bin/g++.exe
发现出现CMake Warning,打开编译输出文件夹下的CMakeDownloadLog.txt文件,查看具体的错误(即使没有出现CMake Warning,也需要查看此文件)。
将下载后的文件放入源码(即sources目录)路径下对应的文件夹。
接下来进行一些选项的勾选(最好不要漏,漏了,你懂的)。
勾选WITH_QT
勾选WITH_OPENGL
不勾选WITH_IPP
不勾选WITH_OPENCL_D3D11_NV
勾选ENABLE_CXX11
不勾选ENABLE_PRECOMPILED_HEADERS
勾选OPENCV_ENABLE_NONFREE
不勾选OPENCV_ENABLE_ALLOCATOR_STATS
由于版本原因,以上有些选项没有,未提到选项默认。
以上错误不用管,只看控制台报错情况。设置“Qt5_DIR”为Qt安装目录下的“Qt5”文件夹路径:
Qt安装路径/5.14.2/mingw73_32/lib/cmake/Qt5
cmake编译opencv完成!
接下来,打开cmd,到编译输出文件(opencv_build)所在路径下,输入以下命令:
mingw32-make -j 8
“-j 8”表示使用8线程来处理,可以根据电脑硬件条件进行调整。使用单线程编译命令为:
mingw32-make
mingw32-make install
OpenCV安装路径\opencv\opencv_build\install\x64\mingw\bin
打开QtCreator,新建“MainWindow”类项目。在.pro文件里输入(“INCLUDEPATH”和“LIBS”的值根据具体路径调整):
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += D:\Downloads\OpenCV\opencv\build\include\
D:\Downloads\OpenCV\opencv\opencv-build\install\include
LIBS += D:\Downloads\OpenCV\opencv\opencv-build\bin\libopencv_*.dll
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
在mainwindow.cpp文件中输入(图片的路径需要具体调整):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
using namespace cv;
using namespace xfeatures2d;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// read an image
cv::Mat image = cv::imread("D://lena.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
编译运行。
编译不报错,且出现以上结果证明Qt配置OpenCV环境成功!
接下来就是在Qt的世界里尽情地玩耍OpenCV!
各版本Opencv模块介绍:https://docs.opencv.org/4.5.3/index.html
1.https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows
2.https://zhuanlan.zhihu.com/p/326284759
3.https://blog.csdn.net/zhizhengguan/article/details/107567449
4.https://www.bilibili.com/video/BV1Za4y1v7ra?p=1