-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.cpp
512 lines (433 loc) · 14.7 KB
/
mask.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// This program is free software: you can use, modify and/or redistribute it
// under the terms of the simplified BSD License. You should have received a
// copy of this license along this program. If not, see
// <http://www.opensource.org/licenses/bsd-license.html>.
//
// Copyright (C) 2018, Thibaud Briand <[email protected]>
// Copyright (C) 2015, Javier Sánchez Pérez <[email protected]>
// Copyright (C) 2014, Nelson Monzón López <[email protected]>
// All rights reserved.
#include "mask.h"
#include <math.h>
#include <stdio.h>
#include <assert.h>
/** Macro to get the number of elements in a static array */
#define NUMEL(x) (sizeof(x)/sizeof(*(x)))
struct gradientStruct
{
/** prefilter **/
double *k;
/** differentiator */
double *d;
/** size **/
int size;
};
/* Definition of the gradient estimators */
//Central
static double kCentral[3] = {0.0, 1.0, 0.0};
static double dCentral[3] = {-0.5, 0.0, 0.5};
//Hypomode
static double kHypomode[2] = {0.5, 0.5};
static double dHypomode[2] = {-1 , 1};
//Farid 3x3
static double kFarid3[3] = {0.229879, 0.540242, 0.229879};
static double dFarid3[3] = {-0.455271, 0.0, 0.455271};
//Farid 5x5
static double kFarid5[5] = {0.037659, 0.249153, 0.426375, 0.249153, 0.037659};
static double dFarid5[5] = {-0.109604, -0.276691, 0, 0.276691, 0.109604};
//Gaussian sigma = 0.3
static double kGaussian3[3] = {0.003865, 0.999990, 0.003865};
static double dGaussian3[3] = {-0.707110, 0.0, 0.707110};
//Gaussian sigma = 0.6
static double kGaussian5[5] = {0.003645, 0.235160, 0.943070, 0.235160, 0.003645};
static double dGaussian5[5] = {-0.021915,-0.706770, 0, 0.706770, 0.021915};
//Store the gradients in a table
static gradientStruct gradientTable[] = {
{kCentral, dCentral, 3},
{kHypomode, dHypomode, 2},
{kFarid3, dFarid3, 3},
{kFarid5, dFarid5, 5},
{kGaussian3, dGaussian3, 3},
{kGaussian5, dGaussian5, 5}
};
/**
*
* Compute the gradient with central differences
*
*/
void
gradient (double *input, //input image
double *dx, //computed x derivative
double *dy, //computed y derivative
int nx, //image width
int ny, //image height
int nz //number of color channels in the image
)
{
int nx_rgb = nx * nz;
for (int index_color = 0; index_color < nz; index_color++)
{
//gradient in the center body of the image
for (int i = 1; i < ny - 1; i++)
{
for (int j = 1; j < nx - 1; j++)
{
int k = (i * nx + j) * nz + index_color;
dx[k] = 0.5 * (input[k + nz] - input[k - nz]);
dy[k] = 0.5 * (input[k + nx_rgb] - input[k - nx_rgb]);
}
}
//gradient in the first and last rows
for (int j = 1; j < nx - 1; j++)
{
int index = j * nz + index_color;
dx[index] = 0.5 * (input[index + nz] - input[index - nz]);
dy[index] = 0.5 * (input[index + nx_rgb] - input[index]);
int k = ((ny - 1) * nx + j) * nz + index_color;
dx[k] = 0.5 * (input[k + nz] - input[k - nz]);
dy[k] = 0.5 * (input[k] - input[k - nx_rgb]);
}
//gradient in the first and last columns
for (int i = 1; i < ny - 1; i++)
{
int p = (i * nx_rgb) + index_color;
dx[p] = 0.5 * (input[p + nz] - input[p]);
dy[p] = 0.5 * (input[p + nx_rgb] - input[p - nx_rgb]);
int k = ((i + 1) * nx - 1) * nz + index_color;
dx[k] = 0.5 * (input[k] - input[k - nz]);
dy[k] = 0.5 * (input[k + nx_rgb] - input[k - nx_rgb]);
}
//calculate the gradient in the corners
dx[index_color] = 0.5 * (input[index_color + nz] - input[index_color]);
dy[index_color] =
0.5 * (input[nx_rgb + index_color] - input[index_color]);
int corner_up_right = (nx - 1) * nz + index_color;
dx[corner_up_right] =
0.5 * (input[corner_up_right] - input[corner_up_right - nz]);
dy[corner_up_right] =
0.5 * (input[(2 * nx_rgb) + index_color - nz] -
input[corner_up_right]);
int corner_down_left = ((ny - 1) * nx) * nz + index_color;
dx[corner_down_left] =
0.5 * (input[corner_down_left + nz] - input[corner_down_left]);
dy[corner_down_left] =
0.5 * (input[corner_down_left] -
input[(ny - 2) * nx_rgb + index_color]);
int corner_down_right = ny * nx_rgb - nz + index_color;
dx[corner_down_right] =
0.5 * (input[corner_down_right] - input[corner_down_right - nz]);
dy[corner_down_right] =
0.5 * (input[corner_down_right] -
input[(ny - 1) * nx_rgb - nz + index_color]);
}
}
/**
*
* Convolution of the rows of an image with a kernel
*
*/
static void
convolution_rows (
double *I, //input/output image
int xdim, //image width
int ydim, //image height
int zdim, //number of color channels in the image
double *kernel, //kernel
int kdim, //kernel length
int bc //boundary condition
)
{
int i, j, k;
// kernel is of the form [ lpadding values, center, rpadding values ]
int kcenter = (kdim - 1)/2;
int lpadding = kcenter;
int rpadding = kdim/2;
// buffer taking into account the boundary condition
int Bdim = lpadding + xdim + rpadding;
double *B = new double[Bdim];
// position of the boundaries in the buffer
int bdx = xdim + lpadding;
//Loop for every channel
for(int index_color = 0; index_color < zdim; index_color++){
//convolution of each line of the input image
for (k = 0; k < ydim; k++) {
// construct buffer for the line k
for (i = lpadding; i < bdx; i++)
B[i] = I[(k * xdim + i - lpadding) * zdim + index_color];
switch (bc)
{
case 0: //Dirichlet boundary conditions
for (i = 0; i < lpadding; i++)
B[i] = 0;
for (j = bdx; j < Bdim; j++)
B[j] = 0;
break;
case 1: //Reflecting boundary conditions (wsym)
for (i = 0; i < lpadding; i++)
B[i] = I[(k * xdim + lpadding - i ) * zdim + index_color];
for (j = bdx; j < Bdim; j++)
B[j] = I[(k * xdim + xdim + bdx - j - 2) * zdim + index_color ];
break;
case 2: //Periodic boundary conditions
for (i = 0; i < lpadding; i++)
B[i] = I[(k * xdim + xdim - lpadding + i) * zdim + index_color];
for (j = bdx; j < Bdim; j++)
B[j] = I[(k * xdim + j - bdx) * zdim + index_color];
break;
}
// convolution of the line k
for (i = lpadding; i < bdx; i++) {
double sum = 0;
for (int j = 0; j < kdim; j++)
sum += B[i-lpadding+j]*kernel[j];
// update I
I[(k * xdim + i - lpadding) * zdim + index_color] = sum;
}
}
}
delete[]B;
}
/**
*
* Convolution of the columns of an image with a kernel
*
*/
static void
convolution_columns (
double *I, //input/output image
int xdim, //image width
int ydim, //image height
int zdim, //number of color channels in the image
double *kernel, //kernel
int kdim, //kernel length
int bc //boundary condition
)
{
int i, j, k;
// kernel is of the form [ lpadding values, center, rpadding values ]
int kcenter = (kdim - 1)/2;
int lpadding = kcenter;
int rpadding = kdim/2;
// buffer taking into account the boundary condition
int Bdim = lpadding + ydim + rpadding;
double *B = new double[Bdim];
// position of the boundaries in the buffer
int bdy = ydim + lpadding;
//Loop for every channel
for(int index_color = 0; index_color < zdim; index_color++){
//convolution of each column of the input image
for (k = 0; k < xdim; k++) {
// construct buffer for the column k
for (i = lpadding; i < bdy; i++)
B[i] = I[((i - lpadding) * xdim + k) * zdim + index_color];
switch (bc)
{
case 0: //Dirichlet boundary conditions
for (i = 0; i < lpadding; i++)
B[i] = 0;
for (j = bdy; j < Bdim; j++)
B[j] = 0;
break;
case 1: //Reflecting boundary conditions
for (i = 0; i < lpadding; i++)
B[i] = I[((lpadding - i) * xdim + k ) * zdim + index_color];
for (j = bdy; j < Bdim; j++)
B[j] = I[((bdy + ydim - j - 2) * xdim + k) * zdim + index_color ];
break;
case 2: //Periodic boundary conditions
for (i = 0; i < lpadding; i++)
B[i] = I[((ydim - lpadding + i) * xdim + k) * zdim + index_color];
for (j = bdy; j < Bdim; j++)
B[j] = I[((j - bdy) * xdim + k) * zdim + index_color];
break;
}
// convolution of the line k
for (i = lpadding; i < bdy; i++) {
double sum = 0;
for (int j = 0; j < kdim; j++)
sum += B[i-lpadding+j]*kernel[j];
// update I
I[((i - lpadding) * xdim + k) * zdim + index_color] = sum;
}
}
}
delete[]B;
}
/**
*
* Compute the gradient estimator
* dx = d * k^t * I
* dy = k * d^t * I
* where * denotes the convolution operator
*
*/
void
gradient_robust (double *input, //input image
double *dx, //computed x derivative
double *dy, //computed y derivative
int nx, //image width
int ny, //image height
int nz, //number of color channels in the image
int gradientType //type of gradient
)
{
//kernel definition
assert(gradientType < (int) NUMEL(gradientTable));
double *kernel = gradientTable[gradientType].k;
double *differentiator = gradientTable[gradientType].d;
int nkernel = gradientTable[gradientType].size;
int bc = 1;
//initialization
for(int i = 0; i < nx*ny*nz; i++)
dx[i] = dy[i] = input[i];
//x derivative computation
//convolution of each column (k^t * I)
convolution_columns(dx, nx, ny , nz, kernel, nkernel, bc);
//convolution of each line (d * (k^t * I))
convolution_rows(dx, nx, ny , nz, differentiator, nkernel, bc);
//y derivative computation
//convolution of each column (d^t * I)
convolution_columns(dy, nx, ny , nz, differentiator, nkernel, bc);
//convolution of each line (k * (d^t * dx))
convolution_rows(dy, nx, ny , nz, kernel, nkernel, bc);
}
/**
*
* Prefiltering of an image compatible with the gradient
* I <-- k * k^t * I
* where * denotes the convolution operator
*
*/
void
prefiltering_robust (
double *I, //input/output image
int nx, //image width
int ny, //image height
int nz, //number of color channels in the image
int gradientType //type of gradient
)
{
// kernel definition
assert(gradientType < (int) NUMEL(gradientTable));
double *kernel = gradientTable[gradientType].k;
int nkernel = gradientTable[gradientType].size;
int bc = 1;
//convolution of each line of the input image
convolution_rows(I, nx, ny , nz, kernel, nkernel, bc);
//convolution of each column of the input image
convolution_columns(I, nx, ny , nz, kernel, nkernel, bc);
}
/**
*
* Convolution with a Gaussian kernel
*
*/
void
gaussian (
double *I, //input/output image
int xdim, //image width
int ydim, //image height
int zdim, //number of color channels in the image
double sigma, //Gaussian sigma
int bc, //boundary condition
int precision //defines the size of the window
)
{
int i, j, k;
double den = 2 * sigma * sigma;
int size = (int) (precision * sigma) + 1;
int bdx = xdim + size;
int bdy = ydim + size;
if (bc && size > xdim){
printf("GaussianSmooth: sigma too large for this bc\n");
throw 1;
}
//compute the coefficients of the 1D convolution kernel
double *B = new double[size];
for (int i = 0; i < size; i++)
B[i] = 1 / (sigma * sqrt (2.0 * 3.1415926)) * exp (-i * i / den);
double norm = 0;
//normalize the 1D convolution kernel
for (int i = 0; i < size; i++)
norm += B[i];
norm *= 2;
norm -= B[0];
for (int i = 0; i < size; i++)
B[i] /= norm;
double *R = new double[size + xdim + size];
double *T = new double[size + ydim + size];
//Loop for every channel
for(int index_color = 0; index_color < zdim; index_color++){
//convolution of each line of the input image
for (k = 0; k < ydim; k++)
{
for (i = size; i < bdx; i++)
R[i] = I[(k * xdim + i - size) * zdim + index_color];
switch (bc)
{
case 0: //Dirichlet boundary conditions
for (i = 0, j = bdx; i < size; i++, j++)
R[i] = R[j] = 0;
break;
case 1: //Reflecting boundary conditions
for (i = 0, j = bdx; i < size; i++, j++)
{
R[i] = I[(k * xdim + size - i ) * zdim + index_color];
R[j] = I[(k * xdim + xdim - i - 1) * zdim + index_color ];
}
break;
case 2: //Periodic boundary conditions
for (i = 0, j = bdx; i < size; i++, j++)
{
R[i] = I[(k * xdim + xdim - size + i) * zdim + index_color];
R[j] = I[(k * xdim + i) * zdim + index_color];
}
break;
}
for (i = size; i < bdx; i++)
{
double sum = B[0] * R[i];
for (int j = 1; j < size; j++)
sum += B[j] * (R[i - j] + R[i + j]);
I[(k * xdim + i - size) * zdim + index_color] = sum;
}
}
//convolution of each column of the input image
for (k = 0; k < xdim; k++)
{
for (i = size; i < bdy; i++)
T[i] = I[((i - size) * xdim + k) * zdim + index_color];
switch (bc)
{
case 0: // Dirichlet boundary conditions
for (i = 0, j = bdy; i < size; i++, j++)
T[i] = T[j] = 0;
break;
case 1: // Reflecting boundary conditions
for (i = 0, j = bdy; i < size; i++, j++)
{
T[i] = I[((size - i) * xdim + k) * zdim + index_color];
T[j] = I[((ydim - i - 1) * xdim + k) * zdim + index_color];
}
break;
case 2: // Periodic boundary conditions
for (i = 0, j = bdx; i < size; i++, j++)
{
T[i] = I[((ydim - size + i) * xdim + k) * zdim + index_color];
T[j] = I[(i * xdim + k) * zdim + index_color];
}
break;
}
for (i = size; i < bdy; i++)
{
double sum = B[0] * T[i];
for (j = 1; j < size; j++)
sum += B[j] * (T[i - j] + T[i + j]);
I[((i - size) * xdim + k) * zdim + index_color] = sum;
}
}
}
delete[]B;
delete[]R;
delete[]T;
}