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: