-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtouchylabel.cpp
More file actions
218 lines (181 loc) · 5.2 KB
/
touchylabel.cpp
File metadata and controls
218 lines (181 loc) · 5.2 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
// TODO: properly comment this file
#include "touchylabel.h"
#include <iostream>
#include <QPainter>
#include <QBrush>
#include "connectedcomponent.h"
using namespace std;
TouchyLabel::TouchyLabel(QWidget* &w) : QLabel(w)
{
m_wand_mode = false;
m_wand_threshold = 0;
}
void TouchyLabel::mousePressEvent(QMouseEvent *ev)
{
if (m_wand_mode == false)
m_start_point = ev->pos();
else {
ConnectedComponent wand(m_hsv, ev->pos().x(), ev->pos().y(), m_wand_threshold);
m_wand_points = wand.get_points();
emit magicWanded(&m_wand_points);
}
}
void TouchyLabel::mouseMoveEvent(QMouseEvent *ev)
{
if (m_wand_mode == false) {
QPoint end_point = ev->pos();
correctSize(&m_end_point);
int begin_x = m_start_point.x();
int begin_y = m_start_point.y();
int width = end_point.x() - m_start_point.x();
int height = end_point.y() - m_start_point.y();
m_pixmap_rect = m_pixmap_orig;
QPainter p(&m_pixmap_rect);
p.setPen(Qt::magenta);
p.drawRect(begin_x, begin_y, width, height);
this->setPixmap(m_pixmap_rect);
}
}
void TouchyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
if (m_wand_mode == false) {
m_end_point = ev->pos();
correctSize(&m_end_point);
}
}
void TouchyLabel::correctSize(QPoint *point) {
// correcting for viewport size
if (point->x() > m_pixmap_orig.width() - 1)
point->setX( m_pixmap_orig.width() - 1);
if (point->y() > m_pixmap_orig.height() - 1)
point->setY( m_pixmap_orig.height() - 1);
}
void TouchyLabel::setImage(IplImage *img, IplImage *hsv)
{
m_pixmap_orig = *(this->pixmap());
m_img = img;
m_hsv = hsv;
}
HSVMinMax TouchyLabel::getValues()
{
return m_values;
}
void TouchyLabel::setValues(bool reset)
{
// resetting the old values if required
if (reset) {
m_values.reset();
m_hues.clear();
m_sats.clear();
m_vals.clear();
}
CvMat *mat = cvCreateMat(m_hsv->height, m_hsv->width, CV_32FC3);
cvConvert(m_hsv, mat);
CvScalar scalar;
for (int x = m_start_point.x(); x < m_end_point.x(); ++x) {
for (int y = m_start_point.y(); y < m_end_point.y(); ++y) {
scalar = cvGet2D(mat, y, x);
m_hues.push_back(scalar.val[0]);
m_sats.push_back(scalar.val[1]);
m_vals.push_back(scalar.val[2]);
}
}
// getting the lowest and highest values
int hue_min = 300, sat_min = 300, val_min = 300;
int hue_max = -1, sat_max = -1, val_max = -1;
for (auto& hue : m_hues) {
hue_min = min(hue_min, hue);
hue_max = max(hue_max, hue);
}
for (auto& sat : m_sats) {
sat_min = min(sat_min, sat);
sat_max = max(sat_max, sat);
}
for (auto& val : m_vals) {
val_min = min(val_min, val);
val_max = max(val_max, val);
}
m_hues.clear(); m_sats.clear(); m_vals.clear();
m_hues.push_back(hue_max);
m_hues.push_back(hue_min);
m_sats.push_back(sat_max);
m_sats.push_back(sat_min);
m_vals.push_back(val_max);
m_vals.push_back(val_min);
m_values.hue_min = hue_min;
m_values.hue_max = hue_max;
m_values.sat_min = sat_min;
m_values.sat_max = sat_max;
m_values.val_min = val_min;
m_values.val_max = val_max;
}
IplImage* TouchyLabel::getHSVImage()
{
return m_hsv;
}
void TouchyLabel::toggleWandMode(int threshold)
{
if (m_wand_mode == true)
m_wand_mode = false;
else
m_wand_mode = true;
m_wand_threshold = threshold;
}
bool TouchyLabel::getWandMode()
{
return m_wand_mode;
}
void TouchyLabel::setWandThresh(int threshold)
{
m_wand_threshold = threshold;
}
// sets the threshold according to a given vectors of points
// TODO: merge with setValues
void TouchyLabel::setWandValues(vector<CvPoint> &points, bool reset)
{
if (reset) {
m_values.reset();
m_hues.clear();
m_sats.clear();
m_vals.clear();
}
CvMat *mat = cvCreateMat(m_hsv->height, m_hsv->width, CV_32FC3);
cvConvert(m_hsv, mat);
CvScalar scalar;
int x, y;
for (auto &point : points) {
x = point.x; y = point.y;
scalar = cvGet2D(mat, y, x);
m_hues.push_back(scalar.val[0]);
m_sats.push_back(scalar.val[1]);
m_vals.push_back(scalar.val[2]);
}
// getting the lowest and highest values
int hue_min = 300, sat_min = 300, val_min = 300;
int hue_max = -1, sat_max = -1, val_max = -1;
for (auto& hue : m_hues) {
hue_min = min(hue_min, hue);
hue_max = max(hue_max, hue);
}
for (auto& sat : m_sats) {
sat_min = min(sat_min, sat);
sat_max = max(sat_max, sat);
}
for (auto& val : m_vals) {
val_min = min(val_min, val);
val_max = max(val_max, val);
}
m_hues.clear(); m_sats.clear(); m_vals.clear();
m_hues.push_back(hue_max);
m_hues.push_back(hue_min);
m_sats.push_back(sat_max);
m_sats.push_back(sat_min);
m_vals.push_back(val_max);
m_vals.push_back(val_min);
m_values.hue_min = hue_min;
m_values.hue_max = hue_max;
m_values.sat_min = sat_min;
m_values.sat_max = sat_max;
m_values.val_min = val_min;
m_values.val_max = val_max;
}