Create new project...And chooseQt 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 guiTARGET = p35QtGUIapTEMPLATE = appSOURCES += main.cpp\ mainwindow.cppHEADERS += mainwindow.hFORMS += mainwindow.uiINCLUDEPATH += 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: