Image_process.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-02-19T20:30:33
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Image_precess
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32 {
INCLUDEPATH += F:\OPENCV3.4.2\opencv\QtBuild\install\include
LIBS += F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_calib3d342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_core342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_dnn342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_features2d342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_flann342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_highgui342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_imgcodecs342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_imgproc342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_ml342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_objdetect342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_photo342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_shape342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_stitching342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_superres342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_video342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_videoio342.dll.a\
F:\OPENCV3.4.2\opencv\QtBuild\install\x86\mingw\lib\libopencv_videostab342.dll.a
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
using namespace cv;
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_open_clicked();
QImage Mat2QImage(const Mat &mat);
void display(QImage img);
void on_pushButton_gray_clicked();
void on_pushButton_close_clicked();
void on_treshold(int,void*);
private:
Ui::MainWindow *ui;
Mat image;
Mat rgb;
Mat imgThresh;
Mat edge;
Mat drawing;
Mat result;
QImage qimg;
QImage imgScared;
QImage img;
int h_min = 0;
int h_max = 10;
int s_min = 43;
int s_max = 255;
int v_min = 0;
int v_max = 255;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_open_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image File(*.jpg *.png)"));
image = imread(fileName.toLatin1().data());
qimg = MainWindow::Mat2QImage(image);
display(qimg);
if(image.data)
{
ui->pushButton_gray->setEnabled(true);
}
}
QImage MainWindow::Mat2QImage(const Mat &mat)
{
if(mat.channels()==3)
{
cvtColor(mat,rgb,COLOR_BGR2RGB);
img = QImage((const unsigned char*)(rgb.data),
rgb.cols,
rgb.rows,
rgb.cols*rgb.channels(),
QImage::Format_RGB888);
}
else if(mat.channels()==1) //灰色图像
{
img = QImage((const unsigned char*)(mat.data),
mat.cols,
mat.rows,
mat.cols*mat.channels(),
QImage::Format_Indexed8);
}
else
{
img = QImage((const unsigned char*)(mat.data),
mat.cols,
mat.rows,
mat.cols*mat.channels(),
QImage::Format_RGB888);
}
return img;
}
void MainWindow::display(QImage img)
{
//imgScared = img.scaled(ui->label->size(),Qt::KeepAspectRatio);
ui->label->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::on_pushButton_gray_clicked()
{
qDebug() << "recognize";
//ui->statusBar->showMessage("press recognize button");
//ui->label->clear();
cvtColor(image,image,COLOR_BGR2HSV);
on_treshold(0,0);
qimg = MainWindow::Mat2QImage(drawing);
display(qimg);
}
void MainWindow::on_treshold(int,void*)
{
vector hsvSplit;
split(image,hsvSplit);
equalizeHist(hsvSplit[2],hsvSplit[2]);
merge(hsvSplit,image);
inRange(image,Scalar(h_min,s_min,v_min),Scalar(h_max,s_max,v_max),imgThresh);
Mat element = getStructuringElement(MORPH_RECT,Size(3,3));
morphologyEx(imgThresh,imgThresh,MORPH_OPEN,element);
morphologyEx(imgThresh,imgThresh,MORPH_CLOSE,element);
Canny(imgThresh,edge,3,9,3);
vector> contours;
vector hierarchy;
vector boundRect(contours.size());
findContours(edge,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
drawing = Mat::zeros(edge.size(),CV_8UC3);
drawContours(drawing,contours,-1,Scalar(0,255,0),1,8,hierarchy);
//int x0=0,y0=0,w0=0,h0=0;
//x0,y0,w0,h0 = boundingRect(contours);
//rectangle(drawing,(x0,y0),(x0+w0,y0+h0),Scalar(0,0,255),2);
// Rect rect = boundingRect(contours);
// rectangle(drawing,rect,Scalar(0,0,255),2,8,0);
// int x0=0,y0=0,w0=0,h0=0;
// x0 = boundingRect(contours).x;
// y0 = boundingRect(contours).y;
// w0 = boundingRect(contours).width;
// h0 = boundingRect(contours).height;
// rectangle(drawing,Point(x0,y0),Point(x0+w0,y0+h0),Scalar(0,0,255),2,8);
// for(int i=0; i < contours.size(); i++)
// {
// boundRect[i] = boundingRect(Mat(contours[i]));
// drawContours(drawing,contours,i,Scalar(0,255,0),1,8,hierarchy);
// rectangle(drawing,boundRect[i].tl(),boundRect[i].br(),Scalar(0,0,255),2,8);
// }
}
void MainWindow::on_pushButton_close_clicked()
{
this->close();
}