-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReconstructionViewer.java
394 lines (345 loc) · 11.1 KB
/
ReconstructionViewer.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package QuickPALM;
import ij.*;
import ij.measure.*;
import ij.plugin.*;
import ij.plugin.filter.*;
import ij.plugin.frame.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.CurveFitter.*;
import java.awt.*;
class ReconstructionViewer
{
ImagePlus imp;
ImageProcessor ip;
MyDialogs settings;
ResultsTable table;
MyFunctions functions;
GaussianBlur gblur = new GaussianBlur();
int position = 0;
int max=0;
int min=9999999;
double maxZ=10;
double minZ=-10;
double [] s;
double [] x;
double [] y;
double [] z;
double [] f;
int nframes = 0;
/** Class constructer used on the online rendering mode.
* @param title name for the rendering window
* @param width original image width
* @param height original image height
* @param dg dialog manager
* @param f functions manager
*/
ReconstructionViewer(java.lang.String title, int width, int height, MyDialogs dg, MyFunctions f)
{
settings = dg;
table = f.ptable;
functions = f;
int new_width=(int) (width*settings.magn+1);
int new_height=(int) (height*settings.magn+1);
if (settings.view)
{
ip=new ColorProcessor(new_width, new_height);
//calculateColorBar();
imp = new ImagePlus(title, ip);
IJ.run(imp, "Set Scale...", "distance=1 known="+settings.pixelsize/settings.magn+" pixel=1 unit=nm");
imp.show();
}
}
/** Class constructer used on the offline rendering mode.
* @param title name for the rendering window
* @param dg dialog manager
* @param f_ functions manager
*/
ReconstructionViewer(java.lang.String title, MyDialogs dg, MyFunctions f_)
{
settings = dg;
table = f_.ptable;
functions = f_;
double pixelsize = table.getValue("X (nm)", 0)/table.getValue("X (px)", 0);
settings.magn = pixelsize/settings.viewer_tpixelsize;
int new_width=(int) (settings.viewer_owidth*settings.magn+1);
int new_height=(int) (settings.viewer_oheight*settings.magn+1);
// create the processor
if (settings.view_mode == settings.view_modes[3]) // 2D particle intensity (8-bit)
ip=new ByteProcessor(new_width, new_height);
else if (settings.view_mode == settings.view_modes[1] || settings.view_mode == settings.view_modes[2]) // 2D histogram or 2D particle intensity (16-bit)
ip=new ShortProcessor(new_width, new_height);
else // color
ip=new ColorProcessor(new_width, new_height);
// create the image
imp = new ImagePlus(title, ip);
IJ.run(imp, "Set Scale...", "distance=1 known="+settings.viewer_tpixelsize+" pixel=1 unit=nm");
// load data
functions.ptable_lock.lock();
s = table.getColumnAsDoubles(0);
x = table.getColumnAsDoubles(1);
y = table.getColumnAsDoubles(2);
z = table.getColumnAsDoubles(5);
f = table.getColumnAsDoubles(13);
functions.ptable_lock.unlock();
// load max & min values
for (int n=0;n<f.length;n++)
{
if (f[n]>nframes) nframes=(int) f[n];
if (s[n]>max) max=(int) Math.round(s[n]);
if (s[n]<min) min=(int) Math.round(s[n]);
if (z[n]>maxZ) maxZ=(int) Math.round(z[n]);
if (z[n]<minZ) minZ=(int) Math.round(z[n]);
if (f[n]>nframes) nframes=(int) f[n];
}
if (dg.viewer_mergeabove!=0) maxZ=dg.viewer_mergeabove;
if (dg.viewer_mergebellow!=0) minZ=dg.viewer_mergebellow;
//max = max/2;
if (settings.view_mode == settings.view_modes[0]) // color
calculateColorBar();
}
/** Main reconstruction drawing function used by the "Reconstruct Dataset" plugin.
* @param fstart show only particle after this frame
* @param fstop show only particle before this frame
* @param zstart show only particles bellow this z-position
* @param zstop show only particles above this z-position
*/
void draw(int fstart, int fstop, double zstart, double zstop)
{
int type = imp.getBitDepth();
int xmag, ymag;
double old_v, new_v;
int [] old_rgb = new int [3];
int [] new_rgb = new int [3];
int satv;
for (int n=0;n<f.length;n++)
{
if (f[n]>=fstart && f[n]<=fstop)
{
if (z[n]>=zstart && z[n]<=zstop)
{
xmag=(int) Math.round(x[n]*settings.magn);
ymag=(int) Math.round(y[n]*settings.magn);
xmag=(xmag>=ip.getWidth())?ip.getWidth()-1:xmag;
xmag=(xmag<0)?0:xmag;
ymag=(ymag>=ip.getHeight())?ip.getHeight()-1:ymag;
ymag=(ymag<0)?0:ymag;
if (settings.view_mode==settings.view_modes[0]) // color
{
old_rgb = ip.getPixel(xmag, ymag, old_rgb);
new_rgb = calculateColor(s[n], z[n]);
new_rgb[0]+=old_rgb[0];
new_rgb[1]+=old_rgb[1];
new_rgb[2]+=old_rgb[2];
new_rgb[0]=(new_rgb[0]>255)?255:new_rgb[0];
new_rgb[1]=(new_rgb[1]>255)?255:new_rgb[1];
new_rgb[2]=(new_rgb[2]>255)?255:new_rgb[2];
ip.putPixel(xmag, ymag, new_rgb);
}
else if (settings.view_mode==settings.view_modes[1]) // 2D histogram
{
old_v=ip.get(xmag, ymag);
new_v=old_v+1;
ip.set(xmag, ymag, (int) new_v);
}
else if (settings.view_mode==settings.view_modes[2]) //2D particle intensity (16-bit)
{
old_v=ip.get(xmag, ymag);
new_v=s[n]+old_v;
ip.set(xmag, ymag, (int) new_v);
}
else // 2D particle intensity (8-bit)
{
satv = 255;
old_v=ip.get(xmag, ymag);
new_v=((s[n]-min)/(max-min))*satv;
//if (old_v<new_v)
// ip.set(xmag, ymag, (int) Math.round(new_v));
new_v = Math.round(new_v+old_v);
new_v = (new_v>satv)?satv:new_v;
ip.set(xmag, ymag, (int) new_v);
}
}
}
}
if (settings.viewer_doConvolve) gblur.blur(ip, (settings.viewer_fwhm/2.354)/settings.viewer_tpixelsize);
imp.updateAndDraw();
//if (imp.isVisible())
// IJ.run(imp, "Enhance Contrast", "saturated=0.5");
}
/** Calculates a color bar to guide users on the position of each particle in Z.*/
ImagePlus calculateColorBar()
{
int border = 20;
ImageProcessor ipbar = new ColorProcessor(80, 510+border*2);
ImagePlus impbar = new ImagePlus("Depth coding", ipbar);
int [] c = new int [3];
for (int n=0; n<=510; n++)
{
c[0]=(n>=255)?n-255:0;
c[1]=(n<=255)?n:510-n;
c[2]=(n<=255)?255-n:0;
for (int m=0; m<10; m++)
ipbar.putPixel(m,n+border,c);
}
impbar.updateAndDraw();
impbar.show();
IJ.runMacro("setFont(\"SansSerif\", 18, \" antialiased\");");
IJ.run("Colors...", "foreground=white background=black selection=yellow");
IJ.runMacro("drawString(\"< "+Math.round(minZ) +"\", "+12+", "+(border+6)+");");
IJ.runMacro("drawString(\" "+Math.round(minZ+(maxZ-minZ)*1/6)+"\", "+12+", "+(510*1/6+border+6)+");");
IJ.runMacro("drawString(\" "+Math.round(minZ+(maxZ-minZ)*2/6)+"\", "+12+", "+(510*2/6+border+6)+");");
IJ.runMacro("drawString(\" "+Math.round(minZ+(maxZ-minZ)*3/6)+"\", "+12+", "+(510*3/6+border+6)+");");
IJ.runMacro("drawString(\" "+Math.round(minZ+(maxZ-minZ)*4/6)+"\", "+12+", "+(510*4/6+border+6)+");");
IJ.runMacro("drawString(\" "+Math.round(minZ+(maxZ-minZ)*5/6)+"\", "+12+", "+(510*5/6+border+6)+");");
IJ.runMacro("drawString(\"> "+Math.round(minZ+(maxZ-minZ)*6/6)+"\", "+12+", "+(510*6/6+border+6)+");");
return impbar;
}
/** Calculates the color for a particle based on its intensity and position in Z.
* @param s particle intensity
* @param z particle position in z
* @return particle color as RGB values
*/
int [] calculateColor(double s, double z)
{
double vs = ((s-min)/(max-min))*(1+settings.saturation); // allow some saturation
double vz = ((z-minZ)/(maxZ-minZ));
int [] c = new int [3];
if (vz>1) vz=1;
else if (vz<0) vz=0;
double vz_=255*vz*2;
c[0] = (int) Math.round(((vz>=0.5)?(vz_-255):0)*vs); //R
c[1] = (int) Math.round(((vz<=0.5)?vz_:(510-vz_))*vs); //G
c[2] = (int) Math.round(((vz<=0.5)?(255-vz_):0)*vs); //B
c[0] = (c[0]>255)?255:c[0];
c[1] = (c[1]>255)?255:c[1];
c[2] = (c[2]>255)?255:c[2];
return c;
}
/** Updates the reconstruction viewer with the lattest acquired particles. */
void update()
{
if (!settings.view) return;
int new_p=table.getCounter();
if (new_p==0 || new_p==position) return;
update(position, new_p-1);
position = new_p;
}
/** Updates the reconstruction viewer by showing the new particles found between
* the given indices.
* @param start first particle to be updated from the table
* @param stop last particle to be updated from the table
*/
void update(int start, int stop)
{
// updated May 5th - instead of loading the full columns as arrays, we only grab the needed values, should increase processing speed
if (!settings.view) return;
int nresults = table.getCounter();
start=(start<0)?0:start;
stop=(stop>nresults)?(nresults-1):stop;
s = new double [stop-start+1];
x = new double [stop-start+1];
y = new double [stop-start+1];
z = new double [stop-start+1];
int index;
functions.ptable_lock.lock();
for (int n=start; n<=stop; n++)
{
index = n-start;
s[index] = table.getValueAsDouble(0, n);
x[index] = table.getValueAsDouble(1, n);
y[index] = table.getValueAsDouble(2, n);
z[index] = table.getValueAsDouble(5, n);
}
functions.ptable_lock.unlock();
boolean newMax=false;
boolean newMin=false;
boolean newMaxZ=false;
boolean newMinZ=false;
// check if there is a new max/min value
for (int n=0;n<=(stop-start);n++)
{
if (s[n]>max)
{
newMax=true;
max=(int) Math.round(s[n]);
}
if (s[n]<min)
{
newMin=true;
min=(int) Math.round(s[n]);
}
if (z[n]>maxZ)
{
newMaxZ=true;
maxZ=z[n];
}
if (z[n]<minZ)
{
newMinZ=true;
minZ=z[n];
}
}
if (newMax || newMinZ || newMaxZ || newMin)
{
clear();
update(0, stop); // if a new max/min value is found we need to reupdate the full image
return;
}
int v, xmag, ymag;
int [] old_rgb = new int [3];
int [] new_rgb = new int [3];
for (int n=0;n<=(stop-start);n++)
{
xmag=(int) Math.round(x[n]*settings.magn);
ymag=(int) Math.round(y[n]*settings.magn);
ip.getPixel(xmag, ymag, old_rgb);
new_rgb = calculateColor(s[n], z[n]);
if ((old_rgb[0]+old_rgb[1]+old_rgb[2])<(new_rgb[0]+new_rgb[1]+new_rgb[2]))
ip.putPixel(xmag, ymag, new_rgb);
}
imp.updateAndDraw();;
}
/** Updates the reconstruction viewer by showing particles found between
* fstart and fstop.
* @param fstart start position of the frame range
* @param fstop stop position of the frame range
*/
void updateShort(int fstart, int fstop)
{
if (!settings.view) return;
clear();
functions.ptable_lock.lock();
s = table.getColumnAsDoubles(0);
x = table.getColumnAsDoubles(1);
y = table.getColumnAsDoubles(2);
z = table.getColumnAsDoubles(5);
f = table.getColumnAsDoubles(13);
functions.ptable_lock.unlock();
int xmag, ymag;
int [] old_rgb = new int [3];
int [] new_rgb = new int [3];
for (int n=0;n<f.length;n++)
{
if (f[n]>=fstart && f[n]<=fstop)
{
xmag=(int) Math.round(x[n]*settings.magn);
ymag=(int) Math.round(y[n]*settings.magn);
ip.getPixel(xmag, ymag, old_rgb);
new_rgb = calculateColor(s[n], z[n]);
if ((old_rgb[0]+old_rgb[1]+old_rgb[2])<(new_rgb[0]+new_rgb[1]+new_rgb[2]))
ip.putPixel(xmag, ymag, new_rgb);
}
}
imp.updateAndDraw();
}
/** Cleans the reconstruction viewer image. */
void clear()
{
if (!settings.view) return;
for (int i=0;i<ip.getWidth();i++)
for (int j=0;j<ip.getHeight();j++)
ip.set(i, j, 0);
position=0;
}
}