/
median_filt3d.c
401 lines (368 loc) · 12.7 KB
/
median_filt3d.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
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
// Copyright (c) 2015, UChicago Argonne, LLC. All rights reserved.
// Copyright 2015. UChicago Argonne, LLC. This software was produced
// under U.S. Government contract DE-AC02-06CH11357 for Argonne National
// Laboratory (ANL), which is operated by UChicago Argonne, LLC for the
// U.S. Department of Energy. The U.S. Government has rights to use,
// reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
// UChicago Argonne, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
// ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
// modified to produce derivative works, such modified software should
// be clearly marked, so as not to confuse it with the version available
// from ANL.
// Additionally, redistribution and use in source and binary forms, with
// or without modification, are permitted provided that the following
// conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
// * Neither the name of UChicago Argonne, LLC, Argonne National
// Laboratory, ANL, the U.S. Government, nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY UChicago Argonne, LLC AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UChicago
// Argonne, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// C-module for median filtration and dezingering (3D and 2D cases)
// Original author: Daniil Kazantsev, Diamond Light Source Ltd.
#include <math.h>
#include <omp.h>
#include <stdlib.h>
#include <string.h>
#include "libtomo/median_filt3d.h"
int floatcomp(const void* elem1, const void* elem2)
{
if(*(const float*)elem1 < *(const float*)elem2)
return -1;
return *(const float*)elem1 > *(const float*)elem2;
}
int uint16comp(const void* elem1, const void* elem2)
{
if(*(const unsigned short*)elem1 < *(const unsigned short*)elem2)
return -1;
return *(const unsigned short*)elem1 > *(const unsigned short*)elem2;
}
void
medfilt3D_float(float* Input, float* Output, int radius, int sizefilter_total,
float mu_threshold, long i, long j, long k, size_t index, size_t dimX,
size_t dimY, size_t dimZ)
{
float* ValVec;
long i_m;
long j_m;
long k_m;
long i1;
long j1;
long k1;
long long counter;
int midval;
size_t index1;
midval = (sizefilter_total / 2);
ValVec = (float*) calloc(sizefilter_total, sizeof(float));
/* filling the allocated vector with the neighbouring values */
counter = 0LL;
for(i_m = -radius; i_m <= radius; i_m++)
{
i1 = i + i_m;
if((i1 < 0) || (i1 >= dimX))
i1 = i;
for(j_m = -radius; j_m <= radius; j_m++)
{
j1 = j + j_m;
if((j1 < 0) || (j1 >= dimY))
j1 = j;
for(k_m = -radius; k_m <= radius; k_m++)
{
k1 = k + k_m;
if((k1 < 0) || (k1 >= dimZ))
k1 = k;
index1 = dimX * dimY * (size_t)k1 + (size_t)j1 * dimX + (size_t)i1;
ValVec[counter] = Input[index1];
counter++;
}
}
}
qsort(ValVec, sizefilter_total, sizeof(float), floatcomp);
if(mu_threshold == 0.0F)
{
/* perform median filtration */
Output[index] = ValVec[midval];
}
else
{
/* perform dezingering */
if(fabsf(Input[index] - ValVec[midval]) >= mu_threshold)
Output[index] = ValVec[midval];
}
free(ValVec);
}
void
medfilt2D_float(float* Input, float* Output, int radius, int sizefilter_total,
float mu_threshold, long i, long j, size_t index, size_t dimX, size_t dimY)
{
float* ValVec;
long i_m;
long j_m;
long i1;
long j1;
long long counter;
int midval;
size_t index1;
midval = (sizefilter_total / 2);
ValVec = (float*) calloc(sizefilter_total, sizeof(float));
/* filling the allocated vector with the neighbouring values */
counter = 0LL;
for(i_m = -radius; i_m <= radius; i_m++)
{
i1 = i + i_m;
if((i1 < 0) || (i1 >= dimX))
i1 = i;
for(j_m = -radius; j_m <= radius; j_m++)
{
j1 = j + j_m;
if((j1 < 0) || (j1 >= dimY))
j1 = j;
index1 = (size_t)(j1) * dimX + (size_t)(i1);
ValVec[counter] = Input[index1];
counter++;
}
}
qsort(ValVec, sizefilter_total, sizeof(float), floatcomp);
if(mu_threshold == 0.0F)
{
/* perform median filtration */
Output[index] = ValVec[midval];
}
else
{
/* perform dezingering */
if(fabsf(Input[index] - ValVec[midval]) >= mu_threshold)
Output[index] = ValVec[midval];
}
free(ValVec);
}
void
medfilt3D_uint16(unsigned short* Input, unsigned short* Output, int radius,
int sizefilter_total, float mu_threshold, long i, long j, long k,
size_t index, size_t dimX, size_t dimY, size_t dimZ)
{
unsigned short* ValVec;
long i_m;
long j_m;
long k_m;
long i1;
long j1;
long k1;
long long counter;
int midval;
size_t index1;
midval = (sizefilter_total / 2);
ValVec = (unsigned short*) calloc(sizefilter_total, sizeof(unsigned short));
/* filling the allocated vector with the neighbouring values */
counter = 0LL;
for(i_m = -radius; i_m <= radius; i_m++)
{
i1 = i + i_m;
if((i1 < 0) || (i1 >= dimX))
i1 = i;
for(j_m = -radius; j_m <= radius; j_m++)
{
j1 = j + j_m;
if((j1 < 0) || (j1 >= dimY))
j1 = j;
for(k_m = -radius; k_m <= radius; k_m++)
{
k1 = k + k_m;
if((k1 < 0) || (k1 >= dimZ))
k1 = k;
index1 = dimX * dimY * (size_t)k1 + (size_t)j1 * dimX + (size_t)i1;
ValVec[counter] = Input[index1];
counter++;
}
}
}
qsort(ValVec, sizefilter_total, sizeof(unsigned short), uint16comp);
if(mu_threshold == 0.0F)
{
/* perform median filtration */
Output[index] = ValVec[midval];
}
else
{
/* perform dezingering */
if(abs(Input[index] - ValVec[midval]) >= mu_threshold)
Output[index] = ValVec[midval];
}
free(ValVec);
}
void
medfilt2D_uint16(unsigned short* Input, unsigned short* Output, int radius,
int sizefilter_total, float mu_threshold, long i, long j, size_t index,
size_t dimX, size_t dimY)
{
unsigned short* ValVec;
long i_m;
long j_m;
long i1;
long j1;
long long counter;
int midval;
size_t index1;
midval = (sizefilter_total / 2);
ValVec = (unsigned short*) calloc(sizefilter_total, sizeof(unsigned short));
/* filling the allocated vector with the neighbouring values */
counter = 0LL;
for(i_m = -radius; i_m <= radius; i_m++)
{
i1 = i + i_m;
if((i1 < 0) || (i1 >= dimX))
i1 = i;
for(j_m = -radius; j_m <= radius; j_m++)
{
j1 = j + j_m;
if((j1 < 0) || (j1 >= dimY))
j1 = j;
index1 = (size_t)(j1) * dimX + (size_t)(i1);
ValVec[counter] = Input[index1];
counter++;
}
}
qsort(ValVec, sizefilter_total, sizeof(unsigned short), uint16comp);
if(mu_threshold == 0.0F)
{
/* perform median filtration */
Output[index] = ValVec[midval];
}
else
{
/* perform dezingering */
if(abs(Input[index] - ValVec[midval]) >= mu_threshold)
Output[index] = ValVec[midval];
}
free(ValVec);
}
DLL int
medianfilter_main_float(float* Input, float* Output, int radius, float mu_threshold,
int ncores, int dimX, int dimY, int dimZ)
{
int sizefilter_total;
int diameter;
long i;
long j;
long k;
size_t index;
size_t totalvoxels;
totalvoxels = (size_t)(dimX) * (size_t)(dimY) * (size_t)(dimZ);
diameter = (2 * radius + 1); /* diameter of the filter's kernel */
/* dealing here with a custom given number of cpu threads */
if(ncores > 0)
{
// Explicitly disable dynamic teams
omp_set_dynamic(0);
// Use a number of threads for all consecutive parallel regions
omp_set_num_threads(ncores);
}
if(dimZ == 0)
/* 2D filtering */
{
sizefilter_total = (int) (powf(diameter, 2));
#pragma omp parallel for shared(Input, Output) private(i, j, index)
for(j = 0; j < dimY; j++)
{
for(i = 0; i < dimX; i++)
{
index = (size_t)(j) * dimX + (size_t)(i);
medfilt2D_float(Input, Output, radius, sizefilter_total, mu_threshold, i,
j, index, (size_t) dimX, (size_t) dimY);
}
}
}
else
/* 3D filtering */
{
sizefilter_total = (int) (powf(diameter, 3));
#pragma omp parallel for shared(Input, Output) private(i, j, k, index)
for(k = 0; k < dimZ; k++)
{
for(j = 0; j < dimY; j++)
{
for(i = 0; i < dimX; i++)
{
index = dimX * dimY * (size_t)(k) + (size_t)(j) * dimX + (size_t)(i);
medfilt3D_float(Input, Output, radius, sizefilter_total, mu_threshold,
i, j, k, index, (size_t) dimX, (size_t) dimY,
(size_t) dimZ);
}
}
}
}
return 0;
}
DLL int
medianfilter_main_uint16(unsigned short* Input, unsigned short* Output, int radius,
float mu_threshold, int ncores, int dimX, int dimY, int dimZ)
{
int sizefilter_total;
int diameter;
long i;
long j;
long k;
size_t index;
size_t totalvoxels;
totalvoxels = (size_t)(dimX) * (size_t)(dimY) * (size_t)(dimZ);
diameter = (2 * radius + 1); /* diameter of the filter's kernel */
/* dealing here with a custom given number of cpu threads */
if(ncores > 0)
{
// Explicitly disable dynamic teams
omp_set_dynamic(0);
// Use a number of threads for all consecutive parallel regions
omp_set_num_threads(ncores);
}
if(dimZ == 0)
/* 2D filtering */
{
sizefilter_total = (int) (powf(diameter, 2));
#pragma omp parallel for shared(Input, Output) private(i, j, index)
for(j = 0; j < dimY; j++)
{
for(i = 0; i < dimX; i++)
{
index = (size_t)(j) * dimX + (size_t)(i);
medfilt2D_uint16(Input, Output, radius, sizefilter_total, mu_threshold, i,
j, index, (size_t) dimX, (size_t) dimY);
}
}
}
else
/* 3D filtering */
{
sizefilter_total = (int) (powf(diameter, 3));
#pragma omp parallel for shared(Input, Output) private(i, j, k, index)
for(k = 0; k < dimZ; k++)
{
for(j = 0; j < dimY; j++)
{
for(i = 0; i < dimX; i++)
{
index = dimX * dimY * (size_t)(k) + (size_t)(j) * dimX + (size_t)(i);
medfilt3D_uint16(Input, Output, radius, sizefilter_total,
mu_threshold, i, j, k, index,(size_t) dimX,
(size_t)dimY, (size_t)dimZ);
}
}
}
}
return 0;
}