-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
256 lines (201 loc) · 6.74 KB
/
mainwindow.cpp
File metadata and controls
256 lines (201 loc) · 6.74 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "hsvminmax.h"
#include <iostream>
#include <sstream>
#include <QString>
#include <QStringList>
#include <QShortcut>
#include <cstdlib>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
shortcut_next(QKeySequence("Right"), this),
shortcut_prev(QKeySequence("Left"), this),
shortcut_togglethresh(QKeySequence("t"), this)
{
ui->setupUi(this);
/*
* variable initializations
*/
m_image = 0;
m_image_hsv = 0;
// setting up the imageLabel
ui->imageLabel->setBackgroundRole(QPalette::Base);
ui->imageLabel->setAlignment(Qt::AlignTop & Qt::AlignLeft);
// hiding some elements at first
ui->imageLabel->setVisible(false);
ui->output_text->setVisible(false);
// resizing to the smallest necessary size
this->resize(0, 0);
m_cur_is_threshed = false;
// update the statusbar
updateStatus();
/*
* connections
*/
connect(ui->button_load, SIGNAL(clicked()), this, SLOT(onLoad()));
connect(ui->button_values, SIGNAL(clicked()), this, SLOT(onGetHSV()));
connect(ui->button_add, SIGNAL(clicked()), this, SLOT(onAdd()));
connect(ui->button_previous, SIGNAL(clicked()), this, SLOT(onPrevious()));
connect(ui->button_next, SIGNAL(clicked()), this, SLOT(onNext()));
connect(ui->button_thresh, SIGNAL(clicked()), this, SLOT(onThresh()));
connect(ui->button_wand, SIGNAL(clicked()), this, SLOT(onToggleWand()));
connect(ui->imageLabel, SIGNAL(magicWanded(vector<CvPoint>*)), this, SLOT(onWanded(vector<CvPoint>*)));
connect(ui->input_mwand, SIGNAL(textChanged(QString)), this, SLOT(onThreshChange(QString)));
connect(this, SIGNAL(pathChanged(QString)), this, SLOT(onPathChange(QString)));
connect(&shortcut_next, SIGNAL(activated()), this, SLOT(onNext()));
connect(&shortcut_prev, SIGNAL(activated()), this, SLOT(onPrevious()));
connect(&shortcut_togglethresh, SIGNAL(activated()), this, SLOT(onThresh()));
}
MainWindow::~MainWindow()
{
delete ui;
}
/*
* slots
*/
void MainWindow::onLoad()
{
QStringList paths = QFileDialog::getOpenFileNames(this, "Choose your images", ".");
// setting the member variables
m_paths = paths.toVector().toStdVector();
m_cur_path = m_paths.begin();
// make the "next" button available if more than 1 image is loaded
if (m_paths.size() > 1)
ui->button_next->setEnabled(true);
ui->button_thresh->setEnabled(true);
emit pathChanged(*m_cur_path);
}
void MainWindow::onPathChange(QString p)
{
// updating the pixmap/graphicsscene
m_pixmap = QPixmap(p);
// updating the underlying IplImage
if (m_image != 0)
cvReleaseImage(&m_image);
if (m_image_hsv != 0)
cvReleaseImage(&m_image_hsv);
ui->imageLabel->setPixmap(m_pixmap);
this->resize(0, 0);
m_image = cvLoadImage(p.toStdString().c_str());
m_image_hsv = cvCreateImage(cvGetSize(m_image), 8, 3);
cvCvtColor(m_image, m_image_hsv, CV_BGR2HSV);
// passing the iplImage to the QLabel and making it visible
// if it isn't already
ui->imageLabel->setImage(m_image, m_image_hsv);
if (!ui->imageLabel->isVisible())
ui->imageLabel->setVisible(true);
}
void MainWindow::onGetHSV()
{
ui->imageLabel->setValues();
updateOutput();
}
void MainWindow::onAdd()
{
ui->imageLabel->setValues(false);
updateOutput();
}
// scrolls to the previous image
void MainWindow::onPrevious()
{
// decrement the iterator
m_cur_path--;
ui->button_next->setEnabled(true);
if (m_cur_path == m_paths.begin()) {
ui->button_previous->setEnabled(false);
}
// remove the threshold-flag
m_cur_is_threshed = false;
emit pathChanged(*m_cur_path);
}
// scrolls to the next image
void MainWindow::onNext()
{
// increment the iterator
m_cur_path++;
ui->button_previous->setEnabled(true);
if ((m_cur_path + 1) == m_paths.end()) {
ui->button_next->setEnabled(false);
}
// remove the threshold-flag
m_cur_is_threshed = false;
emit pathChanged(*m_cur_path);
}
void MainWindow::onThresh()
{
if (!m_cur_is_threshed) {
// disabling the value butotns
ui->button_add->setEnabled(false);
ui->button_values->setEnabled(false);
IplImage *hsv = ui->imageLabel->getHSVImage();
IplImage *threshed = cvCreateImage(cvGetSize(hsv), 8, 1);
HSVMinMax v = ui->imageLabel->getValues();
CvScalar lower = cvScalar(v.hue_min, v.sat_min, v.val_min);
CvScalar upper = cvScalar(v.hue_max, v.sat_max, v.val_max);
cvInRangeS(hsv, lower, upper, threshed);
cvSaveImage("temp.png", threshed);
m_cur_is_threshed = true;
emit pathChanged(QString("./temp.png"));
}
else {
// enabling the value butotns
ui->button_add->setEnabled(true);
ui->button_values->setEnabled(true);
m_cur_is_threshed = false;
emit pathChanged(*m_cur_path);
}
}
void MainWindow::updateOutput()
{
// set it to be visible if it wasn't already
if (!ui->output_text->isVisible())
ui->output_text->setVisible(true);
HSVMinMax minmax = ui->imageLabel->getValues();
stringstream sstr;
// hue
sstr << "hue_min = " << (int) minmax.hue_min << endl;
sstr << "hue_max = " << (int) minmax.hue_max << endl;
// saturation
sstr << "sat_min = " << (int) minmax.sat_min << endl;
sstr << "sat_max = " << (int) minmax.sat_max << endl;
// value
sstr << "val_min = " << (int) minmax.val_min << endl;
sstr << "val_max = " << (int) minmax.val_max << endl;
ui->output_text->setPlainText(QString(sstr.str().c_str()));
ui->output_text->update();
}
void MainWindow::onToggleWand()
{
int threshold = atoi(ui->input_mwand->text().toStdString().c_str());
ui->imageLabel->toggleWandMode(threshold);
updateStatus();
}
void MainWindow::updateStatus()
{
if (ui->imageLabel->getWandMode() == true)
ui->statusBar->showMessage("Wand mode: Enabled");
else
ui->statusBar->showMessage("Wand mode: Disabled");
}
// TODO: make the drawing of points reversible
void MainWindow::onWanded(vector<CvPoint> *points)
{
// calculate the hsv range of the wanded points and update the output
ui->imageLabel->setWandValues(*points);
updateOutput();
// update the image to show which points have been selected
IplImage *hsv = ui->imageLabel->getHSVImage();
for (auto &p : *points) {
cvSet2D(m_image, p.y, p.x, cvScalar(255, 0, 0));
}
cvSaveImage("blobbed.png", m_image);
emit pathChanged(QString("./blobbed.png"));
}
void MainWindow::onThreshChange(QString p)
{
int threshold = atoi(p.toStdString().c_str());
ui->imageLabel->setWandThresh(threshold);
}