在開發影像處理和計算機視覺應用時,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
按Enter鍵執行該命令,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、執行結果