-
Notifications
You must be signed in to change notification settings - Fork 6
/
hysteresis_.java
196 lines (180 loc) · 4.78 KB
/
hysteresis_.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
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
/**
* Description of the Class
*
*@author thomas
*@created 3 janvier 2007
*/
public class hysteresis_ implements PlugInFilter {
ImagePlus imp;
/**
* Description of the Method
*
*@param arg Description of the Parameter
*@param imp Description of the Parameter
*@return Description of the Return Value
*/
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_ALL;
}
/**
* Main processing method for the Hysteresis_ object
*
*@param ip Description of the Parameter
*/
public void run(ImageProcessor ip) {
GenericDialog gd = new GenericDialog("Parameters");
gd.addNumericField("High threshold", 100, 2);
gd.addNumericField("Low threshold", 50, 2);
gd.showDialog();
float T1 = (float) gd.getNextNumber();
float T2 = (float) gd.getNextNumber();
ImageStack stack = imp.getStack();
ImageStack res_trin = new ImageStack(stack.getWidth(), stack.getHeight());
ImageStack res_hyst = new ImageStack(stack.getWidth(), stack.getHeight());
ImageProcessor tmp1;
ImageProcessor tmp2;
for (int s = 1; s <= stack.getSize(); s++) {
tmp1 = trin(stack.getProcessor(s), T1, T2);
tmp2 = hyst(tmp1);
res_trin.addSlice("", tmp1);
res_hyst.addSlice("", tmp2);
}
new ImagePlus("Trinarisation", res_trin).show();
new ImagePlus("Hysteresis", res_hyst).show();
}
/**
* Double thresholding
*
*@param ima original image
*@param T1 high threshold
*@param T2 low threshold
*@return "trinarised" image
*/
ImageProcessor trin(ImageProcessor ima, float T1, float T2) {
int la = ima.getWidth();
int ha = ima.getHeight();
ByteProcessor res = new ByteProcessor(la, ha);
float pix;
for (int x = 0; x < la; x++) {
for (int y = 0; y < ha; y++) {
pix = ima.getPixelValue(x, y);
if (pix >= T1) {
res.putPixel(x, y, 255);
} else if (pix >= T2) {
res.putPixel(x, y, 128);
}
}
}
return res;
}
/**
* Hysteresis thresholding
*
*@param ima original image
*@return thresholded image
*/
ImageProcessor hyst(ImageProcessor ima) {
int la = ima.getWidth();
int ha = ima.getHeight();
ImageProcessor res = ima.duplicate();
float pix;
boolean change = true;
// connection
while (change) {
change = false;
for (int x = 1; x < la - 1; x++) {
for (int y = 1; y < ha - 1; y++) {
if (res.getPixelValue(x, y) == 255) {
if (res.getPixelValue(x + 1, y) == 128) {
change = true;
res.putPixelValue(x + 1, y, 255);
}
if (res.getPixelValue(x - 1, y) == 128) {
change = true;
res.putPixelValue(x - 1, y, 255);
}
if (res.getPixelValue(x, y + 1) == 128) {
change = true;
res.putPixelValue(x, y + 1, 255);
}
if (res.getPixelValue(x, y - 1) == 128) {
change = true;
res.putPixelValue(x, y - 1, 255);
}
if (res.getPixelValue(x + 1, y + 1) == 128) {
change = true;
res.putPixelValue(x + 1, y + 1, 255);
}
if (res.getPixelValue(x - 1, y - 1) == 128) {
change = true;
res.putPixelValue(x - 1, y - 1, 255);
}
if (res.getPixelValue(x - 1, y + 1) == 128) {
change = true;
res.putPixelValue(x - 1, y + 1, 255);
}
if (res.getPixelValue(x + 1, y - 1) == 128) {
change = true;
res.putPixelValue(x + 1, y - 1, 255);
}
}
}
}
if (change) {
for (int x = la - 2; x > 0; x--) {
for (int y = ha - 2; y > 0; y--) {
if (res.getPixelValue(x, y) == 255) {
if (res.getPixelValue(x + 1, y) == 128) {
change = true;
res.putPixelValue(x + 1, y, 255);
}
if (res.getPixelValue(x - 1, y) == 128) {
change = true;
res.putPixelValue(x - 1, y, 255);
}
if (res.getPixelValue(x, y + 1) == 128) {
change = true;
res.putPixelValue(x, y + 1, 255);
}
if (res.getPixelValue(x, y - 1) == 128) {
change = true;
res.putPixelValue(x, y - 1, 255);
}
if (res.getPixelValue(x + 1, y + 1) == 128) {
change = true;
res.putPixelValue(x + 1, y + 1, 255);
}
if (res.getPixelValue(x - 1, y - 1) == 128) {
change = true;
res.putPixelValue(x - 1, y - 1, 255);
}
if (res.getPixelValue(x - 1, y + 1) == 128) {
change = true;
res.putPixelValue(x - 1, y + 1, 255);
}
if (res.getPixelValue(x + 1, y - 1) == 128) {
change = true;
res.putPixelValue(x + 1, y - 1, 255);
}
}
}
}
}
}
// suppression
for (int x = 0; x < la; x++) {
for (int y = 0; y < ha; y++) {
if (res.getPixelValue(x, y) == 128) {
res.putPixelValue(x, y, 0);
}
}
}
return res;
}
}