-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
484 lines (408 loc) · 16 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QTime>
#include <QDir>
#include <QFile>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#pragma execution_character_set("utf-8")
using namespace cv;
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
globalMat=NULL;
globalMark=0;
showMark=0;
QObject::connect(ui->spinBox,SIGNAL(valueChanged(int)),
ui->horizontalSlider,SLOT(setValue(int)));//spinBox值与滑条对应
QObject::connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
ui->spinBox,SLOT(setValue(int)));//滑条与spinBox值对应
connect(ui->horizontalSlider,SIGNAL(sliderReleased()),this,SLOT(setTestImg()));
connect(ui->spinBox,SIGNAL(valueChanged(int)),this,SLOT(setTestImg(int)));
QObject::connect(ui->spinBox_2,SIGNAL(valueChanged(int)),
ui->horizontalSlider_2,SLOT(setValue(int)));//spinBox值与滑条对应
QObject::connect(ui->horizontalSlider_2,SIGNAL(valueChanged(int)),
ui->spinBox_2,SLOT(setValue(int)));//滑条与spinBox值对应
// ui->horizontalSlider_2->setValue(0);//滑条初始值,范围值在窗口设计器里设置了
ui->horizontalSlider->setMaximum(255);
ui->spinBox->setMaximum(255);
// this->ui->label_4->installEventFilter(this);
this->ui->spinBox->installEventFilter(this);
TDlg=new ThresholdDlg;
connect(this,SIGNAL(sendImage(Mat)),TDlg,SLOT(receiveMainImage(Mat)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_on_openfile_triggered()
{
ui->originalImg_label->clear();
ui->resultImg_label->clear();
QString fileName = QFileDialog::getOpenFileName(
this, tr("open image file"),
"./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));
if(fileName.isEmpty())
{
QMessageBox mesg;
mesg.warning(this,"警告","打开图片失败!");//.about(NULL,"图片大小", strg);
return;
}
else
{
QString str=fileName;//文件名
std::string path = str.toLocal8Bit().toStdString(); //opencv加载中文问题关键是这个
Mat pImgs=imread(path);//cv函数创建Mat
emit sendImage(pImgs);
globalMat=pImgs;
globalImg=MatToQImage(pImgs);
// Mat bw = 50 < 128 ? (globalMat < 100) : (globalMat < 10);
// Mat bw = globalMat<100;
// globalImg=MatToQImage(bw);
QImage img;
ui->originalImg_label->clear();
img=globalImg.scaled(ui->originalImg_label->width(),ui->originalImg_label->height(),Qt::KeepAspectRatio);
ui->originalImg_label->setPixmap(QPixmap::fromImage(img));
globalMark=1;
showMark=0;
if(img.isNull())
{
QMessageBox::information(this,"信息","打开文件失败");
}
}
}
QImage MainWindow::MatToQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if (mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for (int row = 0; row < mat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if (mat.type() == CV_8UC4)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}
void MainWindow::on_on_restore_triggered()
{
ui->resultImg_label->clear();
}
void MainWindow::on_ac_clear_triggered()
{
ui->originalImg_label->clear();
ui->resultImg_label->clear();
globalMat=NULL;
globalMark=0;
showMark=0;
}
void MainWindow::on_ac_save_triggered()
{
if( globalMark==1)
{
QString dirPath;
QString filePath;
QString saveName="原始图";
QString saveName2="处理结果图";
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString str = time.toString("yyyyMMdd_hhmmss"); //设置显示格式
filePath = QFileDialog::getExistingDirectory(this,"");
dirPath=QString("%1/Opencv develop temp%2").arg(filePath).arg(str);
QDir *temp = new QDir;//声明文件对象
temp->mkdir(dirPath);//创建文件夹
QImage images;
images=MatToQImage( globalMat);///////////////////////////////////////
QString savePath=QString("%1//%2.jpg").arg(dirPath).arg(saveName);
images.save(savePath);
QImage images2;
images2=MatToQImage( global_ResultMat);///////////////////////////////////////
QString savePath2=QString("%1//%2.jpg").arg(dirPath).arg(saveName2);
if(images2.isNull())
{
QMessageBox::information(this,"信息","处理结果为空未保存");
}
else
{
images2.save(savePath2);
}
QMessageBox::information(this,"信息","保存成功");
}
else
{
QMessageBox::information(this,"信息","保存失败");
}
}
void MainWindow::on_ac_histogramRGB_triggered()
{
Mat src;
src=globalMat;
if( !src.data )
{
QMessageBox::information(this,"信息","没有加载图像");
}
else
{
/// 分割成3个单通道图像 ( R, G 和 B )
vector<Mat> rgb_planes;
split( src, rgb_planes );
/// 设定bin数目
int histSize = 255;
/// 设定取值范围 ( R,G,B) )
float range[] = { 0, 255 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat r_hist, g_hist, b_hist;
/// 计算直方图:
calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
// 创建直方图画布
int hist_w = 400; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) );
/// 将直方图归一化到范围 [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
/// 在直方图画布上画出直方图
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
}
// /// 显示直方图
// namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
// imshow("calcHist Demo", histImage );
// waitKey(0);
QImage imgshow=MatToQImage(histImage);
this-> ui->resultImg_label->clear();
imgshow=imgshow.scaled( this->ui->resultImg_label->width(), this->ui->resultImg_label->height(),Qt::KeepAspectRatio);
this-> ui->resultImg_label->setPixmap(QPixmap::fromImage(imgshow));
}
}
void MainWindow::on_ac_test_triggered()
{
showMark=4;
this->ui->spinBox->setValue(160);
int threshval=this->ui->spinBox->value();
Mat img=globalMat;
//进行,灰度处理
cv::cvtColor(img,img,COLOR_RGB2GRAY);
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
vector<vector<Point> > contours; //定义点和向量
vector<Vec4i> hierarchy;//定义点和向量
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //查找轮廓
Mat dst = Mat::zeros(img.size(), CV_8UC3);//初始化dst
if( !contours.empty() && !hierarchy.empty() ) //开始处理
{
//遍历所有顶层轮廓,随机生成颜色值绘制给各连接组成部分
for( int idx = 0; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
//绘制填充轮廓
drawContours(
dst, //用来绘制轮廓的图像
contours, //指向第一个轮廓的指针
idx, //
color, //内层轮廓的颜色
CV_FILLED, //绘制轮廓的最大等级,绘制轮廓时所使用的线条的粗细度。如果值为负(e.g. =CV_FILLED),绘制内层轮廓。
8, //线条的类型
hierarchy ); //按照给出的偏移量移动每一个轮廓点坐标.
}
}
// imshow( "Connected Components", dst );//显示窗口
QImage imgshow=MatToQImage(dst);
this-> ui->resultImg_label->clear();
imgshow=imgshow.scaled( this->ui->resultImg_label->width(), this->ui->resultImg_label->height(),Qt::KeepAspectRatio);
this-> ui->resultImg_label->setPixmap(QPixmap::fromImage(imgshow));
}
//这个延时比较严重
void MainWindow::setTestImg(int value_m)
{
Mat img=globalMat;
if(showMark==4)
{
//进行,灰度处理
cv::cvtColor(img,img,COLOR_RGB2GRAY);
Mat bw = value_m< 128 ? (img < value_m) : (img > value_m);
vector<vector<Point> > contours; //定义点和向量
vector<Vec4i> hierarchy;//定义点和向量
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //查找轮廓
Mat dst = Mat::zeros(img.size(), CV_8UC3);//初始化dst
if( !contours.empty() && !hierarchy.empty() ) //开始处理
{
//遍历所有顶层轮廓,随机生成颜色值绘制给各连接组成部分
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
//绘制填充轮廓
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
}
// imshow( "Connected Components", dst );//显示窗口
QImage imgshow=MatToQImage(dst);
this-> ui->resultImg_label->clear();
imgshow=imgshow.scaled( this->ui->resultImg_label->width(), this->ui->resultImg_label->height(),Qt::KeepAspectRatio);
this-> ui->resultImg_label->setPixmap(QPixmap::fromImage(imgshow));
global_ResultMat=dst;
}
}
void MainWindow::setTestImg()
{
Mat img=globalMat;
if(showMark==4)
{
int value_m=this->ui->spinBox->value();
//进行,灰度处理
cv::cvtColor(img,img,COLOR_RGB2GRAY);
Mat bw = value_m< 128 ? (img < value_m) : (img > value_m);
vector<vector<Point> > contours; //定义点和向量
vector<Vec4i> hierarchy;//定义点和向量
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); //查找轮廓
Mat dst = Mat::zeros(img.size(), CV_8UC3);//初始化dst
if( !contours.empty() && !hierarchy.empty() ) //开始处理
{
//遍历所有顶层轮廓,随机生成颜色值绘制给各连接组成部分
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
//绘制填充轮廓
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
}
// imshow( "Connected Components", dst );//显示窗口
QImage imgshow=MatToQImage(dst);
this-> ui->resultImg_label->clear();
imgshow=imgshow.scaled( this->ui->resultImg_label->width(), this->ui->resultImg_label->height(),Qt::KeepAspectRatio);
this-> ui->resultImg_label->setPixmap(QPixmap::fromImage(imgshow));
global_ResultMat=dst;
}
}
//bool MainWindow::eventFilter(QObject *obj, QEvent *event)
//{
// if (obj ==this->ui->spinBox) {
// if (event->type() ==QEvent::MouseButtonPress)
// {
// qDebug() << "Ate key press" ;
//// setTestImg();
//// ui->spinBox->setValue(ui->horizontalSlider->value());
// return true;
// }
// else
// {
// return false;
// }
// }
// else
// {
// // pass the event on to the parent class
// return QMainWindow::eventFilter(obj, event);
// }
//}
void MainWindow::on_ac_histogramGray_triggered()
{
#define cvQueryHistValue_1D( hist, idx0 )\
((float)cvGetReal1D( (hist)->bins, (idx0)))
//进行,灰度处理
//matOri输入图像 由对话框来选
//dstImage输出图像,最后希望显示的图像必须添加
//例如:cv::cvtColor(matOri,dstImage,COLOR_RGB2GRAY);
//灰度直方图处理测试
IplImage *src= &IplImage(globalMat);
IplImage* gray_plane = cvCreateImage(cvGetSize(src),8,1);
cvCvtColor(src,gray_plane,CV_RGB2GRAY);
// cv::cvtColor(src,gray_plane,COLOR_RGB2GRAY);
int hist_size = 256; //直方图尺寸
int hist_height = 256;
float range[] = {0,255}; //灰度级的范围
float* ranges[]={range};
//创建一维直方图,统计图像在[0 255]像素的均匀分布
CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
//计算灰度图像的一维直方图
cvCalcHist(&gray_plane,gray_hist,0,0);
//归一化直方图
cvNormalizeHist(gray_hist,1.0);
int scale = 2;
//创建一张一维直方图的“图”,横坐标为灰度级,纵坐标为像素个数(*scale)
IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale,hist_height),8,3);
cvZero(hist_image);
//统计直方图中的最大直方块
float max_value = 0;
cvGetMinMaxHistValue(gray_hist, 0,&max_value,0,0);
//分别将每个直方块的值绘制到图中
for(int i=0;i<hist_size;i++)
{
float bin_val = cvQueryHistValue_1D(gray_hist,i); //像素i的概率
int intensity = cvRound(bin_val*hist_height/max_value); //要绘制的高度
cvRectangle(hist_image,
cvPoint(i*scale,hist_height-1),
cvPoint((i+1)*scale - 1, hist_height - intensity),
CV_RGB(255,255,255));
}
Mat outMat=cvarrToMat(hist_image);
QImage imgshow=MatToQImage(outMat);
this-> ui->resultImg_label->clear();
imgshow=imgshow.scaled( this->ui->resultImg_label->width(), this->ui->resultImg_label->height(),Qt::KeepAspectRatio);
this-> ui->resultImg_label->setPixmap(QPixmap::fromImage(imgshow));
global_ResultMat=outMat;
}
void MainWindow::on_ac_threshold_triggered()
{
if(!globalMat.empty())
{
TDlg->show();
}
else {
}
}