Qt实现配置OpenCV环境,并实现打开图片与调用摄像头

博客 动态
0 218
羽尘
羽尘 2022-04-03 13:57:29
悬赏:0 积分 收藏

Qt 实现配置 OpenCV 环境,并实现打开图片与调用摄像头

一、说明

所用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 guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = A_1TEMPLATE = 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.0SOURCES += \        main.cpp \        mainwindow.cppHEADERS += \        mainwindow.hFORMS += \        mainwindow.uiINCLUDEPATH += D:/RJDZ/OpenCV-MinGW-Build-4.1.0/include\               D:/RJDZ/OpenCV-MinGW-Build-4.1.0/include/opencv2LIBS += 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_OBJECTpublic:    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;}

三、编译,测试效果

可以看到,能够正常运行:

各功能都可以正常使用:
   、

以上,配置完成!

posted @ 2022-04-03 13:16 HanaKoo 阅读(5) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员