Qt 5.3 under OpenCV 2.4.11 Development (3) Simple GUI project

Source: Internet
Author: User

Create a new GUI project, QT4 for Qt4 GUI application,qt5 for Qt Widgets application, base class Select Qmainwindow

Project implementation features: Two buttons, one for selecting a picture in a folder and previewing a picture, a button to invert the picture. The project code is as follows:

. Pro code snippet, the code for adding OpenCV header files and library functions depends on your development environment.

#-------------------------------------------------# # Project created by Qtcreator 2015-07-29t14:41:15##------------ -------------------------------------QT + + core Guigreaterthan (Qt_major_version, 4): qt + = Widgetstarget = Opencv-te St-guitemplate = appsources + = main.cpp Mainwindow.cppheaders + mainwindow.hforms + = mainwindow.uiincludepath+ =D:\USEROPENCVQT\INCLUDE\OPENCV D:\useropencvqt\include\opencv2 D:\useropencvqt\incl UDELIBS+=D:\USEROPENCVQT\LIB\LIBOPENCV_CALIB3D2411.DLL.A D:\USEROPENCVQT\LIB\LIBOPENCV_CONTRIB2411.DLL.A D:\ USEROPENCVQT\LIB\LIBOPENCV_CORE2411.DLL.A D:\USEROPENCVQT\LIB\LIBOPENCV_FEATURES2D2411.DLL.A D:\USEROPENCVQT \LIB\LIBOPENCV_FLANN2411.DLL.A D:\USEROPENCVQT\LIB\LIBOPENCV_GPU2411.DLL.A D:\useropencvqt\lib\libopencv_hig HGUI2411.DLL.A D:\USEROPENCVQT\LIB\LIBOPENCV_IMGPROC2411.DLL.A D:\useropencvqt\lib\libopencv_legacy2411.dll. A D:\useropencvqt\lib\liBOPENCV_ML2411.DLL.A D:\USEROPENCVQT\LIB\LIBOPENCV_OBJDETECT2411.DLL.A D:\useropencvqt\lib\libopencv_video24 11.dll.a

. h code Snippet, note the OPENCV2 header file and the Qfiledialog header file are added.

#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_objectpublic:    explicit MainWindow (Qwidget *parent = 0);    ~mainwindow ();p rivate slots:    void on_pushbutton_clicked ();    void on_pushbutton_2_clicked ();p rivate:    ui::mainwindow *ui;    Cv::mat src;//The Mat object used to preview the picture}; #endif//Mainwindow_h

. cpp code snippet corresponding to the. h file

#include "mainwindow.h" #include "ui_mainwindow.h" Mainwindow::mainwindow (Qwidget *parent):    Qmainwindow (parent) ,    UI (new Ui::mainwindow) {    ui->setupui (this);} Mainwindow::~mainwindow () {    Delete ui;} void mainwindow::on_pushbutton_clicked ()//First button {    QString fileName = Qfiledialog::getopenfilename (This, tr ("Open Image "),". ",                                                    tr (" Image Files (*.png *.jpg *.jpeg *.bmp) ");    src = cv::imread (filename.tolatin1 (). Data ()),//qt4 words with Filename.toascii (). Data (    "src", 0);    Cv::imshow ("src", src);} void mainwindow::on_pushbutton_2_clicked ()//second button {    cv::flip (src, src, 1);    Cv::namedwindow ("result", 0);    Cv::imshow ("result", SRC);}

Main.cpp code snippet, doesn't seem to make any changes

#include "mainwindow.h" #include <qapplication>int main (int argc, char *argv[]) {    qapplication A (argc, argv); C1/>mainwindow W;    W.show ();    return a.exec ();}


To preview the last processed image on the GUI interface, add a label label to the GUI interface and change the second button's handler function to:

void mainwindow::on_pushbutton_2_clicked () {    cv::flip (src, src, 1);    Cv::cvtcolor (SRC, src, cv_bgr2rgb);    Qimage img = qimage ((const unsigned char*) (src.data), Src.cols, Src.rows, qimage::format_rgb888                        );    Ui->label->setpixmap (Qpixmap::fromimage (IMG));    Ui->label->resize (Ui->label->pixmap ()->size ());}

and add the header file:

#include <opencv2/imgproc/imgproc.hpp>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Qt 5.3 under OpenCV 2.4.11 Development (3) Simple GUI project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.