This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Adaptive_Filter.java
333 lines (294 loc) · 10.2 KB
/
Adaptive_Filter.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
319
320
321
322
323
324
325
326
327
328
package de.biovoxxel.toolbox;
import java.util.Arrays;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
/*
* Copyright (C), Jan Brocher / BioVoxxel. All rights reserved.
*
* All Macros/Plugins were written by Jan Brocher/BioVoxxel.
*
* Redistribution and use in source and binary forms of all plugins and macros, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1.) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2.) 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.
* 3.) Neither the name of BioVoxxel nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* DISCLAIMER:
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
*/
/** Adaptive Filter
* This plugin filter creates a square grid of checkboxes to enable the user
* to create a customized 'median' or 'mean' filter with arbitrary size controlled
* by the radius keyed in initially. Several preset grids can be chosen.
* Only pixel intensity values under activated filter grid positions are taken
* into account for the calculations of the new pixel value
*
* So far can be used for 8-bit and 16-bit grayscale images only.
* <br>
* <br>Please cite BioVoxxel and Jan Brocher when you publish results
* obtained by usage of this plugin or a modified version of it
*
* Thank you
*
*/
public class Adaptive_Filter implements PlugInFilter {
ImagePlus imp;
String[] choices = new String[] {"median", "mean" };
String[] shapes = new String[] {"none", "all", "x shaped", "/ shaped", "\\ shaped", "+ shaped", "horizontal", "vertical", "star shaped"};
int filterPosX = 0;
int filterPosY = 0;
int newPixelValue = 0;
private int positive=0;
private int centerPixelValue;
private double testValue1;
private double testValue2;
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_8G+DOES_16;
//implement also for stacks, ROIs and RGB images
}
public void run(ImageProcessor orig) {
int width = orig.getWidth();
int height = orig.getHeight();
GenericDialog init = new GenericDialog("Filter setup");
init.addNumericField("Radius (pixels):", 1, 0);
init.addChoice("Filter", choices, "median");
init.addChoice("Shape", shapes, "all");
init.addNumericField("Tolerance 0.0-0.9", 0.0, 1);
if(imp.getNSlices()>1) {
init.addCheckbox("stack processing", false);
}
init.showDialog();
if (init.wasCanceled()) {
return;
}
int radius = (int) init.getNextNumber();
String filterMethod = init.getNextChoice();
String startingShape = init.getNextChoice();
double tolerance = init.getNextNumber();
boolean processCompleteStack = false;
if(imp.getNSlices()>1) {
processCompleteStack = init.getNextBoolean();
}
int filterEdge = (radius*2)+1;
int filterSize = filterEdge*filterEdge;
String[] gridLabels = new String[filterSize];
boolean [] defaults = new boolean[filterSize];
boolean[] filterArray = new boolean[filterSize];
int n=0;
//grid labeling according to columns and rows
int fn = 0;
for(int fy=1; fy<=filterEdge; fy++) {
for(int fx=1; fx<=filterEdge; fx++) {
gridLabels[fn] = (""+ fy + "/" + fx);
fn++;
}
}
/*
//continuas labeling
for(int f=0; f<filterSize; f++) {
gridLabels[f] = Integer.toString(f);
}
*/
//Arrays.fill(gridLabels, " ");
if(startingShape=="none") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(n==(filterSize-1)/2) {
defaults[n]=true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="all") {
Arrays.fill(defaults, true);
} else if (startingShape=="x shaped") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridY==gridX || gridX==((filterEdge-1)-gridY)) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="/ shaped") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridX==((filterEdge-1)-gridY)) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="\\ shaped") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridY==gridX) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="+ shaped") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridX==(filterEdge-1)/2 || gridY==(filterEdge-1)/2) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="horizontal") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridY==(filterEdge-1)/2) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="vertical") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridX==(filterEdge-1)/2) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
} else if (startingShape=="star shaped") {
for (int gridY=0; gridY<filterEdge; gridY++) {
for (int gridX=0; gridX<filterEdge; gridX++) {
if(gridX==(filterEdge-1)/2 || gridY==(filterEdge-1)/2 || gridY==gridX || gridX==((filterEdge-1)-gridY)) {
defaults[n] = true;
} else {
defaults[n] = false;
}
n++;
}
}
}
GenericDialog grid = new GenericDialog("adapt filter");
grid.addCheckboxGroup(filterEdge, filterEdge, gridLabels, defaults);
grid.showDialog();
if (grid.wasCanceled()) {
return;
}
for(int filter=0; filter<filterSize; filter++) {
filterArray[filter] = grid.getNextBoolean();
if(filterArray[filter]==true) {
positive++;
}
}
int processOneOrAll = 1;
if(processCompleteStack){
processOneOrAll = imp.getNSlices();
}
ImagePlus copyImp = imp.duplicate();
ImageProcessor copy = copyImp.getProcessor();
for(int slice=1; slice<=processOneOrAll; slice++) {
imp.setSliceWithoutUpdate(slice);
copyImp.setSliceWithoutUpdate(slice);
//define grid defined array size
int[] originalPixelValues = new int[positive];
for(int ImageY=0; ImageY<height; ImageY++) {
for(int ImageX=0; ImageX<width; ImageX++) {
centerPixelValue = copy.getPixel(ImageX, ImageY);
//define position inside filter
int k=0;
int index=0;
for(int filterY=-radius; filterY<=radius; filterY++) {
//horizontal border padding
if((ImageY+filterY) < 0) {
filterPosY = -ImageY;
} else if((ImageY+filterY) > height-1) {
filterPosY = (height-1)-ImageY;
} else {
filterPosY = filterY;
}
for(int filterX=-radius; filterX<=radius; filterX++) {
//vertical border padding
if((ImageX+filterX) < 0) {
filterPosX = -ImageX;
} else if((ImageX+filterX) > width-1) {
filterPosX = (width-1)-ImageX;
} else {
filterPosX = filterX;
}
//reading in original pixel values if pixel position was activated in the adaptive filter matrix
if(filterArray[index]==true) {
originalPixelValues[k] = copy.getPixel((ImageX+filterPosX), (ImageY+filterPosY));
k++;
}
index++;
}
}
if(filterMethod=="median") {
Arrays.sort(originalPixelValues);
if(positive%2==0) {
newPixelValue=(((originalPixelValues[(positive/2)-1])+(originalPixelValues[(positive/2)]))/2);
} else {
newPixelValue=(originalPixelValues[(positive-1)/2]);
}
} else if(filterMethod=="mean") {
int sum=0;
for(int m=0; m<positive; m++) {
sum = sum + originalPixelValues[m];
}
newPixelValue = sum/positive;
}
if(centerPixelValue!=0) {
//IJ.log("old: "+ (double) centerPixelValue);
//IJ.log("new "+ (double) newPixelValue);
testValue1 = (double) newPixelValue/centerPixelValue;
//IJ.log("1.) "+testValue1);
testValue2 = (double) centerPixelValue/newPixelValue;
//IJ.log("2.) "+testValue2);
}
if(tolerance==0.0) {
orig.putPixel(ImageX, ImageY, newPixelValue);
} else if(tolerance!=0.0 && centerPixelValue==0) {
orig.putPixel(ImageX, ImageY, newPixelValue);
} else if(tolerance!=0.0 && (testValue1 < (1.0-tolerance))) {
orig.putPixel(ImageX, ImageY, newPixelValue);
} else if(tolerance!=0.0 && (testValue2 < (1.0-tolerance))) {
orig.putPixel(ImageX, ImageY, newPixelValue);
} else {
orig.putPixel(ImageX, ImageY, centerPixelValue);
}
}
}
}
imp.updateAndDraw();
copyImp = null;
copy = null;
}
}