forked from fiji/LocalThickness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clean_Up_Local_Thickness.java
318 lines (307 loc) · 8.34 KB
/
Clean_Up_Local_Thickness.java
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
package net.imagej.localThickness;
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.filter.PlugInFilter;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
/* Bob Dougherty August 1, 2007
Input: 3D Local Thickness map (32-bit stack)
Output: Same as input with border voxels corrected for "jaggies." Non-background voxels
adjacent to background voxels are have their local thickness values replaced by the average of
their non-background neighbors that do not border background points.
August 10. Version 3 This version also multiplies the local thickness by 2 to conform with the
offficial definition of local thickness.
License:
Copyright (c) 2007, OptiNav, Inc.
All rights reserved.
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 OptiNav, Inc. 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT OWNER 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.
*/
public class Clean_Up_Local_Thickness implements PlugInFilter {
private ImagePlus imp;
public float[][] s, sNew;
public int w,h,d;
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_32;
}
public void run(ImageProcessor ip) {
ImageStack stack = imp.getStack();
w = stack.getWidth();
h = stack.getHeight();
d = imp.getStackSize();
//Create 32 bit floating point stack for output, sNew.
ImageStack newStack = new ImageStack(w,h);
sNew = new float[d][];
for(int k = 0; k < d; k++){
ImageProcessor ipk = new FloatProcessor(w,h);
newStack.addSlice(null,ipk);
sNew[k] = (float[])ipk.getPixels();
}
//Create reference to input data
s = new float[d][];
for (int k = 0; k < d; k++)s[k] = (float[])stack.getPixels(k+1);
//First set the output array to flags:
// 0 for a background point
// -1 for a non-background point that borders a background point
// s (input data) for an interior non-background point
for (int k = 0; k < d; k++){
for (int j = 0; j < h; j++){
for (int i = 0; i < w; i++){
sNew[k][i + w*j] = setFlag(i,j,k);
}//i
}//j
}//k
//Process the surface points. Initially set results to negative values
//to be able to avoid including them in averages of for subsequent points.
//During the calculation, positve values in sNew are interior non-background
//local thicknesses. Negative values are surface points. In this case the
//value might be -1 (not processed yet) or -result, where result is the
//average of the neighboring interior points. Negative values are excluded from
//the averaging.
for (int k = 0; k < d; k++){
for (int j = 0; j < h; j++){
for (int i = 0; i < w; i++){
int ind = i + w*j;
if(sNew[k][ind] == -1){
sNew[k][ind] = -averageInteriorNeighbors(i,j,k);
}
}//i
}//j
}//k
//Fix the negative values and double the results
for (int k = 0; k < d; k++){
for (int j = 0; j < h; j++){
for (int i = 0; i < w; i++){
int ind = i + w*j;
sNew[k][ind] = (float)Math.abs(sNew[k][ind]);
}//i
}//j
}//k
IJ.showStatus("Clean Up Local Thickness complete");
String title = stripExtension(imp.getTitle());
ImagePlus impOut = new ImagePlus(title+"_CL",newStack);
impOut.getProcessor().setMinAndMax(0,2*imp.getProcessor().getMax());
impOut.show();
IJ.run("Fire");
}
float setFlag(int i,int j,int k){
if(s[k][i+w*j]==0)return 0;
//change 1
if(look(i,j,k-1)==0)return -1;
if(look(i,j,k+1)==0)return -1;
if(look(i,j-1,k)==0)return -1;
if(look(i,j+1,k)==0)return -1;
if(look(i-1,j,k)==0)return -1;
if(look(i+1,j,k)==0)return -1;
//change 1 before plus
if(look(i,j+1,k-1)==0)return -1;
if(look(i,j+1,k+1)==0)return -1;
if(look(i+1,j-1,k)==0)return -1;
if(look(i+1,j+1,k)==0)return -1;
if(look(i-1,j,k+1)==0)return -1;
if(look(i+1,j,k+1)==0)return -1;
//change 1 before minus
if(look(i,j-1,k-1)==0)return -1;
if(look(i,j-1,k+1)==0)return -1;
if(look(i-1,j-1,k)==0)return -1;
if(look(i-1,j+1,k)==0)return -1;
if(look(i-1,j,k-1)==0)return -1;
if(look(i+1,j,k-1)==0)return -1;
//change 3, k+1
if(look(i+1,j+1,k+1)==0)return -1;
if(look(i+1,j-1,k+1)==0)return -1;
if(look(i-1,j+1,k+1)==0)return -1;
if(look(i-1,j-1,k+1)==0)return -1;
//change 3, k-1
if(look(i+1,j+1,k-1)==0)return -1;
if(look(i+1,j-1,k-1)==0)return -1;
if(look(i-1,j+1,k-1)==0)return -1;
if(look(i-1,j-1,k-1)==0)return -1;
return s[k][i+w*j];
}
float averageInteriorNeighbors(int i,int j,int k){
int n = 0;
float sum = 0;
//change 1
float value = lookNew(i,j,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i,j,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i,j-1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i,j+1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j,k);
if(value > 0){
n++;
sum += value;
}
//change 1 before plus
value = lookNew(i,j+1,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i,j+1,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j-1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j+1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j,k+1);
if(value > 0){
n++;
sum += value;
}
//change 1 before minus
value = lookNew(i,j-1,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i,j-1,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j-1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j+1,k);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j,k-1);
if(value > 0){
n++;
sum += value;
}
//change 3, k+1
value = lookNew(i+1,j+1,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j-1,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j+1,k+1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j-1,k+1);
if(value > 0){
n++;
sum += value;
}
//change 3, k-1
value = lookNew(i+1,j+1,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i+1,j-1,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j+1,k-1);
if(value > 0){
n++;
sum += value;
}
value = lookNew(i-1,j-1,k-1);
if(value > 0){
n++;
sum += value;
}
if(n > 0)return sum/n;
return s[k][i+w*j];
}
float look(int i,int j,int k){
if((i < 0)||(i >= w))return -1;
if((j < 0)||(j >= h))return -1;
if((k < 0)||(k >= d))return -1;
return s[k][i+w*j];
}
//A positive result means this is an interior, non-background, point.
float lookNew(int i,int j,int k){
if((i < 0)||(i >= w))return -1;
if((j < 0)||(j >= h))return -1;
if((k < 0)||(k >= d))return -1;
return sNew[k][i+w*j];
}
//Modified from ImageJ code by Wayne Rasband
String stripExtension(String name) {
if (name!=null) {
int dotIndex = name.lastIndexOf(".");
if (dotIndex>=0)
name = name.substring(0, dotIndex);
}
return name;
}
}