-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDCTdenoising.cpp
280 lines (248 loc) · 9.51 KB
/
DCTdenoising.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
/*
* Code Copyright (c) 2017, Nicola Pierazzo <[email protected]>,
* Gabriele Facciolo <[email protected]>
* Based on the 2010 article by Guoshen Yu <[email protected]>,
* Guillermo Sapiro <[email protected]>
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*----------------------- Multiscale DCTdenoising -------------------------*/
// This code implements "Multiscale DCT denoising".
// http://www.ipol.im/pub/art/2017/201
// Copyright, Nicola Pierazzo, Gabriele Facciolo, 2017.
// Please report bugs and/or send comments to G. Facciolo [email protected]
/*---------------------------------------------------------------------------*/
#include <cmath>
#include <utility>
#include <tuple>
#include <vector>
#include "Image.hpp"
#include "DCTPatch.hpp"
#include "utils.hpp"
#include "DCTdenoising.h"
#ifdef _OPENMP
#include <omp.h>
#endif
using imgutils::Image;
using imgutils::DCTPatch;
using imgutils::ComputeTiling;
using imgutils::SplitTiles;
using imgutils::MergeTiles;
using std::move;
using std::sqrt;
using std::pair;
using std::abs;
using std::vector;
using std::copy;
constexpr float HARD_THRESHOLD = 3.f;
Image ColorTransform(Image &&src) {
Image img = move(src);
if (img.channels() == 3) {
for (int row = 0; row < img.rows(); ++row) {
for (int col = 0; col < img.columns(); ++col) {
float r, g, b;
r = img.val(col, row, 0);
g = img.val(col, row, 1);
b = img.val(col, row, 2);
img.val(col, row, 0) = (r + g + b) / sqrt(3.f);
img.val(col, row, 1) = (r - b) / sqrt(2.f);
img.val(col, row, 2) = (r - 2 * g + b) / sqrt(6.f);
}
}
}
return img;
}
Image ColorTransformInverse(Image &&src) {
Image img = move(src);
if (img.channels() == 3) {
for (int row = 0; row < img.rows(); ++row) {
for (int col = 0; col < img.columns(); ++col) {
float y, u, v;
y = img.val(col, row, 0);
u = img.val(col, row, 1);
v = img.val(col, row, 2);
img.val(col, row, 0) = (sqrt(2.f) * y + sqrt(3.f) * u + v) / sqrt(6.f);
img.val(col, row, 1) = (y - sqrt(2.f) * v) / sqrt(3.f);
img.val(col, row, 2) = (sqrt(2.f) * y - sqrt(3.f) * u + v) / sqrt(6.f);
}
}
}
return img;
}
inline void ExtractPatch(const Image &src, int pr, int pc, DCTPatch *dst) {
// src is padded, so (pr, pc) becomes the upper left pixel
for (int chan = 0; chan < dst->channels(); ++chan) {
for (int row = 0; row < dst->rows(); ++row) {
// // the following line copies a line interval to the patch and
// // is equivalent toi (but faster):
// for (int col = 0; col < dst->columns(); ++col) {
// dst->space(col, row, chan) = src.val(pc + col, pr + row, chan);
// }
copy(&(src.val(pc, pr + row, chan)),
&src.val(pc + dst->columns(), pr + row, chan),
&dst->space(0, row, chan));
}
}
}
/*! \brief DCT denoising steps: Hard thresholding and Wiener
*
* This funciton implemets both steps, when guided==false the hard
* thresholding step is applied and the guide image is ignored.
* Otherwise the Wiener filtering step is applied using guide as oracle.
* Returns a pair containing the aggregated patches and weights
*
* Functions step1, and step2 provide an alternative interface to this one.
*/
inline pair<Image, Image> DCTsteps(const Image &noisy, const Image &guide,
const float sigma, const int dct_sz,
bool adaptive_aggregation, const bool guided) {
Image result(noisy.rows(), noisy.columns(), noisy.channels());
Image weights(noisy.rows(), noisy.columns());
DCTPatch patch(dct_sz, dct_sz, noisy.channels());
DCTPatch gpatch(dct_sz, dct_sz, noisy.channels()); // unused if !guided
for (int pr = 0; pr <= noisy.rows() - dct_sz; ++pr) {
for (int pc = 0; pc <= noisy.columns() - dct_sz; ++pc) {
// starts processing of a single patch
float wP = 0; // adaptive aggregation weight
ExtractPatch(noisy, pr, pc, &patch);
patch.ToFreq();
if (guided) {
ExtractPatch(guide, pr, pc, &gpatch);
gpatch.ToFreq();
}
for (int chan = 0; chan < noisy.channels(); ++chan) {
for (int row = 0; row < dct_sz; ++row) {
for (int col = 0; col < dct_sz; ++col) {
if (guided) { // Wiener filtering with oracle guide
if (row || col) {
float G = gpatch.freq(col, row, chan);
float w = (G * G) / (G * G + sigma * sigma);
patch.freq(col, row, chan) *= w;
// add to weights excluding DC
wP += w*w;
}
} else { // Hard thresholding
if (row || col) {
if (abs(patch.freq(col, row, chan)) < HARD_THRESHOLD * sigma) {
patch.freq(col, row, chan) = 0.f;
} else { // count ALL nonzero frequencies excluding DC
wP++;
}
}
}
}
}
}
patch.ToSpace();
wP = 1.f/(1.f + wP);
if (!adaptive_aggregation)
wP = 1.f;
// Aggregation of the patch
for (int ch = 0; ch < noisy.channels(); ++ch) {
for (int row = 0; row < dct_sz; ++row) {
for (int col = 0; col < dct_sz; ++col) {
result.val(col + pc, row + pr, ch) += patch.space(col, row, ch)*wP;
}
}
}
for (int row = 0; row < dct_sz; ++row) {
for (int col = 0; col < dct_sz; ++col) {
weights.val(col + pc, row + pr) += 1.f*wP;
}
}
}
}
return {move(result), move(weights)};
}
/*! \brief wrapper for DCTsteps in case of Hard thresholding
*
* Returns a pair containing the aggregated patches and weights
*/
inline pair<Image, Image> step1(const Image &noisy,
const float sigma, const int dct_sz,
bool adaptive_aggregation) {
Image dummyguide; // dummy guide
return DCTsteps(noisy, dummyguide, sigma, dct_sz, adaptive_aggregation, false);
}
/*! \brief wrapper for DCTsteps in case of Wiener filtering
*
* Returns a pair containing the aggregated patches and weights
*/
inline pair<Image, Image> step2(const Image &noisy, const Image &guide,
const float sigma, const int dct_sz,
bool adaptive_aggregation) {
return DCTsteps(noisy, guide, sigma, dct_sz, adaptive_aggregation, true);
}
/*! \brief Denoise an image with sliding window DCT denoising
*
* If guided==false, then guide is ignored and hard thresholding is applied,
* otherwise Wiener filtering is applied using guide as oracle.
* Returns a denoised image
*/
inline Image DCTdenoisingBoth(const Image &noisy, const Image &guide, float sigma,
int dct_sz, bool adaptive_aggregation,
const bool guided, int nthreads) {
#ifdef _OPENMP
if (!nthreads) nthreads = omp_get_max_threads(); // number of threads
#else
nthreads = 1;
#endif // _OPENMP
int r = dct_sz / 2; // half patch size for the padding
// compute tiling parameters
pair<int, int> tiling = ComputeTiling(noisy.rows(), noisy.columns(),
nthreads);
// prepare image tiles
vector<Image> noisy_tiles, guide_tiles;
noisy_tiles = SplitTiles(ColorTransform(noisy.copy()),
r, dct_sz - r, tiling);
if (guided) {
guide_tiles = SplitTiles(ColorTransform(guide.copy()),
r, dct_sz - r, tiling);
}
vector<pair<Image, Image>> result_tiles(nthreads);
// parallel-process the tiles
#pragma omp parallel for num_threads(nthreads)
for (int i = 0; i < nthreads; ++i) {
if (guided) { // Wiener filtering
result_tiles[i] = step2(noisy_tiles[i], guide_tiles[i],
sigma, dct_sz, adaptive_aggregation);
} else { // Hard thresholding
result_tiles[i] = step1(noisy_tiles[i],
sigma, dct_sz, adaptive_aggregation);
}
}
// combine tiles and invert color transform
return ColorTransformInverse(MergeTiles(result_tiles, noisy.shape(), r,
dct_sz - r, tiling));
}
/*! \brief wrapper for DCTdenoisingBoth: Wiener filtering case (guided)
*
* Returns the denoised image
*/
Image DCTdenoisingGuided(const Image &noisy, const Image &guide, float sigma,
int dct_sz, bool adaptive_aggregation, int nthreads) {
return DCTdenoisingBoth(noisy, guide, sigma, dct_sz,
adaptive_aggregation, true, nthreads);
}
/*! \brief wrapper for DCTdenoisingBoth: Hard thresholding case
*
* Returns the denoised image
*/
Image DCTdenoising(const Image &noisy, float sigma,
int dct_sz, bool adaptive_aggregation, int nthreads) {
Image dummyguide;
return DCTdenoisingBoth(noisy, dummyguide, sigma, dct_sz,
adaptive_aggregation, false, nthreads);
}