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
|
package ij.plugin.filter;
import java.awt.*;
import java.awt.image.*;
import java.util.Vector;
import java.io.*;
import ij.*;
import ij.process.*;
import ij.io.*;
import ij.gui.*;
import ij.measure.*;
import java.awt.geom.*;
/** Saves the XY coordinates of the current ROI boundary. */
public class XYWriter implements PlugInFilter {
ImagePlus imp;
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_ALL+ROI_REQUIRED+NO_CHANGES;
}
public void run(ImageProcessor ip) {
saveXYCoordinates(imp);
}
public void saveXYCoordinates(ImagePlus imp) {
Roi roi = imp.getRoi();
if (roi==null)
throw new IllegalArgumentException("ROI required");
SaveDialog sd = new SaveDialog("Save Coordinates as Text...", imp.getTitle(), ".txt");
String name = sd.getFileName();
if (name == null)
return;
String directory = sd.getDirectory();
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(directory+name)));
}
catch (IOException e) {
IJ.error("XYWriter", "Unable to save coordinates:\n "+e.getMessage());
return;
}
Calibration cal = imp.getCalibration();
String ls = System.getProperty("line.separator");
if (roi.subPixelResolution()) {
FloatPolygon p = roi.getFloatPolygon();
for (int i=0; i<p.npoints; i++)
pw.print(IJ.d2s((p.xpoints[i])*cal.pixelWidth,4) + "\t" + IJ.d2s((p.ypoints[i])*cal.pixelHeight,4) + ls);
} else {
Polygon p = roi.getPolygon();
boolean scaled = cal.scaled();
for (int i=0; i<p.npoints; i++) {
if (scaled)
pw.print(IJ.d2s((p.xpoints[i])*cal.pixelWidth,4) + "\t" + IJ.d2s((p.ypoints[i])*cal.pixelHeight,4) + ls);
else
pw.print((p.xpoints[i]) + "\t" + (p.ypoints[i]) + ls);
}
}
pw.close();
}
}
|