1). Pro File
#-------------------------------------------------# # Project created by Qtcreator 2014-05-18t12:56:52##------------ -------------------------------------QT + + core Guigreaterthan (Qt_major_version, 4): qt + = Widgetstarget = Myqtdemot Emplate = appsources + = main.cpp Mainwindow.cppheaders + mainwindow.hforms + = Mainwindow.uiincludepath+=d:\ope NCV249\INCLUDE\OPENCV D:\opencv249\include\opencv2 D:\opencv249\includelibs+=d:\open CV249\LIB\LIBOPENCV_CALIB3D249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_CONTRIB249.DLL.A d:\opencv249\lib\libopencv_ CORE249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_FEATURES2D249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_FLANN249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_GPU249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_HIGHGUI249.DLL.A D:\opencv249\lib\libop ENCV_IMGPROC249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_LEGACY249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_ML249.DLL.A D:\opencv249\lib\libopencv_oBJDETECT249.DLL.A D:\OPENCV249\LIB\LIBOPENCV_VIDEO249.DLL.A
2). h file
#ifndef mainwindow_h#define mainwindow_h#include <QMainWindow> #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 (); Cv::mat Image,image2; Qimage img;private Slots: void on_pushbutton_clicked (); void on_pushbutton_2_clicked ();p rivate: ui::mainwindow *ui;}; #endif//Mainwindow_h
3). cpp file
#include "mainwindow.h" #include "ui_mainwindow.h" #include "mainwindow.h" #include "ui_mainwindow.h" #include < qstring> #include <QFileDialog> #include <QMessageBox> #include <opencv/cv.h> #include < Qtextcodec>using namespace CV; Mainwindow::mainwindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this);} Mainwindow::~mainwindow () {Delete UI;} void mainwindow::on_pushbutton_clicked () {QString filename = qfiledialog::getopenfilename (this,tr ("Open Image"), "", TR ("Image File (*.bmp *.jpg *.jpeg *.png)"); Qtextcodec *code = Qtextcodec::codecforname ("GB18030"); std::string name = Code->fromunicode (filename). data (); Image = Cv::imread (name); if (!image.data) {Qmessagebox msgBox; Msgbox.settext (TR ("image data is null"); Msgbox.exec (); } else {cv::cvtcolor (IMAGE,IMAGE2,CV_BGR2RGB); img = qimage ((const unsigned char*) (image2.data), Image.cols,image.rows, Image.step, qimage::format_rgb888); Ui->label->clear (); Ui->label->setpixmap (Qpixmap::fromimage (IMG)); Ui->processpushbutton->setenabled (TRUE); Ui->label->resize (Ui->label->pixmap ()->size ()); }}void mainwindow::on_pushbutton_2_clicked () {//Cv::flip (image,image,1); Mat Gray; Cvtcolor (Image,gray,cv_bgr2gray); Cvtcolor (GRAY,GRAY,CV_BG)//Imshow ("Li", gray); Cv::cvtcolor (IMAGE,IMAGE,CV_BGR2RGB); img = qimage ((const unsigned char*) (image.data), Image.cols,image.rows,image.cols*image.channels (), Qimage::format _rgb888); img = qimage ((const unsigned char*) (gray.data), Gray.cols,gray.rows,gray.step, Qimage::format_indexed8); Ui->label->setpixmap (Qpixmap::fromimage (IMG));}
Grayscale of QT+QT CREATOR+OPENCV image