-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualthresholddialog.cpp
145 lines (120 loc) · 5.31 KB
/
dualthresholddialog.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
#include "dualthresholddialog.h"
#include <QtWidgets>
DualThresholdDialog::DualThresholdDialog(QWidget *parent, QImage src)
:QDialog(parent)
,image(src)
,srcImg(src)
,imageOperation(new ImageOperations)
{
imageLabel = new QLabel;
imageLabel->setMaximumWidth(500);
QPixmap pix = QPixmap::fromImage(image);
qreal h = pix.height();
qreal w = pix.width();
pix = pix.scaled(500, 500 * h / w, Qt::KeepAspectRatio);
imageLabel->setPixmap(pix);
QVBoxLayout *imageLayout = new QVBoxLayout;
imageLayout->addWidget(imageLabel);
imageLayout->setContentsMargins(10, 10, 10, 10);
QGroupBox *imageGroup = new QGroupBox(tr("View Image"));
imageGroup->setLayout(imageLayout);
QLabel *lowThresholdLabel = new QLabel;
lowThresholdLabel->setText(tr("Value: (from 0 to 255)"));
lowThresholdEdit = new QLineEdit;
lowThresholdEdit->setText("0");
lowThresholdEdit->setMaximumWidth(50);
QHBoxLayout *lowEditLayout = new QHBoxLayout;
lowEditLayout->addWidget(lowThresholdLabel);
lowEditLayout->addWidget(lowThresholdEdit);
lowThresholdSlider = new QSlider(Qt::Horizontal);
lowThresholdSlider->setMinimum(0);
lowThresholdSlider->setMaximum(255);
lowThresholdSlider->setValue(0);
QVBoxLayout *lowLayout = new QVBoxLayout;
lowLayout->addLayout(lowEditLayout);
lowLayout->addWidget(lowThresholdSlider);
lowLayout->setContentsMargins(10, 10, 10, 10);
QGroupBox *lowGroup = new QGroupBox(tr("Low Threshold"));
lowGroup->setLayout(lowLayout);
QLabel *highThresholdLabel = new QLabel;
highThresholdLabel->setText(tr("Value: (from 0 to 255"));
highThresholdEdit = new QLineEdit;
highThresholdEdit->setText("0");
highThresholdEdit->setMaximumWidth(50);
QHBoxLayout *highEditLayout = new QHBoxLayout;
highEditLayout->addWidget(highThresholdLabel);
highEditLayout->addWidget(highThresholdEdit);
highThresholdSlider = new QSlider(Qt::Horizontal);
highThresholdSlider->setMinimum(0);
highThresholdSlider->setMaximum(255);
highThresholdSlider->setValue(0);
QVBoxLayout *highLayout = new QVBoxLayout;
highLayout->addLayout(highEditLayout);
highLayout->addWidget(highThresholdSlider);
highLayout->setContentsMargins(10, 10, 10, 10);
QGroupBox *highGroup = new QGroupBox(tr("High Threshold"));
highGroup->setLayout(highLayout);
QPushButton *applyButton = new QPushButton(tr("Apply"));
QPushButton *closeButton = new QPushButton(tr("Close"));
QHBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addStretch(1);
btnLayout->addWidget(applyButton);
btnLayout->addWidget(closeButton);
QIntValidator *thresholdValidator = new QIntValidator;
thresholdValidator->setRange(0, 255);
lowThresholdEdit->setValidator(thresholdValidator);
highThresholdEdit->setValidator(thresholdValidator);
QVBoxLayout *settingLayout = new QVBoxLayout;
settingLayout->addWidget(lowGroup);
settingLayout->addWidget(highGroup);
settingLayout->addStretch(1);
settingLayout->addSpacing(12);
settingLayout->addLayout(btnLayout);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(imageGroup);
mainLayout->addLayout(settingLayout);
setLayout(mainLayout);
connect(lowThresholdEdit, SIGNAL(textChanged(QString)), this, SLOT(lowThresholdEditChanged(QString)));
connect(highThresholdEdit, SIGNAL(textChanged(QString)), this, SLOT(highThresholdEditChanged(QString)));
connect(lowThresholdSlider, SIGNAL(valueChanged(int)), this, SLOT(lowThresholdSliderChanged(int)));
connect(highThresholdSlider, SIGNAL(valueChanged(int)), this, SLOT(highThresholdSliderChanged(int)));
connect(closeButton, &QAbstractButton::clicked, this, &QWidget::close);
connect(applyButton, &QAbstractButton::clicked, this, &DualThresholdDialog::apply);
}
void DualThresholdDialog::lowThresholdEditChanged(QString str){
int num = str.toInt();
lowThresholdSlider->setValue(num);
QImage newImage = imageOperation->dualThreshold(srcImg, num, highThresholdSlider->value());
updateImage(newImage);
}
void DualThresholdDialog::highThresholdEditChanged(QString str){
int num = str.toInt();
highThresholdSlider->setValue(num);
QImage newImage = imageOperation->dualThreshold(srcImg, lowThresholdSlider->value(), num);
updateImage(newImage);
}
void DualThresholdDialog::lowThresholdSliderChanged(int value){
lowThresholdEdit->setText(QString::number(value));
QImage newImage = imageOperation->dualThreshold(srcImg, lowThresholdSlider->value(), highThresholdSlider->value());
updateImage(newImage);
}
void DualThresholdDialog::highThresholdSliderChanged(int value){
highThresholdEdit->setText(QString::number(value));
QImage newImage = imageOperation->dualThreshold(srcImg, lowThresholdSlider->value(), highThresholdSlider->value());
updateImage(newImage);
}
void DualThresholdDialog::apply(){
QJsonObject obj;
obj.insert("low", lowThresholdEdit->text());
obj.insert("high", highThresholdEdit->text());
emit sendApplyDualThreshold(obj, image);
close();
}
void DualThresholdDialog::updateImage(QImage &newImage){
image = newImage;
QPixmap pix = QPixmap::fromImage(image);
qreal h = pix.height();
qreal w = pix.width();
pix = pix.scaled(500, 500 * h / w, Qt::KeepAspectRatio);
imageLabel->setPixmap(pix);
}