1 package com.github.celldynamics.quimp.plugin.randomwalk;
2
3 import ij.IJ;
4 import ij.ImagePlus;
5 import ij.process.BinaryProcessor;
6 import ij.process.ImageProcessor;
7
8
9
10
11
12
13
14
15
16 public abstract class BinaryFilters {
17
18
19
20
21
22
23
24 public static enum Filters {
25
26
27
28 NONE,
29
30
31
32
33
34 SIMPLE,
35
36
37
38
39
40 MEDIAN
41 }
42
43
44
45
46
47
48
49 public static enum MorphoOperations {
50
51
52
53 ERODE,
54
55
56
57 DILATE,
58
59
60
61 IJOPEN
62 }
63
64
65
66
67
68
69
70
71 public static BinaryFilters getFilter(Filters type) {
72 switch (type) {
73 case NONE:
74 return new MedianMorpho.EmptyMorpho();
75 case SIMPLE:
76 return new MedianMorpho.SimpleMorpho();
77 case MEDIAN:
78 return new MedianMorpho.MedianMorpho();
79 default:
80 throw new IllegalArgumentException("Unknown filter type");
81 }
82 }
83
84
85
86
87
88
89
90
91
92 abstract ImageProcessor filter(ImageProcessor input);
93
94
95
96
97
98
99
100 public static class MedianMorpho extends BinaryFilters {
101
102
103
104
105
106
107
108 @Override
109 ImageProcessor filter(ImageProcessor input) {
110 BinaryProcessor bp = getBinaryProcessor(input);
111 bp.medianFilter();
112 return bp;
113 }
114
115 }
116
117
118
119
120
121
122
123 public static class SimpleMorpho extends BinaryFilters {
124
125
126
127
128
129
130 @Override
131 ImageProcessor filter(ImageProcessor input) {
132 return iterateMorphological(getBinaryProcessor(input), MorphoOperations.IJOPEN, 1);
133 }
134
135 }
136
137
138
139
140
141
142
143 public static class EmptyMorpho extends BinaryFilters {
144
145
146
147
148
149
150
151
152 @Override
153 ImageProcessor filter(ImageProcessor input) {
154 BinaryProcessor ret = getBinaryProcessor(input);
155 return ret;
156 }
157
158 }
159
160
161
162
163
164
165
166 public static BinaryProcessor getBinaryProcessor(ImageProcessor input) {
167 return new BinaryProcessor(input.duplicate().convertToByteProcessor());
168 }
169
170
171
172
173
174
175
176
177
178 public static ImageProcessor iterateMorphological(ImageProcessor ip, MorphoOperations oper,
179 double iter) {
180 BinaryProcessor result;
181 switch (oper) {
182 case ERODE:
183 result = getBinaryProcessor(ip);
184 for (int i = 0; i < iter; i++) {
185 result.erode(1, 0);
186 }
187 break;
188 case DILATE:
189 result = getBinaryProcessor(ip);
190 for (int i = 0; i < iter; i++) {
191 result.dilate(1, 0);
192 }
193 break;
194 case IJOPEN:
195
196 IJ.run("Options...", "iterations=" + iter + " count=1 black do=Nothing");
197 IJ.run(new ImagePlus("before", ip), "Open", "");
198 result = (BinaryProcessor) ip;
199 break;
200 default:
201 throw new IllegalArgumentException("Binary operation not supported");
202 }
203 return result;
204 }
205
206 }