
ij.plugin.ZAxisProfiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ij Show documentation
Show all versions of ij Show documentation
ImageJ is an open source Java image processing program inspired by NIH Image for the Macintosh.
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; i1;
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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy