-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIM_Pattern_Tool.java
More file actions
201 lines (139 loc) · 5.47 KB
/
SIM_Pattern_Tool.java
File metadata and controls
201 lines (139 loc) · 5.47 KB
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
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.plugin.frame.*;
import java.awt.*;
import java.awt.event.*;
public class SIM_Pattern_Tool extends PlugInFrame implements ActionListener {
ImagePlus imp;
ImageProcessor original;
TextField angleField;
TextField phaseField;
TextField freqField;
TextField modField;
Button updateButton;
Button exportCurrentButton;
Button exportSIMButton;
double pixelSize = 0.1;
public SIM_Pattern_Tool() {
super("SIM Pattern Tool");
imp = IJ.getImage();
original = imp.getProcessor().duplicate();
setLayout(new GridLayout(7,2));
add(new Label("Angle (0–180)"));
angleField = new TextField("0");
add(angleField);
add(new Label("Phase (0–360)"));
phaseField = new TextField("0");
add(phaseField);
add(new Label("Spatial Frequency (0.1–10)"));
freqField = new TextField("3.5");
add(freqField);
add(new Label("Modulation (0–1)"));
modField = new TextField("0.3");
add(modField);
updateButton = new Button("Update Pattern");
exportCurrentButton = new Button("Export Current Pattern");
exportSIMButton = new Button("SIM Test Patterns (3 angles x 3 phases)");
add(updateButton);
add(exportCurrentButton);
add(exportSIMButton);
updateButton.addActionListener(this);
exportCurrentButton.addActionListener(this);
exportSIMButton.addActionListener(this);
pack();
setVisible(true);
updatePattern();
}
double clamp(double val,double min,double max){
return Math.max(min,Math.min(max,val));
}
void updatePattern(){
int width = imp.getWidth();
int height = imp.getHeight();
double angle = clamp(parse(angleField.getText()),0,180);
double phase = clamp(parse(phaseField.getText()),0,360);
double spatialFreq = clamp(parse(freqField.getText()),0.1,10);
double modulation = clamp(parse(modField.getText()),0,1);
double theta = Math.toRadians(angle);
double phi = Math.toRadians(phase);
double kx = spatialFreq*Math.cos(theta);
double ky = spatialFreq*Math.sin(theta);
FloatProcessor pattern = new FloatProcessor(width,height);
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
double X = (x-width/2.0)*pixelSize;
double Y = (y-height/2.0)*pixelSize;
double val = 0.5*(1 + modulation*Math.cos(
2*Math.PI*(kx*X + ky*Y) + phi));
pattern.setf(x,y,(float)val);
}
}
ImageProcessor overlay = original.duplicate();
overlay.copyBits(pattern,0,0,Blitter.ADD);
imp.setProcessor(overlay);
imp.updateAndDraw();
}
double parse(String s){
try{
return Double.parseDouble(s);
}catch(Exception e){
return 0;
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==updateButton){
updatePattern();
return;
}
int width = imp.getWidth();
int height = imp.getHeight();
double angle = parse(angleField.getText());
double phase = parse(phaseField.getText());
double spatialFreq = parse(freqField.getText());
double modulation = parse(modField.getText());
if(e.getSource()==exportCurrentButton){
double theta = Math.toRadians(angle);
double phi = Math.toRadians(phase);
double kx = spatialFreq*Math.cos(theta);
double ky = spatialFreq*Math.sin(theta);
FloatProcessor pattern = new FloatProcessor(width,height);
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
double X = (x-width/2.0)*pixelSize;
double Y = (y-height/2.0)*pixelSize;
double val = 0.5*(1 + modulation*Math.cos(
2*Math.PI*(kx*X + ky*Y) + phi));
pattern.setf(x,y,(float)val);
}
}
new ImagePlus("Current SIM Pattern",pattern).show();
}
if(e.getSource()==exportSIMButton){
double[] angles = {0,60,120};
double[] phases = {0,120,240};
ImageStack stack = new ImageStack(width,height);
for(double a:angles){
double theta = Math.toRadians(a);
for(double p:phases){
double phi = Math.toRadians(p);
FloatProcessor pattern = new FloatProcessor(width,height);
double kx = spatialFreq*Math.cos(theta);
double ky = spatialFreq*Math.sin(theta);
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
double X = (x-width/2.0)*pixelSize;
double Y = (y-height/2.0)*pixelSize;
double val = 0.5*(1 + modulation*Math.cos(
2*Math.PI*(kx*X + ky*Y) + phi));
pattern.setf(x,y,(float)val);
}
}
stack.addSlice("angle"+a+"_phase"+p,pattern);
}
}
new ImagePlus("SIM Test Patterns (3 angles x 3 phases)",stack).show();
}
}
public void run(String arg){}
}