Creating a GUI application using Qt

本文介绍如何使用Qt创建GUI应用程序,并集成OpenCV进行图像处理。通过具体的代码示例展示了如何加载图像、显示图像以及执行基本的图像操作,如翻转等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Create New Project... and  choose Qt GUI Application


Specifying the OpenCV library files and header files location (with extension .pro)

#-------------------------------------------------
#
# Project created by QtCreator 2012-05-29T17:22:53
#
#-------------------------------------------------

QT       += core gui

TARGET = p35QtGUIap
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += E:\OpenCV2.3.1\build\include\opencv2\
INCLUDEPATH += E:\OpenCV2.3.1\build\include\opencv\
INCLUDEPATH += E:\OpenCV2.3.1\build\include\

LIBS += -LE:\OpenCV2.3.1\build\x86\vc10\lib \
-lopencv_core231d \
-lopencv_highgui231d \
-lopencv_video231d \
-lopencv_ml231d \
-lopencv_legacy231d \
-lopencv_imgproc231d


finding a file having the extension .ui,which is the one that describes the UI layout


Defining a cv::Mat class member variable in the header file of the MainWindow class

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;
    cv::Mat iamge; // the image variable
};

#endif // MAINWINDOW_H

callback function

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
            tr("Open Image"),".",
          tr("Image File (*.png *.jpg *.jpeg *.bmp)"));
        image= cv::imread(fileName.toAscii().data());
    cv::cvtColor(image,image,CV_BGR2RGB);
        // Qt image
    QImage img= QImage((const unsigned char*)(image.data),
                           image.cols,image.rows,QImage::Format_RGB888);
        // display on label
    ui->label->setPixmap(QPixmap::fromImage(img));
        // resize the label to fit the image
    ui->label->resize(ui->label->pixmap()->size());
}

void MainWindow::on_pushButton_2_clicked()
{
    cv::flip(image,image,1);
    //cv::cvtColor(image,image,CV_BGR2RGB);
    // Qt image
    QImage img= QImage((const unsigned char*)(image.data),
                       image.cols,image.rows,QImage::Format_RGB888);
    // display on label
    ui->label->setPixmap(QPixmap::fromImage(img));
    // resize the label to fit the image
    ui->label->resize(ui->label->pixmap()->size());
}

Run:


Process:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值