-
Notifications
You must be signed in to change notification settings - Fork 0
/
quality.cpp
378 lines (306 loc) · 10.2 KB
/
quality.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
#include "quality.h"
#include <vector>
#include <cstring>
#include <cmath>
using namespace byteimage;
#define EPS 0.01
double* Entropy::pdf(const ByteImage& img, const Window& w) const {
double* p = new double [range()];
memset(p, 0, range() * sizeof(double));
for (int r = w.r; r < w.r + w.nr; r++)
for (int c = w.c; c < w.c + w.nc; c++)
p[img.at(r, c) >> nshifts]++;
for (int i = 0; i < range(); i++)
p[i] /= w.size();
return p;
}
double* Entropy::pdf(const ByteImage& img0, const ByteImage& img1, const Window& w) const {
double* p = new double [range() * range()];
memset(p, 0, range() * range() * sizeof(double));
int x, y;
for (int r = w.r; r < w.r + w.nr; r++)
for (int c = w.c; c < w.c + w.nc; c++) {
x = img0.at(r, c) >> nshifts;
y = img1.at(r, c) >> nshifts;
p[x * range() + y]++;
}
for (int i = 0; i < range() * range(); i++)
p[i] /= w.size();
return p;
}
double* Entropy::pdf_x(double* joint) const {
double* p = new double [range()];
memset(p, 0, range() * sizeof(double));
for (int x = 0; x < range(); x++)
for (int y = 0; y < range(); y++)
p[x] += joint[x * range() + y];
return p;
}
double* Entropy::pdf_y(double* joint) const {
double* p = new double [range()];
memset(p, 0, range() * sizeof(double));
for (int x = 0; x < range(); x++)
for (int y = 0; y < range(); y++)
p[y] += joint[x * range() + y];
return p;
}
double Entropy::operator()(const ByteImage& img, const Window& w) const {
if (img.nchannels > 1) return (*this)(img.toGrayscale(), w);
double* p = pdf(img, w);
double H = 0.0;
for (int i = 0; i < range(); i++) {
if (!p[i]) continue;
H -= p[i] * log2(p[i]);
}
delete [] p;
return H;
}
double Entropy::operator()(const ByteImage& img0, const ByteImage& img1, const Window& w) const {
if (img0.nchannels > 1 || img1.nchannels > 1) return (*this)(img0.toGrayscale(), img1.toGrayscale(), w);
double* p = pdf(img0, img1, w);
double H = 0.0;
for (int i = 0; i < range() * range(); i++) {
if (!p[i]) continue;
H -= p[i] * log2(p[i]);
}
delete [] p;
return H;
}
#include <vector>
class SparseArray {
public:
class Count {
public:
Byte x, y, z;
unsigned count;
Count(Byte x, Byte y, Byte z) {
this->x = x; this->y = y; this->z = z;
count = 1;
}
};
std::vector<Count> v[256];
void inc(Byte x, Byte y, Byte z) {
Byte addr = x & y & z;
int ind;
for (ind = 0; ind < v[addr].size(); ind++)
if (v[addr][ind].x == x && v[addr][ind].y == y && v[addr][ind].z == z) break;
if (ind == v[addr].size())
v[addr].push_back(Count(x, y, z));
else
v[addr][ind].count++;
}
};
double Entropy::operator()(const ByteImage& img0, const ByteImage& img1, const ByteImage& img2, const Window& w) const {
if (img0.nchannels > 1 || img1.nchannels > 1 || img2.nchannels > 1) return (*this)(img0.toGrayscale(), img1.toGrayscale(), img2.toGrayscale(), w);
SparseArray array;
Byte x, y, z;
for (int r = w.r; r < w.r + w.nr; r++)
for (int c = w.c; c < w.c + w.nc; c++) {
x = img0.at(r, c) >> nshifts;
y = img1.at(r, c) >> nshifts;
z = img2.at(r, c) >> nshifts;
array.inc(x, y, z);
}
double p, H = 0.0;
for (int addr = 0; addr < 256; addr++)
for (int ind = 0; ind < array.v[addr].size(); ind++) {
p = (double)array.v[addr][ind].count / w.size();
H -= p * log2(p);
}
return H;
}
double Entropy::cond(const ByteImage& img0, const ByteImage& img1, const Window& w) const {
if (img0.nchannels > 1 || img1.nchannels > 1) return cond(img0.toGrayscale(), img1.toGrayscale(), w);
double* pxy = pdf(img0, img1, w);
double* py = pdf_y(pxy);
double sum = 0.0;
for (int x = 0; x < range(); x++)
for (int y = 0; y < range(); y++) {
if (!pxy[x * range() + y]) continue;
sum += pxy[x * range() + y] * log2(py[y] / pxy[x * range() + y]);
}
delete [] pxy;
delete [] py;
return sum;
}
double Entropy::I(const ByteImage& img0, const ByteImage& img1, const Window& w) const {
if (img0.nchannels > 1 || img1.nchannels > 1) return I(img0.toGrayscale(), img1.toGrayscale(), w);
double* pxy = pdf(img0, img1, w);
double* px = pdf_x(pxy);
double* py = pdf_y(pxy);
double sum = 0.0;
for (int x = 0; x < range(); x++)
for (int y = 0; y < range(); y++) {
if (!pxy[x * range() + y]) continue;
sum += pxy[x * range() + y] * log2(pxy[x * range() + y] / (px[x] * py[y]));
}
delete [] pxy;
delete [] px;
delete [] py;
return sum;
}
double QualityMeasure::SSIM(const ByteImage& a, const ByteImage& b, const Window& w) const {
if (a.nchannels > 1 || b.nchannels > 1)
return SSIM(a.toGrayscale(), b.toGrayscale(), w);
//Expected values
double va, vb, Sa = 0.0, Sb = 0.0, Saa = 0.0, Sab = 0.0, Sbb = 0.0;
const double c1 = 0.01, c2 = 0.01;
for (int r = w.r; r < w.r + w.nr; r++)
for (int c = w.c; c < w.c + w.nc; c++) {
va = a.at(r, c);
vb = b.at(r, c);
Sa += va;
Sb += vb;
Saa += va * va;
Sbb += vb * vb;
Sab += va * vb;
}
const double mean_a = Sa / w.size();
const double mean_b = Sb / w.size();
const double var_a = Saa / w.size() - mean_a * mean_a;
const double var_b = Sbb / w.size() - mean_b * mean_b;
const double cov = Sab / w.size() - mean_a * mean_b;
return ((2.0 * mean_a * mean_b + c1) / (mean_a * mean_a + mean_b * mean_b + c1)) * ((2.0 * cov + c2) / (var_a + var_b + c2));
}
double QualityMeasure::CAPT(const ByteImage& a, const ByteImage& b, const ByteImage& f, const Window& w) const {
const double c = 0.01;
double Ha = entropy(a, w);
double Hab = entropy(a, b, w);
double Haf = entropy(a, f, w);
double Habf = entropy(a, b, f, w);
return (Hab + Haf - Habf - Ha + c) / (Hab - Ha + c);
}
ByteImage QualityMeasure::visualizeQuality(const ByteImage& a, const ByteImage& b, const ByteImage& f) const {
if (a.nchannels > 1 || b.nchannels > 1 || f.nchannels > 1)
return visualizeQuality(a.toGrayscale(), b.toGrayscale(), f.toGrayscale());
/* Determine windows */
std::vector<Window> windows;
{
Window window(0, 0, window_size, window_size);
for (window.r = (a.nr % window_size) / 2;
window.r + window.nr <= a.nr;
window.r += window.nr)
for (window.c = (a.nc % window_size) / 2;
window.c + window.nc <= a.nc;
window.c += window.nc)
windows.push_back(window);
}
/* Compute saliency measure */
std::vector<double> Sa(windows.size()), Sb(windows.size()), Sf(windows.size());
for (int i = 0; i < windows.size(); i++) {
Sa[i] = entropy(a, windows[i]);
Sb[i] = entropy(b, windows[i]);
Sf[i] = entropy(f, windows[i]);
}
/* Compute final quality measure map */
ByteImage result(f.nr, f.nc);
double Q, lambda;
Byte v;
for (int i = 0; i < windows.size(); i++) {
lambda = Sa[i] / (Sa[i] + Sb[i]);
if (Sa[i] == 0.0 && Sb[i] == 0.0) lambda = 0.5;
if (lambda < EPS)
Q = SSIM(b, f, windows[i]);
else if (1.0 - lambda < EPS)
Q = SSIM(a, f, windows[i]);
else
Q = (lambda * SSIM(a, f, windows[i]) + (1.0 - lambda) * SSIM(b, f, windows[i]));
if (Q >= 1.0) v = 255;
else if (Q <= 0.0) v = 0;
else v = (unsigned char)(255.0 * Q);
for (int r = 0; r < windows[i].nr; r++)
for (int c = 0; c < windows[i].nc; c++)
result.at(windows[i].r + r, windows[i].c + c) = v;
}
return result;
}
double QualityMeasure::Q_A(const ByteImage& a, const ByteImage& b, const ByteImage& f) const {
if (a.nchannels > 1 || b.nchannels > 1 || f.nchannels > 1)
return Q_A(a.toGrayscale(), b.toGrayscale(), f.toGrayscale());
/* Determine windows */
std::vector<Window> windows;
{
Window window(0, 0, window_size, window_size);
for (window.r = (a.nr % window_size) / 2;
window.r + window.nr <= a.nr;
window.r += window.nr)
for (window.c = (a.nc % window_size) / 2;
window.c + window.nc <= a.nc;
window.c += window.nc)
windows.push_back(window);
}
/* Compute saliency measure */
std::vector<double> Sa(windows.size()), Sb(windows.size()), Sf(windows.size());
for (int i = 0; i < windows.size(); i++) {
Sa[i] = entropy(a, windows[i]);
Sb[i] = entropy(b, windows[i]);
Sf[i] = entropy(f, windows[i]);
}
/* Compute window weights */
std::vector<double> C(windows.size());
if (perceptual) {
double sum = 0.0;
for (int i = 0; i < C.size(); i++)
sum += (C[i] = Sa[i] + Sb[i]);
for (int i = 0; i < C.size(); i++)
C[i] /= sum;
}
else {
for (int i = 0; i < C.size(); i++)
C[i] = 1.0 / C.size();
}
/* Compute final quality measure */
const double c = 0.01, s2 = sqrt(2.0);
double Q = 0.0, lambda;
for (int i = 0; i < windows.size(); i++) {
Q += C[i] * 0.5 * (SSIM(a, f, windows[i]) + CAPT(a, b, f, windows[i]));
}
return Q;
}
double QualityMeasure::operator()(const ByteImage& a, const ByteImage& b, const ByteImage& f) const {
if (a.nchannels > 1 || b.nchannels > 1 || f.nchannels > 1)
return (*this)(a.toGrayscale(), b.toGrayscale(), f.toGrayscale());
/* Determine windows */
std::vector<Window> windows;
{
Window window(0, 0, window_size, window_size);
for (window.r = (a.nr % window_size) / 2;
window.r + window.nr <= a.nr;
window.r += window.nr)
for (window.c = (a.nc % window_size) / 2;
window.c + window.nc <= a.nc;
window.c += window.nc)
windows.push_back(window);
}
/* Compute saliency measure */
std::vector<double> Sa(windows.size()), Sb(windows.size()), Sf(windows.size());
for (int i = 0; i < windows.size(); i++) {
Sa[i] = entropy(a, windows[i]);
Sb[i] = entropy(b, windows[i]);
Sf[i] = entropy(f, windows[i]);
}
/* Compute window weights */
std::vector<double> C(windows.size());
if (perceptual) {
double sum = 0.0;
for (int i = 0; i < C.size(); i++)
sum += (C[i] = Sa[i] + Sb[i]);
for (int i = 0; i < C.size(); i++)
C[i] /= sum;
}
else {
for (int i = 0; i < C.size(); i++)
C[i] = 1.0 / C.size();
}
/* Compute final quality measure */
double Q = 0.0, lambda;
double qa, qb;
for (int i = 0; i < windows.size(); i++) {
if (Sa[i] == 0.0 && Sb[i] == 0.0) continue;
lambda = Sa[i] / (Sa[i] + Sb[i]);
qa = SSIM(a, f, windows[i]);
qb = SSIM(b, f, windows[i]);
Q += C[i] * (lambda * qa + (1.0 - lambda) * qb);
}
return Q;
}