-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmainwindow.cpp
2543 lines (2405 loc) · 69.1 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <QTextCodec.h>
#include <QFileDialog>
#include <QtDebug>
#include <QString>
#include <QDialog>
#include <QMessageBox>
#include <QImage>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QTextEdit>
//#include <windows.h>
#include <QPrinter>
#include <QPainter>
#include <QPrintDialog>
#include <QMouseEvent>
#include <QMatrix>
#include <QTimer>
#include <QDateTime>
#include <stack>
#include <QWizard>
#include <QWizardPage>
#include <QVBoxLayout>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include<iostream>
#include<time.h>
using namespace std;
using namespace cv;
int interNum = 100;//迭代次数
int internum1;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/images/myico.png"));//图标
ui_hide();//暂时隐藏不需要的内容
QTimer *timer = new QTimer(this);//当前时间显示
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
timer->start(1000);
Mat start = imread("images/start.png");
img_display(start);
}
MainWindow::~MainWindow()
{
delete ui;
}
//-------------帮助向导----------------------//
QWizardPage *createIntroPage()//介绍页
{
QWizardPage *page = new QWizardPage;
page->setTitle("Hello!");
QLabel *label = new QLabel;
QString str = "本软件具有图像预处理、形态学操作、图像滤波、图像分割等四大功能模块.";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
return page;
}
QWizardPage *createIntroPage1()//介绍页1
{
QWizardPage *page = new QWizardPage;
page->setTitle(QObject::trUtf8("图像预处理."));
QLabel *label = new QLabel;
QString str = "主要包括以下五个部分.\n"
"①灰度化."
"②二值化."
"③对比度和亮度."
"④直方图均衡化."
"⑤设置感兴趣区域.";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
label->setWordWrap(true);
return page;
}
QWizardPage *createIntroPage2()//介绍页2
{
QWizardPage *page = new QWizardPage;
page->setTitle(QObject::trUtf8("形态学处理."));
QLabel *label = new QLabel;
QString str = "主要包括以下七个部分.\n"
"①膨胀、腐蚀."
"②开、闭运算."
"③梯度、顶帽、黑帽运算."
"④填充孔洞."
"⑤去除小面积区域."
"⑥形态学重构."
"⑦轮廓的提取及描述.";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
label->setWordWrap(true);
return page;
}
QWizardPage *createIntroPage3()//介绍页3
{
QWizardPage *page = new QWizardPage;
page->setTitle(QObject::trUtf8("图像滤波处理."));
QLabel *label = new QLabel;
QString str = "主要包括以下五种滤波.\n"
"①方框滤波."
"②均值滤波."
"③高斯滤波."
"④中值滤波."
"⑤双边滤波.";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
label->setWordWrap(true);
return page;
}
QWizardPage *createIntroPage4()//介绍页4
{
QWizardPage *page = new QWizardPage;
page->setTitle(QObject::trUtf8("图像分割"));
QLabel *label = new QLabel;
QString str = "主要包括以下五种类型的分割.\n"
"①基于阈值的大津法分割.\n"
"②基于形态学的分水岭算法分割.\n"
"③基于边缘检测的Canny、Sobel、Laplacian算子分割.\n"
"④基于区域的生长法分割.\n"
"⑤基于图论的Grabcut分割.\n"
"⑥基于活动轮廓模型的Leverset分割.\n";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
//label->setWordWrap(true) ;
return page;
}
QWizardPage *createfinishPage()//结束页
{
QWizardPage *page = new QWizardPage;
page->setTitle(QObject::trUtf8("注意事项"));
QLabel *label = new QLabel;
QString str = "Copyright by Dingkeyan";
label->setText(str);
QFont ft;
ft.setPointSize(10);
label->setFont(ft); QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
page->setLayout(layout);
label->setWordWrap(true);
return page;
}
void MainWindow::timerUpdate()//显示时间函数
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->label_time->setText(str);
}
//-------------Mat与QImage的转化----------------------//
QImage cvMat2QImage(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)
{
qDebug() << "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
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
Mat QImage2cvMat(QImage image)
{
cv::Mat mat;
qDebug() << image.format();
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}
//-------------显示图片函数----------------------//
void MainWindow::img_display(Mat image)
{
dispimg = cvMat2QImage(image);
ui->label->clear();
ui->label->setPixmap(QPixmap::fromImage(dispimg));
ui->label->resize(ui->label->pixmap()->size());
}
void MainWindow::img_display_1(Mat image)
{
ui->label_1->setVisible(true);
dispimg = cvMat2QImage(image);
ui->label_1->clear();
ui->label_1->setPixmap(QPixmap::fromImage(dispimg));
ui->label_1->resize(ui->label_1->pixmap()->size());
}
void MainWindow::img_display_2(Mat image)
{
ui->label_2->setVisible(true);
dispimg = cvMat2QImage(image);
ui->label_2->clear();
ui->label_2->setPixmap(QPixmap::fromImage(dispimg));
ui->label_2->resize(ui->label_2->pixmap()->size());
}
//--------------暂时隐藏不需要的部件--------------//
void MainWindow::ui_hide()
{
ui->thresh_Slider->setVisible(false);
ui->label_thresh->setVisible(false);
ui->thresh_Slider_1->setVisible(false);
ui->label_thresh_1->setVisible(false);
ui->comboBox1->setVisible(false);
ui->comboBox2->setVisible(false);
ui->label_1->setVisible(false);
ui->label_2->setVisible(false);
ui->tip2->setVisible(false);
ui->tip3->setVisible(false);
ui->spinBox->setVisible(false);
}
//--------------设置滑块的参数--------------//
void MainWindow::setSlider(int Slider_Min, int Slider_Max, int Slider_Val)
{
ui->thresh_Slider->setVisible(true);//显示滑块
ui->label_thresh->setVisible(true);//显示滑块值
ui->thresh_Slider->setMinimum(Slider_Min);//设置滑动条控件的最小值
ui->thresh_Slider->setMaximum(Slider_Max);//设置滑动条控件的最大值
ui->thresh_Slider->setValue(Slider_Val);//设置滑动条控件的初始值
ui->thresh_Slider->setPageStep(1);//设置滑动条控件的单步间隔值,可惜只能int
}
//--------------设置滑块_1的参数--------------//
void MainWindow::setSlider_1(int Slider_Min, int Slider_Max, int Slider_Val)
{
ui->thresh_Slider_1->setVisible(true);//显示滑块
ui->label_thresh_1->setVisible(true);//显示滑块值
ui->thresh_Slider_1->setMinimum(Slider_Min);//设置滑动条控件的最小值
ui->thresh_Slider_1->setMaximum(Slider_Max);//设置滑动条控件的最大值
ui->thresh_Slider_1->setValue(Slider_Val);//设置滑动条控件的初始值
ui->thresh_Slider_1->setPageStep(1);//设置滑动条控件的单步间隔值,可惜只能int
}
//----------------------开始----------------------//
void MainWindow::on_open_image_triggered()//打开图片
{
QString filename;
filename = QFileDialog::getOpenFileName(this, tr("选择图像"), "", tr("Images(*.png *.bmp *.jpg *.tif *.GIF )"));
QTextCodec *code = QTextCodec::codecForName("gb18030");//为了使用中文路径
std::string name = code->fromUnicode(filename).data();
image = cv::imread(name);//读入原始图像矩阵
image.copyTo(init_img);//把打开的图像保存到全局变量init_img中,用于初始化还原图像
image.copyTo(rep_img);//把图像保存到全局变量rep_img中,用于撤销操作
if (filename.isEmpty())return;
else img_display(image);
ui_hide();//隐藏不需要的部件
thresh = 1;
thresh_1 = 1;
MORPH = 0;
//以下为相对于图片的坐标
x = 0;
y = 0;
x1 = ui->label->width();
y1 = ui->label->height();
ui->tip1->setText(tr("原始图."));
statusBar()->showMessage(tr("已正确打开图片."));
}
void MainWindow::on_print_image_triggered()//打印图片
{
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
QPrinter printer;
QPrintDialog printDialog(&printer, this);
if (printDialog.exec())//判断是否点击了”打印”
{
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = dispimg.size();
size.scale(rect.size(), Qt::KeepAspectRatio);//使图像铺满整个页面
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
painter.setWindow(dispimg.rect());
painter.drawImage(0, 0, dispimg);
}
statusBar()->showMessage(tr("已正确打印图像."));
}
void MainWindow::on_save_image_triggered()//保存图片
{
QString filename;
filename = QFileDialog::getSaveFileName(this, tr("保存图像"), "", tr("Images(*.png)"));
QTextCodec *code = QTextCodec::codecForName("gb18030");//为了使用中文路径
std::string name = code->fromUnicode(filename).data();
if (filename.isEmpty()) return;
else cv::imwrite(name, image);//保存image到指定文件夹下,是BGR格式
statusBar()->showMessage(tr("已正确保存图像."));
}
void MainWindow::on_exit_image_triggered()//退出系统
{
if (!(QMessageBox::information(this, tr("提示"), tr("确定退出系统?"), tr("Yes"), tr("No"))))
{
this->close();
}
}
void MainWindow::on_camera_image_triggered()//打开摄像头
{
VideoCapture capture(0);
while (1) //循环显示每一帧
{
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
if (!capture.isOpened())return;
capture >> frame; //读取当前帧
imshow("Camera", frame); //显示当前帧
char c = waitKey(30); //延时30ms
if (c == 13)//按下Enter保存图像
{
frame.copyTo(image);
img_display(image);
break;
}
if (c == 27)break;//按下ESC退出
}
cvDestroyWindow("Camera");
statusBar()->showMessage(tr("已正确打开摄像头,按Enter键截屏保存."));
}
//-------------工具条操作---------------------//
void MainWindow::on_repeal_image_triggered()//撤销
{
img_display(rep_img);
rep_img.copyTo(image);
statusBar()->showMessage(tr("已撤销上一次图像操作."));
}
void MainWindow::on_init_image_triggered()//还原
{
img_display(init_img);
ui->tip1->setText(tr("原始图."));
init_img.copyTo(image);
ui_hide();
flag = 0;
thresh = 1;
thresh_1 = 1;
MORPH = 0;
interNum = 100;
ui->comboBox2->setCurrentIndex(0);
ui->spinBox->setValue(50);
x = 0;
y = 0;
x1 = ui->label->width();
y1 = ui->label->height();
statusBar()->showMessage(tr("已恢复原始图像."));
ui->tip1->setText(tr("原始图."));
}
void MainWindow::on_OK_image_triggered()//确定
{
flag = 255;
dst_image.copyTo(image);
ui_hide();
statusBar()->showMessage(tr("已确认本操作."));
}
void MainWindow::on_print_image_action_triggered()//打印
{
on_print_image_triggered();
}
void MainWindow::on_open_action_triggered()//打开
{
on_open_image_triggered();
ui_hide();
}
void MainWindow::on_save_action_triggered()//保存
{
on_save_image_triggered();
}
void MainWindow::on_camera_action_triggered()//摄像头
{
on_camera_image_triggered();
}
void MainWindow::on_zoom_in_triggered()//放大
{
QImage dst;
QImage src;
image.copyTo(rep_img);
src = cvMat2QImage(image);
if (src.isNull()) return;
QMatrix matrix;
matrix.scale(1.5, 1.5);
dst = src.transformed(matrix);
Mat dst1 = QImage2cvMat(dst);
img_display(dst1);
dst1.copyTo(image);
statusBar()->showMessage(tr("每次点击放大1.5倍."));
}
void MainWindow::on_zoom_out_triggered()//缩小
{
QImage dst;
QImage src;
image.copyTo(rep_img);
src = cvMat2QImage(image);
if (src.isNull()) return;
QMatrix matrix;
matrix.scale(0.66, 0.66);
dst = src.transformed(matrix);
Mat dst1 = QImage2cvMat(dst);
img_display(dst1);
dst1.copyTo(image);
statusBar()->showMessage(tr("每次点击缩小1.5倍."));
}
void MainWindow::on_information_action_triggered()//帮助
{
//QMessageBox::information(this, tr("help"),tr("Sorry!暂无帮助"));
QWizard* wizard = new QWizard;
wizard->setWindowTitle("Introduction");
wizard->addPage(createIntroPage());
wizard->addPage(createIntroPage1());
wizard->addPage(createIntroPage2());
wizard->addPage(createIntroPage3());
wizard->addPage(createIntroPage4());
wizard->addPage(createfinishPage());
wizard->resize(320, 240);
wizard->show();
}
void MainWindow::on_ROI_action_triggered()//设置ROI
{
flag = 11;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
statusBar()->showMessage(tr("请拖动鼠标选择一个矩形区域."));
}
//-------------鼠标事件---------------------//
void MainWindow::mousePressEvent(QMouseEvent*event)//按下
{
if (event->button() == Qt::LeftButton)//鼠标左键按下
{
x = event->x() - (ui->label->x());
y = event->y() - (ui->label->y()) - 55;//减去menubar+toobar的高度
if (flag == 11)//ROI
{
statusBar()->showMessage(tr("鼠标相对控件坐标:(") + QString::number(x) + tr(",") + QString::number(y) + tr(")"));
}
//qDebug("鼠标相对控件坐标:%d %d ",x,y);
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)//移动
{
if (event->buttons()&Qt::LeftButton)//鼠标左键按下的同时移动鼠标
{
if (flag == 11 | flag == 21)//只在ROI和Grabcut时用
{
QPainter paint;
QPixmap pix;
x1 = event->x() - (ui->label->x());
y1 = event->y() - (ui->label->y()) - 55;
paint.setPen(QColor(Qt::green)); //设定画笔颜色
paint.setBrush(QColor(Qt::red)); //设定填充颜色
// paint.drawRect(QRect(70,70,80,80));
paint.drawRect(QRect(x, y, x1, y1));
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)//释放
{
if (event->button() == Qt::LeftButton)//鼠标左键释放
{
if (flag == 11)//只在ROI时用
{
if (x >= 0 && y >= 0 && x1 <= ui->label->width() && y1 <= ui->label->height())
{
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
cv::Rect rect(x, y, x1 - x, y1 - y);
src(rect).copyTo(dst);
img_display(dst);
dst.copyTo(image);
ui->tip1->setText(tr("ROI图."));
statusBar()->showMessage(tr("已获得ROI图像."));
flag = 0;
x = 0;
y = 0;
x1 = ui->label->width();
y1 = ui->label->height();
}
}
if (flag == 21)//只在Grabcut时用
{
if (x >= 0 && y >= 0 && x1 <= ui->label->width() && y1 <= ui->label->height())
{
Mat dst, src, mask;
image.copyTo(rep_img);
image.copyTo(src);
dst = Mat(src.size(), CV_8UC1, cv::Scalar(cv::GC_BGD));//掩模图像必须是8UC1
if (src.channels() == 1) cvtColor(src, src, CV_GRAY2RGB);//输入图像必须是8UC3
src.convertTo(src, CV_8UC3);
cv::Rect rectangle(x, y, x1 - x, y1 - y);// 矩形外的像素是背景
cv::Mat result;
//两个临时矩阵变量,作为算法的中间变量使用,不用care
cv::Mat bgModel, fgModel;
// GrabCut 分段
cv::grabCut(src, //输入图像
result, //分段结果
rectangle,// 包含前景的矩形
bgModel, fgModel, // 前景、背景
1, // 迭代次数
cv::GC_INIT_WITH_RECT); // 用矩形
// 得到可能是前景的像素
//比较函数保留值为GC_PR_FGD的像素
cv::compare(result, cv::GC_PR_FGD, result, cv::CMP_EQ);
// 产生输出图像
cv::Mat foreground(src.size(), CV_8UC3, cv::Scalar(255, 255, 255));
//背景值为 GC_BGD=0,作为掩码
image.copyTo(foreground, result);
img_display(foreground);
foreground.copyTo(image);
ui->tip1->setText(tr("用GrabCut算法分割后的图."));
statusBar()->showMessage(tr("已对图像用GrabCut算法分割."));
}
}
}
}
//-------------图像增强:灰度化---------------------//
void MainWindow::on_togray_triggered()
{
Mat dst, src;
image.copyTo(rep_img);//处理前图像保存到全局变量rep_img中,用于撤销操作
image.copyTo(src);//把原图像保存到局部变量src中进行操作
if (!src.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
dst.create(src.size(), src.type()); // 创建与src同类型和大小的矩阵(dst)
if (src.channels() == 1) src.copyTo(dst);
else cvtColor(src, dst, CV_BGR2GRAY); //转换为灰度图
img_display(dst);
dst.copyTo(image);//把处理后的图像更新全局变量image中,用于再处理
ui->tip1->setText(tr("灰度图."));
statusBar()->showMessage(tr("已对图像灰度化."));
}
//-------------图像增强:二值化---------------------//
void MainWindow::on_tobinary_before_triggered()
{
flag = 10;
Mat dst, src;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
image.copyTo(rep_img);
image.copyTo(src);
dst.create(src.size(), src.type());
if (src.channels() == 3) { cvtColor(src, src, CV_BGR2GRAY); } //转换为灰度图
threshold(src, dst, thresh, 255, CV_THRESH_BINARY); //二值化
img_display(dst);
if (thresh == 1)thresh = 128;//thresh初始化
setSlider(0, 255, thresh);
dst.copyTo(dst_image);
ui->tip1->setText(tr("二值图."));
statusBar()->showMessage(tr("已对图像二值化."));
}
//-------------图像增强:对比度和亮度---------------------//
void MainWindow::on_contrast_image_triggered()
{
flag = 7;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
int beta = thresh_1;//亮度
float alpha = thresh * 3 / 255;//对比度0-3倍
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
dst.create(src.size(), src.type());
if (src.channels() == 3) //BGR图像
{
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
for (int c = 0; c < 3; c++)
{
dst.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(src.at<Vec3b>(y, x)[c] + beta));
}
}
}
}
else //不是BGR图像
{
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
dst.at<uchar>(y, x) = saturate_cast<uchar>(alpha*(src.at<uchar>(y, x)) + beta);
}
}
}
img_display(dst);
if (thresh == 1)thresh = 60;//thresh初始化
setSlider(0, 255, thresh);
if (thresh_1 == 1)thresh_1 = 60;//thresh初始化
setSlider_1(0, 255, thresh_1);
dst.copyTo(dst_image);//ok键按下时把dstimage保存到image中,便于接下来再处理
ui->tip1->setText(tr("处理后图"));
statusBar()->showMessage(tr("已进行亮度和对比度的调整."));
}
//-------------图像增强:直方图均衡化---------------------//
void MainWindow::on_histogram_equalization_triggered()
{
Mat dst, src;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
image.copyTo(rep_img);
image.copyTo(src);
if (src.channels() == 1)
src.copyTo(dst);
else
cvtColor(src, dst, CV_BGR2GRAY); //转换为灰度图
equalizeHist(src, dst);
img_display(dst);
dst.copyTo(image);
ui->tip1->setText(tr("直方图均衡化后的图."));
statusBar()->showMessage(tr("已对图像进行直方图均衡化."));
}
//-------------设置ROI区域---------------------//
void MainWindow::on_action_ROI_triggered()
{
on_ROI_action_triggered();
}
//-------------图像形态学操作---------------------//
void MainWindow::on_dilate_image_triggered()//膨胀
{
flag = 22;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_DILATE, element, Point(-1, -1), thresh_1);//膨胀
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("膨胀后图"));
statusBar()->showMessage(tr("已对图像进行膨胀."));
}
void MainWindow::on_erode_image_triggered()//腐蚀
{
flag = 1;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_ERODE, element, Point(-1, -1), thresh_1);//腐蚀
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("腐蚀后图"));
statusBar()->showMessage(tr("已对图像进行腐蚀."));
}
void MainWindow::on_opening_image_triggered()//开运算
{
flag = 2;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_OPEN, element, Point(-1, -1), thresh_1);//开运算
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("开运算后的图."));
statusBar()->showMessage(tr("已对图像进行开运算."));
}
void MainWindow::on_closing_image_triggered()//闭运算
{
flag = 3;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_CLOSE, element, Point(-1, -1), thresh_1);//闭运算
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("闭运算后的图"));
statusBar()->showMessage(tr("已对图像进行闭运算."));
}
void MainWindow::on_gradient_image_triggered()//梯度
{
flag = 4;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_GRADIENT, element, Point(-1, -1), thresh_1);//梯度
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("梯度化图"));
statusBar()->showMessage(tr("已对图像进行梯度化."));
}
void MainWindow::on_tophat_image_triggered()//顶帽
{
flag = 5;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_TOPHAT, element, Point(-1, -1), thresh_1);//顶帽
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("顶帽处理后图"));
statusBar()->showMessage(tr("已对图像进行顶帽处理."));
}
void MainWindow::on_blackhat_image_triggered()//黑帽
{
flag = 6;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat dst, src;
image.copyTo(rep_img);
image.copyTo(src);
Mat element = getStructuringElement(MORPH, Size(thresh, thresh));
morphologyEx(src, dst, MORPH_BLACKHAT, element, Point(-1, -1), thresh_1);//黑帽
img_display(dst);
setSlider(1, 15, thresh);
setSlider_1(1, 5, thresh_1);
ui->comboBox1->setVisible(true);
dst.copyTo(dst_image);
ui->tip1->setText(tr("黑帽处理后的图."));
statusBar()->showMessage(tr("已对图像进行黑帽处理."));
}
//----------------------孔洞填充----------------------//
void MainWindow::on_hole_fill_triggered()
{
flag = 8;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat src;
image.copyTo(rep_img);
image.copyTo(src);
Mat dst = Mat::zeros(src.size(), CV_8UC1);
if (src.channels() != 1)
cvtColor(src, src, CV_BGR2GRAY); //src必须为一个2值单通道图像
src.convertTo(src, CV_8UC1);
RemoveSmallRegion(src, dst, thresh, 0, 0);
img_display(dst);
if (thresh == 1)
thresh = 100;//thresh初始化
setSlider(0, 500, thresh);
dst.copyTo(dst_image);
ui->tip1->setText(tr("孔洞填充后的图."));
statusBar()->showMessage(tr("已对图像进行孔洞填充."));
}
//---------------------去除小面积区域----------------------//
void MainWindow::on_deduct_small_area_triggered()
{
flag = 9;
if (!image.data)
{
QMessageBox::warning(this, tr("Warning"), tr("请先打开图片."));
return;
}
Mat src;
image.copyTo(rep_img);
image.copyTo(src);
Mat dst = Mat::zeros(src.size(), CV_8UC1);
if (src.channels() != 1)
cvtColor(src, src, CV_BGR2GRAY); //src必须为一个2值单通道图像
src.convertTo(src, CV_8UC1);
RemoveSmallRegion(src, dst, thresh, 1, 1);
img_display(dst);
if (thresh == 1)thresh = 200;//thresh初始化
setSlider(0, 1500, thresh);
dst.copyTo(dst_image);
ui->tip1->setText(tr("去除设定面积区域后的图."));
statusBar()->showMessage(tr("已对图像去除设定面积区域."));
}
//CheckMode: 0代表去除黑区域,1代表去除白区域; NeihborMode:0代表4邻域,1代表8邻域;记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查
void MainWindow::RemoveSmallRegion(Mat& Src, Mat& Dst, int AreaLimit, int CheckMode, int NeihborMode)
{
int RemoveCount = 0; //记录除去的个数
Mat Pointlabel = Mat::zeros(Src.size(), CV_8UC1);
if (CheckMode == 1)//去除小区域
{
for (int i = 0; i < Src.rows; ++i)
{
uchar* iData = Src.ptr<uchar>(i);
uchar* iLabel = Pointlabel.ptr<uchar>(i);
for (int j = 0; j < Src.cols; ++j)
{
if (iData[j] < 10)
{
iLabel[j] = 3;
}
}
}
}
else//去除孔洞
{
for (int i = 0; i < Src.rows; ++i)
{
uchar* iData = Src.ptr<uchar>(i);
uchar* iLabel = Pointlabel.ptr<uchar>(i);
for (int j = 0; j < Src.cols; ++j)
{
if (iData[j] > 10)
{
iLabel[j] = 3;
}
}
}
}
vector<Point2i> NeihborPos; //记录邻域点位置
NeihborPos.push_back(Point2i(-1, 0));
NeihborPos.push_back(Point2i(1, 0));
NeihborPos.push_back(Point2i(0, -1));
NeihborPos.push_back(Point2i(0, 1));
if (NeihborMode == 1)
{
NeihborPos.push_back(Point2i(-1, -1));
NeihborPos.push_back(Point2i(-1, 1));
NeihborPos.push_back(Point2i(1, -1));
NeihborPos.push_back(Point2i(1, 1));
}
int NeihborCount = 4 + 4 * NeihborMode;
int CurrX = 0, CurrY = 0;
//开始检测
for (int i = 0; i < Src.rows; ++i)
{
uchar* iLabel = Pointlabel.ptr<uchar>(i);
for (int j = 0; j < Src.cols; ++j)
{
if (iLabel[j] == 0)
{
//********开始该点处的检查**********
vector<Point2i> GrowBuffer; //堆栈,用于存储生长点
GrowBuffer.push_back(Point2i(j, i));
Pointlabel.at<uchar>(i, j) = 1;
int CheckResult = 0; //用于判断结果(是否超出大小),0为未超出,1为超出
for (int z = 0; z<GrowBuffer.size(); z++)
{
for (int q = 0; q<NeihborCount; q++)
{
CurrX = GrowBuffer.at(z).x + NeihborPos.at(q).x;
CurrY = GrowBuffer.at(z).y + NeihborPos.at(q).y;
if (CurrX >= 0 && CurrX<Src.cols&&CurrY >= 0 && CurrY<Src.rows)
{