package data_plot; import java.awt.*; import java.util.ArrayList; import ij.*; import ij.gui.*; import ij.process.*; import ij.util.*; import ij.measure.*; import ij.plugin.Straightener; /* ProfileDataPlot is used for displaying of arrows plot * and is largely based on the ProfilePlot class * * @version 1.0; 14 May 2012 * * @author Philippe CARL * @author University of Strasbourg * @author philippe.carl@unistra.fr * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** Creates a density profile plot of a rectangular selection or line selection. */ public class ProfileDataPlot { static final int MIN_WIDTH = 350; static final double ASPECT_RATIO = 0.5; private double min, max; private boolean minAndMaxCalculated; private static double fixedMin = Prefs.getDouble("pp.min",0.0); private static double fixedMax = Prefs.getDouble("pp.max",0.0); protected ImagePlus imp; protected double[] profile; protected double magnification; protected double xInc; protected String units; protected String yLabel; protected float[] xValues; public ProfileDataPlot() { } public ProfileDataPlot(ImagePlus imp) { this(imp, false); } public ProfileDataPlot(ImagePlus imp, boolean averageHorizontally) { this.imp = imp; Roi roi = imp.getRoi(); if (roi==null) { IJ.error("Profile Plot", "Selection required."); return; } int roiType = roi.getType(); if (!(roi.isLine() || roiType==Roi.RECTANGLE)) { IJ.error("Line or rectangular selection required."); return; } Calibration cal = imp.getCalibration(); xInc = cal.pixelWidth; units = cal.getUnits(); yLabel = cal.getValueUnit(); ImageProcessor ip = imp.getProcessor(); //ip.setCalibrationTable(cal.getCTable()); if (roiType==Roi.LINE) profile = getStraightLineProfile(roi, cal, ip); else if (roiType==Roi.POLYLINE || roiType==Roi.FREELINE) { int lineWidth = (int)Math.round(roi.getStrokeWidth()); if (lineWidth==1) profile = getIrregularProfile(roi, ip, cal); else profile = getWideLineProfile(imp, lineWidth); } else if (averageHorizontally) profile = getRowAverageProfile(roi.getBounds(), cal, ip); else profile = getColumnAverageProfile(roi.getBounds(), ip); ip.setCalibrationTable(null); ImageCanvas ic = imp.getCanvas(); if (ic!=null) magnification = ic.getMagnification(); else magnification = 1.0; } //void calibrate(Calibration cal) { // float[] cTable = cal.getCTable(); // if (cTable!=null) // for () // profile[i] = profile[i]; // //} /** Returns the size of the plot that createWindow() creates. */ public Dimension getPlotSize() { if (profile==null) return null; int width = (int)(profile.length*magnification); int height = (int)(width*ASPECT_RATIO); if (widthmaxWidth) { width = maxWidth; height = (int)(width*ASPECT_RATIO); } return new Dimension(width, height); } /** Displays this profile plot in a window. */ public void createWindow() { DataPlot plot = getPlot(); if (plot==null) return; plot.setSourceImageID(imp.getID()); plot.show(); } DataPlot getPlot() { if (profile==null) return null; Dimension d = getPlotSize(); String xLabel = "Distance ("+units+")"; int n = profile.length; if (xValues==null) { xValues = new float[n]; for (int i=0; i0 && (title.length()-index)<=5) title = title.substring(0, index); return title; } /** Returns the profile plot data. */ public double[] getProfile() { return profile; } /** Returns the calculated minimum value. */ public double getMin() { if (!minAndMaxCalculated) findMinAndMax(); return min; } /** Returns the calculated maximum value. */ public double getMax() { if (!minAndMaxCalculated) findMinAndMax(); return max; } /** Sets the y-axis min and max. Specify (0,0) to autoscale. */ public static void setMinAndMax(double min, double max) { fixedMin = min; fixedMax = max; IJ.register(ProfileDataPlot.class); } /** Returns the profile plot y-axis min. Auto-scaling is used if min=max=0. */ public static double getFixedMin() { return fixedMin; } /** Returns the profile plot y-axis max. Auto-scaling is used if min=max=0. */ public static double getFixedMax() { return fixedMax; } double[] getStraightLineProfile(Roi roi, Calibration cal, ImageProcessor ip) { ip.setInterpolate(PlotWindow.interpolate); Line line = (Line)roi; double[] values = line.getPixels(); if (values==null) return null; if (cal!=null && cal.pixelWidth!=cal.pixelHeight) { double dx = cal.pixelWidth*(line.x2 - line.x1); double dy = cal.pixelHeight*(line.y2 - line.y1); double length = Math.round(Math.sqrt(dx*dx + dy*dy)); if (values.length>1) xInc = length/(values.length-1); } return values; } double[] getRowAverageProfile(Rectangle rect, Calibration cal, ImageProcessor ip) { double[] profile = new double[rect.height]; int[] counts = new int[rect.height]; double[] aLine; ip.setInterpolate(false); for (int x=rect.x; x=1.0-inc/2.0) { if (interpolate) value = ip.getInterpolatedValue(x, y); else value = ip.getPixelValue((int)Math.round(x), (int)Math.round(y)); values.add(new Double(value)); lastx=x; lasty=y; } x += xinc; y += yinc; } while (--n2>0); } double[] values2 = new double[values.size()]; for (int i=0; imax) max=value; } this.min = min; this.max = max; } } -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html