Qt 實現配置 OpenCV 環境,並實現開啟圖片與呼叫攝像頭
阿新 • • 發佈:2022-04-03
一、說明
所用QT版本:5.9.1
電腦配置:win10,64位系統
呼叫的是編譯好的:OpenCV-MinGW-Build-4.1.0(稍後放連結)
在大學期間,由於專案需求需要用到QT+opencv進行程式設計。在網上看了一下,有很多介紹配置的方法的文章,大致有兩種,一種是需要使用CMake對opencv進行編譯,這種方法太複雜了,而且我在嘗試中也是各種報錯,各種嘗試無果之後果斷放棄了;
另一種是直接引用庫函式,配置起來非常簡單,我選擇的配置方法是第二種。雖然第二種方法只有三四步的過程,網上也有很多教程,但是在我實際配置的過程中,遇到了很多麻煩,本來幾分鐘搞定的事情,我花了幾天才完成,中途一度有過放棄。
這裡簡單介紹一下配置方法,提點一下特別需要主要的細節。
二、步驟
1、新建一個專案(注意英文路徑)
2、在pro檔案內加入程式碼(完整如下):
#------------------------------------------------- # # Project created by QtCreator 2021-11-04T19:23:51 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = A_1 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as 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 you use 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:/RJDZ/OpenCV-MinGW-Build-4.1.0/include\ D:/RJDZ/OpenCV-MinGW-Build-4.1.0/include/opencv2 LIBS += D:/RJDZ/OpenCV-MinGW-Build-4.1.0/x86/mingw/lib/libopencv_*.dll.a
3、在ui介面隨便拖入的幾個QPushButton,修改objectName :
4、在 mainwindow.h 標頭檔案中加入:
#include <QMainWindow>
#include <opencv2/opencv.hpp>
完整如下:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <opencv2/opencv.hpp> using namespace cv; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); int num = 0; ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
5、main.cpp 保持不變 :
#include "mainwindow.h"
#include <QApplication>
int num=0;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
6、在 mainwindow.cpp 中加入以下標頭檔案 :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
完整程式碼以及功能定義 :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->en1,&QPushButton::clicked,[=](){
qDebug()<<"開啟圖片" ;
Mat image=imread("D:/pp/2.jpg");//一定要使用絕對路徑,其他可以回報錯
//namedWindow( "Display window”, WINDOW_AUTOSIZE ");
imshow( "Display window", image );
qDebug()<<"num:"<<num<<endl;
});
connect(ui->video,&QPushButton::clicked,[=](){
VideoCapture cap(0);
Mat imgs;
while (true) {
num=0;
cap.read(imgs);
flip(imgs,imgs,1);
imshow("Video",imgs);
waitKey(1);
if(num==1){
break;
}
}
});
connect(ui->V_off,&QPushButton::clicked,[=](){
num=1;
});
connect(ui->off,&QPushButton::clicked,[=](){
parent->close();
close();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
三、編譯,測試效果
可以看到,能夠正常執行:
各功能都可以正常使用:
、
以上,配置完成!