在开发图像处理和计算机视觉应用时,QT和OpenCV是两个不可或缺的工具。QT为我们提供了丰富的图形界面设计功能,而OpenCV则提供了强大的图像处理算法。那么,如何将这两者完美结合呢?本文将为你详细解析QT5.12与OpenCV的配置过程,让你轻松上手!
一、软件下载
1、QT 5.12.9 下载地址:
https://download.qt.io/archive/qt/5.12/5.12.9/qt-opensource-windows-x86-5.12.9.exe
2、CMake 3.21.0 下载地址:
https://cmake.org/files/v3.21/cmake-3.21.0-rc1-windows-x86_64.msi
3、Opencv 4.6.0下载地址:
通过官网地址:
https://opencv.org/releases/
选择4.6.0的windows版本下载:
二、安装软件
1、安装QT 5.12.9
2、安装opencv-4.6.0-vc14_vc15.exe
注意:最好和QT安装在同一个盘符
比如我的安装路径如下:
3、安装cmake-3.21.0-rc1-windows-x86_64.msi
默认路径安装:C:\Program Files\CMake
三、配置系统环境变量
找到高级系统设置->环境变量->-编辑->新建:
添加如下内容:
1 D:\qt\5.12.9\mingw73_64\bin 2 D:\qt\Tools\mingw730_64\bin 3 C:\Program Files\CMake\bin
四、编译Opencv
1、创建opencv-build目录
在opencv源码包目录下创建opencv-build目录
D:\opencv\opencv\opencv-build
2、运行cmake-gui.exe
进入C:\Program Files\CMake\bin目录,双击cmake-gui.exe运行,分别配置opencv的源码目录和编译产生二进制的目录路径,如下:
然后点击配置:
选择MinGW Makefiles,点击Specify native compilers,再next:
选择gcc和g++工具路径,然后点Finish:
D:/qt/Tools/mingw730_64/bin/gcc.exe D:/qt/Tools/mingw730_64/bin/g++.ext
Configuring done之后,会出现一大片红的,不用管,下拉将WITH_OPENGL和WITH_QT选中,另外不勾选BUILD_PROTOBUF, 同时然后点击add Entry, 配置OPENCV_VS_VERSIONINFO_SKIP=1(界面按钮:add Entry -> Name:OPENCV_VS_VERSIONINFO_SKIP ->Type:Bool->Value打钩),如下:
配置后, 再次点击Configure, 此时仍旧会出现一些红色配置项 ,确认 OPENCV_VS_VERSION_SKIP是勾选的和BUILD_PROTOBUF是不勾选的,如果没有, 通过(界面按钮:add Entry -> Name:OPENCV_VS_VERSIONINFO_SKIP ->Type:Bool->Value打钩) 再次配置下:
再次点击Configure,红色消失,最后点击Generate,完成之后, 关闭Cmake GUI界面, 然后进入opencv-build目录:
将鼠标放在该文件夹界面内,按住shift键,同时点击鼠标右键,点击在此处打开Powershell窗口(s),进入Windows Powershell,输入:
mingw32-make -j 8
按回车键执行该命令,100%完成之后,输入:
mingw32-make install
完成之后退出
另外再将如下路径按照前述方式加入环境变量即可:
D:\opencv\opencv\opencv-build\install\x64\mingw\bin
五、创建QT工程测试验证
1、创建Opencv_test工程
Base Calss为QMainWindows,创建完的工程目录如下:
2、测试代码:
a.opencv_test.pro
1 //opencv_test.pro 2 QT += core gui multimedia 3 4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 5 6 CONFIG += c++11 7 8 # The following define makes your compiler emit warnings if you use 9 # any Qt feature that has been marked deprecated (the exact warnings 10 # depend on your compiler). Please consult the documentation of the 11 # deprecated API in order to know how to port your code away from it. 12 DEFINES += QT_DEPRECATED_WARNINGS 13 14 # You can also make your code fail to compile if it uses deprecated APIs. 15 # In order to do so, uncomment the following line. 16 # You can also select to disable deprecated APIs only up to a certain version of Qt. 17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 18 19 SOURCES += \ 20 main.cpp \ 21 mainwindow.cpp 22 23 HEADERS += \ 24 mainwindow.h 25 26 FORMS += \ 27 mainwindow.ui 28 29 //很重要:主要添加这两行,指定头文件路径和库路径 30 INCLUDEPATH += D:\opencv\opencv\opencv-build\install\include 31 LIBS += D:\opencv\opencv\opencv-build\install\x64\mingw\lib\libopencv_*.a 32 33 # Default rules for deployment. 34 qnx: target.path = /tmp/$${TARGET}/bin 35 else: unix:!android: target.path = /opt/$${TARGET}/bin 36 !isEmpty(target.path): INSTALLS += target
b.mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 QT_BEGIN_NAMESPACE 7 namespace Ui { class MainWindow; } 8 QT_END_NAMESPACE 9 10 class MainWindow : public QMainWindow 11 { 12 Q_OBJECT 13 14 public: 15 MainWindow(QWidget *parent = nullptr); 16 ~MainWindow(); 17 18 private: 19 Ui::MainWindow *ui; 20 }; 21 #endif // MAINWINDOW_H
c. mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 //添加相关投文件和包 5 #include <opencv2/core/core.hpp> 6 #include <opencv2/highgui/highgui.hpp> 7 #include <opencv2/imgproc/imgproc.hpp> 8 using namespace cv; 9 10 11 MainWindow::MainWindow(QWidget *parent) 12 : QMainWindow(parent) 13 , ui(new Ui::MainWindow) 14 { 15 ui->setupUi(this); 16 //一定要使用绝对路径找到图片的正确为止,其他可以回报错 17 Mat image=imread("E:\\qtproject\\opencvTest\\1.jpg",1); 18 namedWindow( "Display", WINDOW_AUTOSIZE ); 19 imshow( "Display window", image ); 20 21 } 22 23 MainWindow::~MainWindow() 24 { 25 delete ui; 26 }
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
3、运行结果
标签:bin,教程,QT,opencv,mainwindow,OpenCV,include,MainWindow,Qt5.12 From: https://www.cnblogs.com/ybqjymy/p/18070391