-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.cpp
294 lines (278 loc) · 9.99 KB
/
utils.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
//
// Created by Nicola Pierazzo on 21/10/15.
//
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <utility>
#include <fftw3.h>
#include "utils.hpp"
using std::pair;
using std::sqrt;
using std::vector;
using std::move;
using std::max;
using std::min;
namespace imgutils {
// use isometric DCT for multiscale decompose/recompose
#define ISOMETRIC_DCT
inline int SymmetricCoordinate(int pos, int size) {
if (pos < 0) pos = -pos - 1;
if (pos >= 2 * size) pos %= 2 * size;
if (pos >= size) pos = 2 * size - 1 - pos;
return pos;
}
/*! \brief Compute best tiling of image in at most ntiles
*
* Returns the pair: rows, columns
*/
pair<int, int> ComputeTiling(int rows, int columns, int ntiles) {
// The objective is extract ntiles square-ish tiles
// For a square image the optimal number of rows is sqrt(ntiles)
// The ratio rows/columns permits to handle rectangular images
float best_r = sqrt(static_cast<float>(ntiles * rows) / columns);
int r_low = static_cast<int>(best_r);
int r_up = r_low + 1;
if (r_low < 1) return {1, ntiles}; // single row
if (r_up > ntiles) return {ntiles, 1}; // single column
// look for the nearest integer divisors of ntiles
while (ntiles % r_low != 0) --r_low;
while (ntiles % r_up != 0) ++r_up;
// At this point there are two possible tilings:
// {r_low, ntiles / r_low} and {r_up, ntiles / r_up}.
// We need to select the best.
// To do that, we consider the shape of the tiles.
// In the first case, the tiles are roughly
// {rows / r_low, columns * r_low / ntiles} pixels.
// In the second case, the tiles are
// {rows / r_up, columns * r_up / ntiles} pixels.
// Since r_low <= best_r <= r_up the first tile will have i
// more rows than columns and vice-versa.
//
// To select the best case we consider the ratio between the
// lengths of the longer and the shorter edge of a tile.
// The closer this ratio is to 1, the "squarer" the tile will be.
// In other words, we select the first tiling if
// (rows / r_low) / (columns * r_low / ntiles) <
// (columns * r_up / ntiles) / (rows / r_up)
// That is equivalent to (all values are > 0):
// rows * ntiles < r_up * r_low * columns
if (r_up * r_low * columns > ntiles * rows) {
return {r_low, ntiles / r_low};
} else {
return {r_up, ntiles / r_up};
}
}
/*! \brief Split image in tiles
*
* Returns a vector containing tiling.first x tiling.sencond images
* each padded by pad_* pixels. Tiles are stored in lexicographic order.
* Padding outside the image is done by symmetrization
*/
vector<Image> SplitTiles(const Image &src, int pad_before, int pad_after,
pair<int, int> tiling) {
vector<Image> result;
for (int tr = 0; tr < tiling.first; ++tr) {
int rstart = src.rows() * tr / tiling.first - pad_before;
int rend = src.rows() * (tr + 1) / tiling.first + pad_after;
for (int tc = 0; tc < tiling.second; ++tc) {
int cstart = src.columns() * tc / tiling.second - pad_before;
int cend = src.columns() * (tc + 1) / tiling.second + pad_after;
// copy image to tile using the above computed limits
Image tile(rend - rstart, cend - cstart, src.channels());
for (int ch = 0; ch < src.channels(); ++ch) {
for (int row = rstart; row < rend; ++row) {
for (int col = cstart; col < cend; ++col) {
tile.val(col - cstart, row - rstart, ch) = src.val(
SymmetricCoordinate(col, src.columns()),
SymmetricCoordinate(row, src.rows()),
ch);
}
}
}
result.push_back(move(tile));
}
}
return result;
}
/*! \brief Recompose tiles produced by SplitTiles
*
* Returns an image resulting of recomposing the tiling
* padded margins are averaged in the result
*/
Image MergeTiles(const vector<pair<Image, Image>> &src, pair<int, int> shape,
int pad_before, int pad_after, pair<int, int> tiling) {
int channels = src[0].first.channels();
Image result(shape.first, shape.second, channels);
Image weights(shape.first, shape.second);
auto tile = src.begin();
for (int tr = 0; tr < tiling.first; ++tr) {
int rstart = shape.first * tr / tiling.first - pad_before;
int rend = shape.first * (tr + 1) / tiling.first + pad_after;
for (int tc = 0; tc < tiling.second; ++tc) {
int cstart = shape.second * tc / tiling.second - pad_before;
int cend = shape.second * (tc + 1) / tiling.second + pad_after;
// copy tile to image using the above computed limits
for (int ch = 0; ch < channels; ++ch) {
for (int row = max(0, rstart); row < min(shape.first, rend); ++row) {
for (int col = max(0, cstart); col < min(shape.second, cend); ++col) {
result.val(col, row, ch) +=
tile->first.val(col - cstart, row - rstart, ch);
}
}
}
// image weight due to the padding
for (int row = max(0, rstart); row < min(shape.first, rend); ++row) {
for (int col = max(0, cstart); col < min(shape.second, cend); ++col) {
weights.val(col, row) += tile->second.val(col - cstart, row - rstart);
}
}
++tile;
}
}
// normalize by the weight
for (int ch = 0; ch < channels; ++ch) {
for (int row = 0; row < shape.first; ++row) {
for (int col = 0; col < shape.second; ++col) {
result.val(col, row, ch) /= weights.val(col, row);
}
}
}
return result;
}
/*! \brief 2D DCT transform of image (channel-wise)
*
* Operates over the img in-place
* Computes the normalized but not orthogonal 2D DCT
*/
void dct_inplace(Image &img) {
int n[] = {img.rows(), img.columns()};
fftwf_r2r_kind dct2[] = {FFTW_REDFT10, FFTW_REDFT10};
fftwf_plan plan = fftwf_plan_many_r2r(2, n, img.channels(), img.data(), NULL,
1, img.pixels(), img.data(), NULL, 1,
img.pixels(), dct2, FFTW_ESTIMATE);
fftwf_execute(plan);
fftwf_destroy_plan(plan);
#ifdef ISOMETRIC_DCT
////> isometric normalization
// this normalization (and scaling) affects several other functions:
// idct_inplace, decompose, recompose
// but they can all be removed only by applying the Normalization below
double norm_factor = sqrt(.25f / (img.rows() * img.columns()));
for (int ch = 0; ch < img.channels(); ++ch) {
for (int row = 0; row < img.rows(); ++row) {
img.val(0, row, ch) /= sqrt(2.f);
for (int col = 0; col < img.columns(); ++col) {
img.val(col, row, ch) *= norm_factor;
}
}
for (int col = 0; col < img.columns(); ++col) {
img.val(col, 0, ch) /= sqrt(2.f);
}
}
#else
////> Normalization
for (int i = 0; i < img.samples(); ++i) {
img.val(i) /= 4 * img.pixels();
}
#endif
}
/*! \brief 2D inverse DCT transform of image (channel-wise)
*
* Operates over the img in-place
*/
void idct_inplace(Image &img) {
#ifdef ISOMETRIC_DCT
////> isometric normalization
long double norm_factor = sqrt(.25f / (img.rows() * img.columns()));
for (int ch = 0; ch < img.channels(); ++ch) {
for (int row = 0; row < img.rows(); ++row) {
img.val(0, row, ch) *= sqrt(2.f);
for (int col = 0; col < img.columns(); ++col) {
img.val(col, row, ch) *= norm_factor;
}
}
for (int col = 0; col < img.columns(); ++col) {
img.val(col, 0, ch) *= sqrt(2.f);
}
}
#endif
int n[] = {img.rows(), img.columns()};
fftwf_r2r_kind idct2[] = {FFTW_REDFT01, FFTW_REDFT01};
fftwf_plan plan = fftwf_plan_many_r2r(2, n, img.channels(), img.data(), NULL,
1, img.pixels(), img.data(), NULL, 1,
img.pixels(), idct2, FFTW_ESTIMATE);
fftwf_execute(plan);
fftwf_destroy_plan(plan);
}
/*! \brief Dyadic DCT pyramid decomposition.
*
* Returns a vector containing #levels images
*/
vector<Image> decompose(const Image &img, int levels) {
vector<Image> pyramid;
Image freq = img.copy();
dct_inplace(freq);
int h{freq.rows()}, w{freq.columns()};
for (int i = 0; i < levels; ++i) {
// Copy data
Image layer(h, w, freq.channels());
#ifdef ISOMETRIC_DCT
////> isometric normalization scaling
const double scaling = std::sqrt((double)(w*h)/((double)(img.rows()*img.columns())));
#else
const double scaling = 1.0;
#endif
for (int ch = 0; ch < freq.channels(); ++ch) {
for (int r = 0; r < h; ++r) {
for (int c = 0; c < w; ++c) {
layer.val(c, r, ch) = freq.val(c, r, ch) * scaling;
}
}
}
// Inverse DCT
idct_inplace(layer);
w /= 2;
h /= 2;
pyramid.push_back(move(layer));
}
return pyramid;
}
/*! \brief Dyadic DCT pyramid conservative recomposition.
*
* Takes a vector containing the layers of the pyramid. Computes
* the transform of each layer and recomposes the frequencies in a
* fine-to-coarse fashion, copying into the final result only the
* lowest recompose_factor frequencies of each coarse layer.
* After inverting the DCT, returns the resulting image.
*/
Image recompose(const vector<Image> &pyramid, float recompose_factor) {
// Use the bigger image to determine width, height and number of channels
Image output = pyramid[0].copy();
// Perform the DCT
dct_inplace(output);
for (int i = 1; i < static_cast<int>(pyramid.size()); ++i) {
// Read level i of the pyramid
Image layer = pyramid[i].copy();
// Perform the DCT
dct_inplace(layer);
#ifdef ISOMETRIC_DCT
////> isometric normalization scaling
const double scaling = std::sqrt((double)(output.rows()*output.columns())/((double)(layer.rows()*layer.columns())));
#else
const double scaling = 1.0;
#endif
// Copy data (selected by recompose_factor)
for (int ch = 0; ch < layer.channels(); ++ch) {
for (int r = 0; r < layer.rows() * recompose_factor; ++r) {
for (int c = 0; c < layer.columns() * recompose_factor; ++c) {
output.val(c, r, ch) = layer.val(c, r, ch) * scaling;
}
}
}
}
// IDCT of the output image
idct_inplace(output);
return output;
}
} // namespace imgutils