File: ZAxisProfiler.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (271 lines) | stat: -rw-r--r-- 8,826 bytes parent folder | download
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
package ij.plugin;
import ij.plugin.*;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.*;
import ij.plugin.filter.Analyzer;
import ij.util.Tools;
import java.awt.*;

/** Implements the Image/Stacks/Plot Z-axis Profile command, 
	which plots the selection mean gray value versus slice number.
*/
public class ZAxisProfiler implements PlugIn, Measurements, PlotMaker {
	private static String[] choices = {"time", "z-axis"};
	private static String choice = choices[0];
	private boolean showingDialog;
	private ImagePlus imp;
	private boolean isPlotMaker;
	private boolean timeProfile;
	private boolean firstTime = true;
	private String options;
	
	/** Returns a Plot of the selection mean gray value versus slice number. */
	public static Plot getPlot(ImagePlus imp) {
		return getPlot(imp, "time");
	}

	/** Returns a Plot of the selection mean versus slice number for the
		specified hyperstack, where 'options' can be "time" or "z-axis". */
	public static Plot getPlot(ImagePlus imp, String options) {
		ZAxisProfiler zap = new ZAxisProfiler();
		zap.imp = imp;
		zap.options = options;
		zap.isPlotMaker = true;
		Plot plot = zap.getPlot();
		return plot;
	}

	public void run(String arg) {
		imp = IJ.getImage();
		if (imp.getStackSize()<2) {
			IJ.error("ZAxisProfiler", "This command requires a stack.");
			return;
		}
		isPlotMaker = true;
		Plot plot = getPlot();
		if (plot!=null) {
			if (isPlotMaker)
				plot.setPlotMaker(this);
			plot.show();
		}
	}
		
	public Plot getPlot() {
		Roi roi = imp.getRoi();
		ImageProcessor ip = imp.getProcessor();
		double minThreshold = ip.getMinThreshold();
		double maxThreshold = ip.getMaxThreshold();
		float[] y;
		boolean hyperstack = imp.isHyperStack();
		if (hyperstack)
			y = getHyperstackProfile(imp, minThreshold, maxThreshold);
		else
			y = getZAxisProfile(imp, minThreshold, maxThreshold);
		if (y==null)
			return null;
		float[] x = new float[y.length];
		
		String xAxisLabel = showingDialog&&choice.equals(choices[0])?"Frame":"Slice";
		Calibration cal = imp.getCalibration();
		double calFactor = 1.0;
		double origin = -1;
		if (cal.scaled()) {
			if (timeProfile) {
				calFactor = (float) cal.frameInterval;
				boolean zeroInterval = calFactor==0;
				if (zeroInterval)
					calFactor = 1;
				else
					origin = 0;
				String timeUnit = zeroInterval?"Frame":"["+cal.getTimeUnit()+"]";
				xAxisLabel = timeUnit;
			} else {
				calFactor = (float) cal.pixelDepth;
				boolean zeroDepth = calFactor==0;
				if (zeroDepth)
					calFactor = 1;
				else
					origin = cal.zOrigin;
				String depthUnit = zeroDepth?"Slice":"["+cal.getZUnit()+"]";
				xAxisLabel = depthUnit;
			}
		}
		for (int i=0; i<x.length; i++)
			x[i] = (float)((i-origin)*calFactor);

		String title;
		if (roi!=null) {
			Rectangle r = roi.getBounds();
			title = imp.getTitle()+"-"+r.x+"-"+r.y;
		} else
			title = imp.getTitle()+"-0-0";
		//String xAxisLabel = showingDialog&&choice.equals(choices[0])?"Frame":"Slice";
		Plot plot = new Plot(title, xAxisLabel, "Mean", x, y);
		boolean useConnectedCircles = x.length<=60; 	//not LINE but CONNECTED_CIRCLES
		if (useConnectedCircles)
			plot.setStyle(0, "black, gray, 1, connected");
		plot.setColor(Color.black);
		double ymin = ProfilePlot.getFixedMin();
		double ymax = ProfilePlot.getFixedMax();
		if (!(ymin==0.0 && ymax==0.0)) {
			double[] a = Tools.getMinMax(x);
			double xmin=a[0]; double xmax=a[1];
			plot.setLimits(xmin, xmax, ymin, ymax);
		} else {
			double[] a = Tools.getMinMax(y);
			ymin = a[0]; ymax = a[1];
		}
		//draw a blue vertical line for the current stack position (in live mode)
		int pos = imp.getCurrentSlice();
		if (hyperstack) {
			if (timeProfile)
				pos = imp.getT();
			else
				pos = imp.getZ();
		}
		double xx = (pos - 1 - origin)*calFactor;
		if (!useConnectedCircles) {  //For a line plot, the frame starts and ends at the first and last point
			if (pos==1)              //shift 1 pxl towards the center to avoid hiding the blue line hidden behind the frame
				xx += calFactor*Math.min(0.4, x.length*0.7/(double)plot.getDrawingFrame().width);
			else if (pos==x.length)
				xx -= calFactor*Math.min(0.4, x.length*0.7/(double)plot.getDrawingFrame().width);
		}
		if (firstTime)               //don't draw the line for the first time, create an (invisible) PlotObject
			xx = Double.NaN;         //(otherwise, the number of PlotObjects would change, breaking the Plot.COPY_EXTRA_OBJECTS)
		plot.setColor(Color.blue);
		plot.drawLine(xx, ymin-10*(ymax-ymin), xx, ymax+10*(ymax-ymin));
		plot.setColor(Color.black);
		plot.setLineWidth(1);
		firstTime = false;
		return plot;
	}
	
	public ImagePlus getSourceImage() {
		return imp;
	}

	private float[] getHyperstackProfile(ImagePlus imp, double minThreshold, double maxThreshold) {
		Roi roi = imp.getRoi();
		int slices = imp.getNSlices();
		int frames = imp.getNFrames();
		int c = imp.getC();
		int z = imp.getZ();
		int t = imp.getT();
		int size = slices;
		if (firstTime)
			timeProfile = slices==1 && frames>1;
		if (options==null && slices>1 && frames>1 && (!isPlotMaker ||firstTime)) {
			showingDialog = true;
			GenericDialog gd = new GenericDialog("Profiler");
			gd.addChoice("Profile", choices, choice);
			gd.showDialog();
			if (gd.wasCanceled())
				return null;
			choice = gd.getNextChoice();
			timeProfile = choice.equals(choices[0]);
		}
		if (options!=null)
			timeProfile = frames>1 && !options.contains("z");
		if (timeProfile)
			size = frames;
		else
			size = slices;
		float[] values = new float[size];
		Calibration cal = imp.getCalibration();
		ResultsTable rt = new ResultsTable();
		Analyzer analyzer = new Analyzer(imp, rt);
		int measurements = Analyzer.getMeasurements();
		boolean showResults = !isPlotMaker && measurements!=0 && measurements!=LIMIT;
		measurements |= MEAN;
		if (showResults) {
			if (!Analyzer.resetCounter())
				return null;
		}
		ImageStack stack = imp.getStack();
		boolean showProgress = size>400 || stack.isVirtual();
		for (int i=1; i<=size; i++) {
			if (showProgress)
				IJ.showProgress(i,size);
			int index = 1;
			if (timeProfile)
				index = imp.getStackIndex(c, z, i);
			else
				index = imp.getStackIndex(c, i, t);
			ImageProcessor ip = stack.getProcessor(index);
			if (minThreshold!=ImageProcessor.NO_THRESHOLD)
				ip.setThreshold(minThreshold,maxThreshold,ImageProcessor.NO_LUT_UPDATE);
			ip.setRoi(roi);
			ImageStatistics stats = ImageStatistics.getStatistics(ip, measurements, cal);
			analyzer.saveResults(stats, roi);
			values[i-1] = (float)stats.mean;
		}
		if (showResults)
			rt.show("Results");
		return values;
	}

	private float[] getZAxisProfile(ImagePlus imp, double minThreshold, double maxThreshold) {
		Roi roi = imp.getRoi();
		ImageStack stack = imp.getStack();
		if (firstTime) {
			int slices = imp.getNSlices();
			int frames = imp.getNFrames();
			timeProfile = slices==1 && frames>1;
		}
		int size = stack.size();
		boolean showProgress = size>400 || stack.isVirtual();
		float[] values = new float[size];
		Calibration cal = imp.getCalibration();
		ResultsTable rt = new ResultsTable();
		Analyzer analyzer = new Analyzer(imp, rt);
		int measurements = Analyzer.getMeasurements();
		boolean showResults = !isPlotMaker && measurements!=0 && measurements!=LIMIT;
		boolean showingLabels = firstTime && showResults && ((measurements&LABELS)!=0 || (measurements&SLICE)!=0);
		measurements |= MEAN;
		if (showResults) {
			if (!Analyzer.resetCounter())
				return null;
		}
		boolean isLine = roi!=null && roi.isLine();
		int current = imp.getCurrentSlice();
		for (int i=1; i<=size; i++) {
			if (showProgress)
				IJ.showProgress(i,size);
			if (showingLabels)
				imp.setSlice(i);
			ImageProcessor ip = stack.getProcessor(i);
			if (ip==null) {
				IJ.log("ZAxisProfiler: stack.getProcessor("+i+") returned null ("+stack.getClass().getName()+","+ imp+")");
				values[i-1] = Float.NaN;
				continue;
			}
			if (minThreshold!=ImageProcessor.NO_THRESHOLD)
				ip.setThreshold(minThreshold,maxThreshold,ImageProcessor.NO_LUT_UPDATE);
			ip.setRoi(roi);
			ImageStatistics stats = null;
			if (isLine)
				stats = getLineStatistics(roi, ip, measurements, cal);
			else
				stats = ImageStatistics.getStatistics(ip, measurements, cal);
			analyzer.saveResults(stats, roi);
			values[i-1] = (float)stats.mean;
		}
		if (showResults)
			rt.show("Results");
		if (showingLabels)
			imp.setSlice(current);
		return values;
	}
	
	private ImageStatistics getLineStatistics(Roi roi, ImageProcessor ip, int measurements, Calibration cal) {
		ImagePlus imp = new ImagePlus("", ip);
		imp.setRoi(roi);
		ProfilePlot profile = new ProfilePlot(imp);
		double[] values = profile.getProfile();
		ImageProcessor ip2 = new FloatProcessor(values.length, 1, values);
		return ImageStatistics.getStatistics(ip2, measurements, cal);
	}
	
}