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 | /**
* @file PatchWork.cpp
*
* Sorted list evaluated non-overlaping patches
*/
#include "PatchWork.h"
#include <Tools/ColorClasses.h>
using namespace naoth;
void PatchWork::subsampling(const Image& image, std::vector<unsigned char>& result, int x0, int y0, int x1, int y1, int size)
{
x0 = std::max(0, x0);
y0 = std::max(0, y0);
x1 = std::min((int)image.width()-1, x1);
y1 = std::min((int)image.height()-1, y1);
result.resize(size*size);
double width_step = static_cast<double>(x1 - x0) / static_cast<double>(size);
double height_step = static_cast<double>(y1 - y0) / static_cast<double>(size);
int xi = 0;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
int yi = 0;
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
unsigned char yy = image.getY((int)(x + 0.5), (int)(y + 0.5));
result[xi*size + yi] = yy;
yi++;
}
xi++;
}
}
void PatchWork::subsampling(const Image& image, const FieldColorPercept& fielColorPercept, std::vector<BallCandidates::ClassifiedPixel>& result, int x0, int y0, int x1, int y1, int size)
{
x0 = std::max(0, x0);
y0 = std::max(0, y0);
x1 = std::min((int)image.width()-1, x1);
y1 = std::min((int)image.height()-1, y1);
result.resize(size*size);
double width_step = static_cast<double>(x1 - x0) / static_cast<double>(size);
double height_step = static_cast<double>(y1 - y0) / static_cast<double>(size);
int xi = 0;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
int yi = 0;
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
BallCandidates::ClassifiedPixel& p = result[xi*size + yi];
image.get((int)(x + 0.5), (int)(y + 0.5), p.pixel);
if(fielColorPercept.greenHSISeparator.noColor(p.pixel)) {
p.c = (unsigned char)ColorClasses::white;
} else if(fielColorPercept.greenHSISeparator.isChroma(p.pixel)) {
p.c = (unsigned char)ColorClasses::green;
} else {
p.c = (unsigned char)ColorClasses::none;
}
yi++;
}
xi++;
}
}
void PatchWork::multiplyBrightness(double factor, std::vector<unsigned char>& patch) {
if(factor == 1.0) {
// nothing to do
return;
}
for(unsigned int i = 0; i < patch.size(); i++) {
double originalY = patch[i];
double multipliedY = originalY * factor;
double clippedY = Math::clamp(multipliedY, 0.0, 255.0);
patch[i] = (unsigned char) Math::round(clippedY);
}
}
void PatchWork::multiplyBrightness(double factor, BallCandidates::PatchYUVClassified& patch) {
if(factor == 1.0) {
// nothing to do
return;
}
for(unsigned int i = 0; i < patch.data.size(); i++)
{
double originalY = patch.data[i].pixel.y;
double multipliedY = originalY * factor;
double clippedY = Math::clamp(multipliedY, 0.0, 255.0);
patch.data[i].pixel.y = (unsigned char) Math::round(clippedY);
}
}
void PatchWork::toPatch(const BallCandidates::PatchYUVClassified &src, BallCandidates::Patch &target)
{
target.data.clear();;
target.min = src.min;
target.max = src.max;
for(std::vector<BallCandidates::ClassifiedPixel>::const_iterator it=src.data.begin();
it != src.data.end(); it++)<--- Prefer prefix ++/-- operators for non-primitive types. [+]Prefix ++/-- operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.
{
const BallCandidates::ClassifiedPixel& origPixel = *it;
target.data.push_back(origPixel.pixel.y);
}
}
BallCandidates::PatchesList PatchWork::toPatchList(const BallCandidates::PatchYUVClassifiedList &src)
{
std::list<BallCandidates::Patch> result;
for(BallCandidates::PatchYUVClassifiedList::const_iterator it=src.begin();
it != src.end(); it++)<--- Prefer prefix ++/-- operators for non-primitive types. [+]Prefix ++/-- operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.
{
BallCandidates::Patch p;
toPatch(*it, p);
result.push_back(p);
}
return result;
}
double PatchWork::calculateContrast(const BallCandidates::PatchYUVClassified& patch)
{
// average of all non green pixel
double avg = 0;
double cnt = 0;
for(const BallCandidates::ClassifiedPixel& p: patch.data) {
if(p.c != ColorClasses::green) {
avg += p.pixel.y;
cnt++;
}
}
// we got only green ?!
if(cnt == 0) {
return 0;
}
// expected value -> average/mean
avg /= cnt;
// variance of all non green pixel
double variance = 0;
for(const BallCandidates::ClassifiedPixel& p: patch.data) {
if(p.c != ColorClasses::green) {
double t = static_cast<double>(p.pixel.y) - avg;
variance += t*t;
}
}
return std::sqrt(variance / cnt);
}
double PatchWork::calculateContrastIterative(const BallCandidates::PatchYUVClassified& patch)
{
double K = -1; // inidicating first pixel
double n = 0;
double sum_ = 0;
double sum_sqr = 0;
for(const BallCandidates::ClassifiedPixel& p: patch.data) {
if(p.c != ColorClasses::green) {
if(K == -1) {
K = p.pixel.y;
}
n++;
sum_ += p.pixel.y - K;
sum_sqr += (p.pixel.y-K)*(p.pixel.y-K);
}
}
// make sure we got some none-green pixels!
if(n == 0) {
return 0;
}
return std::sqrt((sum_sqr - (sum_ * sum_)/n)/(n));
}
double PatchWork::calculateContrastIterative2nd(const BallCandidates::PatchYUVClassified& patch)
{
double n = 0;
double mean = 0.0;
double M2 = 0.0;
for(const BallCandidates::ClassifiedPixel& p: patch.data) {
if(p.c != ColorClasses::green) {
n++;
double delta = p.pixel.y - mean;
mean += delta/n;
double delta2 = p.pixel.y - mean;
M2 += delta*delta2;
}
}
// make sure we got some none-green pixels!
if(n < 2) {
return 0;
}
return std::sqrt(M2 / (n-1));
}
double PatchWork::calculateContrast(const Image& image, const FieldColorPercept& fielColorPercept, int x0, int y0, int x1, int y1, int size)
{
x0 = std::max(0, x0);
y0 = std::max(0, y0);
x1 = std::min((int)image.width()-1, x1);
y1 = std::min((int)image.height()-1, y1);
double width_step = static_cast<double>(x1 - x0) / static_cast<double>(size);
double height_step = static_cast<double>(y1 - y0) / static_cast<double>(size);
double avg = 0;
double cnt = 0;
BallCandidates::ClassifiedPixel p;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
image.get((int)(x + 0.5), (int)(y + 0.5), p.pixel);
if(!fielColorPercept.greenHSISeparator.isColor(p.pixel)) { // no green!
avg += p.pixel.y;
cnt++;
}
}
}
// we got only green ?!
if(cnt == 0) { return 0; }
// expected value -> average/mean
avg /= cnt;
double variance = 0;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
image.get((int)(x + 0.5), (int)(y + 0.5), p.pixel);
if(!fielColorPercept.greenHSISeparator.isColor(p.pixel)) { // green!
auto t = p.pixel.y;
variance += (t-avg)*(t-avg);
}
}
}
variance /= cnt;
return std::sqrt(variance);
}
double PatchWork::calculateContrastIterative(const Image& image, const FieldColorPercept& fielColorPercept, int x0, int y0, int x1, int y1, int size)
{
x0 = std::max(0, x0);
y0 = std::max(0, y0);
x1 = std::min((int)image.width()-1, x1);
y1 = std::min((int)image.height()-1, y1);
double width_step = static_cast<double>(x1 - x0) / static_cast<double>(size);
double height_step = static_cast<double>(y1 - y0) / static_cast<double>(size);
double K = -1; // inidicating first pixel
double n = 0;
double sum_ = 0;
double sum_sqr = 0;
BallCandidates::ClassifiedPixel p;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
image.get((int)(x + 0.5), (int)(y + 0.5), p.pixel);
if(!fielColorPercept.greenHSISeparator.isColor(p.pixel)) { // no green!
if(K == -1) { K = p.pixel.y; }
n++;
sum_ += p.pixel.y - K;
sum_sqr += (p.pixel.y-K)*(p.pixel.y-K);
}
}
}
// make sure we got some none-green pixels!
if(n == 0) {
return 0;
}
return std::sqrt((sum_sqr - (sum_ * sum_)/n)/(n));
}
double PatchWork::calculateContrastIterative2nd(const Image& image, const FieldColorPercept& fielColorPercept, int x0, int y0, int x1, int y1, int size)
{
x0 = std::max(0, x0);
y0 = std::max(0, y0);
x1 = std::min((int)image.width()-1, x1);
y1 = std::min((int)image.height()-1, y1);
double width_step = static_cast<double>(x1 - x0) / static_cast<double>(size);
double height_step = static_cast<double>(y1 - y0) / static_cast<double>(size);
double n = 0;
double mean = 0.0;
double M2 = 0.0;
BallCandidates::ClassifiedPixel p;
for(double x = x0 + width_step/2.0; x < x1; x += width_step)
{
for(double y = y0 + height_step/2.0; y < y1; y += height_step)
{
image.get((int)(x + 0.5), (int)(y + 0.5), p.pixel);
if(!fielColorPercept.greenHSISeparator.isColor(p.pixel)) { // no green!
n++;
double delta = p.pixel.y - mean;
mean += delta/n;
double delta2 = p.pixel.y - mean;
M2 += delta*delta2;
}
}
}
// make sure we got some none-green pixels!
if(n < 2) {
return 0;
}
return std::sqrt(M2 / (n-1));
}
|