-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdctfilter.c
351 lines (285 loc) · 9.06 KB
/
dctfilter.c
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
// -----------------------------------------------------------------
// Overlapping 2D-DCT (Discrete Cosine Transform) image filter
//
// Image model: z = y + n, where z is the measured image mxn
// y is the noise free image
// n is Gaussian noise
//
// Compile with: g++ -O3 dctfilter.c dct.c -o dctfilter
//
// Mikael Mieskolainen, {2011, 2021}
// -----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <cstring>
#include "dct.h" // 2D-DCT Library
// This abstracts the image matrix as a linear array
// access image matrix pixel at row i and column j, with N columns
// The first for "->" and the second for "." access
#define pixel(data,i,j) ( (data)->image[(i)*((data)->N) + (j)] )
#define pixel_h(data,i,j) ( (data).image[(i)*((data).N) + (j)] )
// This is the image object
struct IMAGE {
IMAGE(int m, int n) {
M = m;
N = n;
};
double *image; // Data
int M; // Number of Rows
int N; // Number of Columns
};
// Subfunction prototypes
void ones(struct IMAGE*);
void zeros(struct IMAGE*);
void printValues(struct IMAGE*, int jump=1);
void pointDiv(struct IMAGE*, struct IMAGE*, struct IMAGE*);
void pointMul(struct IMAGE*, struct IMAGE*, struct IMAGE*);
bool dctfilter(struct IMAGE*, struct IMAGE*, int step, double thresh, int window_size);
double thrfunc(double* outblock, int window_size, double thresh);
// Main program
int main(int argc, char* argv[]) {
char* inputfile;
char* outputfile;
// Image dimensions fixed here (no header in the binary files)
int M = 512; // Rows
int N = 512; // Cols
// Filter parameters
int step = 1; // Local window hopping step
double threshold = 40; // Frequency domain threshold
int bytes = 8; // 64 bit (double) input expected (more formats TBD!)
bool quiet = false; // Print output
int opt;
while ((opt = getopt(argc, argv, "i:o:b:m:n:s:t:q")) != -1) {
switch(opt) {
case 'i':
inputfile = strdup(optarg);
break;
case 'o':
outputfile = strdup(optarg);
break;
case 'b':
bytes = atoi(optarg);
if (bytes < 1) {
printf("-b needs to be int >= 1 \n");
return EXIT_FAILURE;
}
break;
case 'm':
M = atoi(optarg);
if (M < 1) {
printf("-m needs to be integer >= 1 \n");
return EXIT_FAILURE;
}
break;
case 'n':
N = atoi(optarg);
if (N < 1) {
printf("-n needs to be integer >= 1 \n");
return EXIT_FAILURE;
}
break;
case 's':
step = atoi(optarg);
if (step < 1) {
printf("-s needs to be integer >= 1 \n");
return EXIT_FAILURE;
}
break;
case 't':
threshold = atof(optarg);
if (threshold < 0) {
printf("-t needs to be float >= 0 \n");
return EXIT_FAILURE;
}
break;
case 'q':
quiet = true;
break;
case ':':
printf("option needs a value \n");
break;
case '?':
fprintf(stderr, "Usage: %s [-i inputfile] [-o outputfile] [-m nrows] [-n ncols] [-s step] [-t threshold] [-q quiet] \n", argv[0]);
return EXIT_FAILURE;
}
}
if (!quiet) {
printf("-i inputfile: %s \n", inputfile);
printf("-o outputfile: %s \n", outputfile);
printf("-b bytes: %d \n", bytes);
printf("-m nrows: %i \n", M);
printf("-n ncols: %i \n", N);
printf("-s step: %i \n", step);
printf("-t threshold: %0.1f \n", threshold);
}
const int window_size = 8; // Keep it at 8 (2D-DCT function is fixed 8x8)
// ------------------------------------------------
// Create image matrix for the original (noisy) image
// and denoised image
struct IMAGE z(M,N);
struct IMAGE y_est(M,N);
// ------------------------------------------------
// Allocate memory
z.image = (double*) malloc(z.M * z.N * sizeof(double));
y_est.image = (double*) malloc(y_est.M * y_est.N * sizeof(double));
if (z.image == NULL) {
printf("dctfilter: Out of memory with z \n");
return EXIT_FAILURE;
}
if (y_est.image == NULL) {
printf("dctfilter: Out of memory with y_est \n");
return EXIT_FAILURE;
}
// Init matrices with zeros
zeros(&z);
zeros(&y_est);
// ------------------------------------------------
// Read the image
FILE *fid;
fid = fopen(inputfile, "rb");
if (fid) {
int ret = fread(z.image, bytes, z.M * z.N, fid);
fclose(fid);
} else {
printf("dctfilter: Error opening inputfile <%s> ! \n", inputfile);
return EXIT_FAILURE;
}
// ------------------------------------------------
// Filter the image
if (dctfilter(&z, &y_est, step, threshold, window_size)) {
// fine
} else {
printf("dctfilter: Problem in filtering \n");
return EXIT_FAILURE;
}
// ------------------------------------------------
// Write the image
fid = fopen(outputfile, "wb");
if (fid) {
int ret = fwrite(y_est.image, bytes, y_est.M * y_est.N, fid);
fclose(fid);
} else {
printf("dctfilter: Error opening outputfile <%s> ! \n", outputfile);
return EXIT_FAILURE;
}
// Free the image matrices
free(z.image);
free(y_est.image);
if (!quiet) { printf("dctfilter: Filtering done \n"); }
return EXIT_SUCCESS;
}
// 2D-filter function
bool dctfilter(struct IMAGE* z, struct IMAGE* y_est, int step, double thresh, int window_size) {
// Boundary around image matrix
const int b = window_size / 2;
// 2D-DCT 8x8 buffers (matrix row by row as linear array)
double inblock[(window_size*window_size)] = {0};
double outblock[(window_size*window_size)] = {0};
// Allocate the aggregation matrix and weight matrix
struct IMAGE A(z->M, z->N);
struct IMAGE W(z->M, z->N);
A.image = (double*) malloc(A.M * A.N * sizeof(double));
W.image = (double*) malloc(W.M * W.N * sizeof(double));
if (A.image == NULL) { printf("dctfilter: Out of memory with A \n"); return false; }
if (W.image == NULL) { printf("dctfilter: Out of memory with W \n"); return false; }
// Init matrices with zeros
zeros(&A);
zeros(&W);
// Over all rows and columns of the image
for (int i = b; i < z->M - b + 2; i += step) {
for (int j = b; j < z->N - b + 2; j += step) {
double* VP = &inblock[0]; // Current pointer
// Accumulate the local window
for (int m = i-b, k = 0; m < i+b; ++m) {
for (int n = j-b; n < j+b; ++n, ++k) {
// Check boundary
*(VP + k) = (m < z->M && n < z->N) ? pixel(z,m,n) : 0.0;
}
}
// 2D-DCT for the block
fdct(VP, outblock);
// Filtering in frequency domain & weight calculation
const double weight = thrfunc(outblock, window_size, thresh);
// Inverse 2D-DCT for the block
idct(outblock, VP);
// Aggregate the block and weight
for (int m = i-b, k = 0; m < i+b; ++m) {
for (int n = j-b; n < j+b; ++n, ++k) {
if (m < z->M && n < z->N) { // Check boundary
pixel_h(A,m,n) += weight * (*(VP + k));
pixel_h(W,m,n) += weight;
}
}
}
}
}
// Weighted mean of the aggregation buffer
for (int i = 0; i < y_est->M; ++i) {
for (int j = 0; j < y_est->N; ++j) {
pixel(y_est,i,j) = pixel_h(A,i,j) / pixel_h(W,i,j);
}
}
free(A.image);
free(W.image);
return true;
}
// Hard-thresholding and block weight function
double thrfunc(double* outblock, int window_size, double thresh) {
double sum = 0.0;
double sum2 = 0.0;
for (int i = 0; i < (window_size*window_size); ++i, ++outblock) {
const double c = fabs(*outblock);
if (c < thresh) {
*outblock = 0;
} else {
sum += c; // Accumulate coefficient values
sum2 += c*c;
}
}
const double n = window_size*window_size;
const double var = (sum2 - sum*sum/n) / n;
// Weight for this block
const double W = var > 0 ? 1.0/var : 1.0;
return W;
}
// Point wise division: y = a ./ b
void pointDiv(struct IMAGE *y, struct IMAGE *a, struct IMAGE *b) {
for (int i = 0; i < y->M; ++i) {
for (int j = 0; j < y->N; ++j) {
pixel(y,i,j) = pixel(a,i,j) / pixel(b,i,j);
}
}
}
// Point wise multiplication: y = a .* b
void pointMul(struct IMAGE *y, struct IMAGE *a, struct IMAGE *b) {
for (int i = 0; i < y->M; ++i) {
for (int j = 0; j < y->N; ++j) {
pixel(y,i,j) = pixel(a,i,j) * pixel(b,i,j);
}
}
}
// Init with ones
void ones(struct IMAGE *x) {
for (int i = 0; i < x->M; ++i) {
for (int j = 0; j < x->N; ++j) {
pixel(x,i,j) = 1.0;
}
}
}
// Init with zeros
void zeros(struct IMAGE *x) {
for (int i = 0; i < x->M; ++i) {
for (int j = 0; j < x->N; ++j) {
pixel(x,i,j) = 0.0;
}
}
}
void printValues(struct IMAGE *x, int jump) {
for (int i = 0; i < x->M; i += jump) {
for (int j = 0; j < x->N; j += jump) {
printf("Value at [%d,%d] is %0.2f \n", i, j, pixel(x,i,j) );
}
}
}