-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCumulative_Z-Project.bsh
108 lines (98 loc) · 3.34 KB
/
Cumulative_Z-Project.bsh
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
/*
* Cumulative_Z-Project.bsh
* IJ BAR: https://github.com/tferr/Scripts#scripts
*
* BeanShell script that produces cumulative projections using ImageJ's built-in projector
* ("Image>Stacks>Z Project.."). An immediate application of these progressive projections is
* the display of trailing paths of moving particles in timelapse experiments. For a demo, run
* it on Trackmate's sample image ("File>Open Samples>Tracks for TrackMate", in Fiji/IJ2)
*/
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.plugin.RGBStackConverter;
import ij.plugin.ZProjector;
import ij.process.ImageProcessor;
int startSlice, stopSlice;
int method = ZProjector.MAX_METHOD;
ImagePlus imp;
void run(String arg) {
imp = WindowManager.getCurrentImage();
if (invalidImage(imp)) {
String sample = "http://fiji.sc/samples/FakeTracks.tif";
if (IJ.showMessageWithCancel("Invalid image",
"A stack is required but none was found.\nOpen sample image from fiji.sc?")) {
imp = new ImagePlus(sample);
if (invalidImage(imp)) {
IJ.error("Error:\n\"" + sample + "\" could not be retrieved.");
return;
}
imp.show();
} else
return;
}
if (imp.isComposite()) {
if (IJ.showMessageWithCancel("Convert to RGB?",
"Multichannel images must be first converted to RGB.\nConvert?")) {
(new RGBStackConverter()).convertToRGB(imp);
} else
return;
}
startSlice = 1;
stopSlice = (imp.isHyperStack()) ? imp.getNSlices() : imp.getStackSize();
if (!showDialog(imp))
return;
ImagePlus imp2 = cumulativeZProject(imp, method);
if (imp2 != null) {
imp2.setCalibration(imp.getCalibration());
imp2.show();
}
}
ImagePlus cumulativeZProject(ImagePlus imp, int method) {
if (method<0 || method>=ZProjector.METHODS.length)
return null;
if (imp.isHyperStack() && imp.getNFrames() > 1) {
ZProjector zp = new ZProjector(imp);
zp.setMethod(method);
zp.setStartSlice(startSlice);
zp.setStopSlice(stopSlice);
zp.doHyperStackProjection(true);
imp = zp.getProjection();
startSlice = 1;
stopSlice = imp.getStackSize();
}
ZProjector zp = new ZProjector(imp);
zp.setMethod(method);
zp.setStartSlice(startSlice);
ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
for (int z = startSlice; z <= stopSlice; z++) {
zp.setStopSlice(z);
if (imp.getBitDepth() == 24)
zp.doRGBProjection(true);
else
zp.doProjection(true);
ImageProcessor ip = zp.getProjection().getProcessor();
stack.addSlice("Proj " + startSlice + "-" + z, ip);
}
return new ImagePlus(WindowManager.makeUniqueName("CumulativeProj_" + imp.getTitle()), stack);
}
boolean invalidImage(ImagePlus imp) {
return imp == null || (imp != null && imp.getStackSize() == imp.getNChannels());
}
boolean showDialog(ImagePlus imp) {
GenericDialog gd = new GenericDialog("Cumulative Z-Project");
gd.addSlider("Start slice:", startSlice, stopSlice, startSlice);
gd.addSlider("Stop slice:", startSlice, stopSlice, stopSlice);
gd.addChoice("Projection type:", ZProjector.METHODS, ZProjector.METHODS[method]);
gd.addHelp("https://github.com/tferr/Scripts/tree/master/Annotation#cumulative-z-project");
gd.showDialog();
if (gd.wasCanceled())
return false;
startSlice = (int)Math.min(1, gd.getNextNumber());
stopSlice = (int)Math.max(stopSlice, gd.getNextNumber());
method = gd.getNextChoiceIndex();
return true;
}
run("");