import java.awt.*; import ij.*; import ij.gui.*; import ij.plugin.filter.PlugInFilter; import ij.process.*; /** * The user types in a width and height value, and the plugin creates * a rectangular or oval region of interest based on those values. * (Why? Because it can be time-consuming to repeatedly create a * ROI of an exact size with the mouse.) * * @author Jeffrey Kuhn * @author The University of Texas at Austin * @author jkuhn@ccwf.cc.utexas.edu */ public class SpecifyROI_ implements PlugInFilter { int iX; int iY; int iWidth, iHeight; boolean bAbort; ImagePlus imp; static boolean oval; /** * Called by ImageJ when the filter is loaded */ public int setup(String arg, ImagePlus imp) { this.imp = imp; if (arg.equals("about")) {showAbout(); return DONE;} return DOES_ALL+NO_CHANGES; } /** * Called by ImageJ to process the image */ public void run(ImageProcessor ip) { bAbort = false; Rectangle r = ip.getRoi(); iWidth = r.width; iHeight = r.height; getROIWidthAndHeight(); if (bAbort) return; iX = r.x; iY = r.y; iX += (r.width - iWidth) / 2; iY += (r.height - iHeight) / 2; if (oval) imp.setRoi(new OvalRoi(iX, iY, iWidth, iHeight,imp)); else imp.setRoi(iX, iY, iWidth, iHeight); IJ.register(SpecifyROI_.class); } /** * Creates a dialog box, allowing the user to enter the requested * width and height of a rectangular Region Of Interest */ void getROIWidthAndHeight() { GenericDialog gd = new GenericDialog("ROI Size", IJ.getInstance()); gd.addNumericField("Width:", iWidth, 0); gd.addNumericField("Height:", iHeight, 0); gd.addCheckbox("Oval", oval); gd.showDialog(); if (gd.wasCanceled()) { bAbort = true; return; } iWidth = (int) gd.getNextNumber(); iHeight = (int) gd.getNextNumber(); oval = gd.getNextBoolean(); } /** * Displays a short message describing the filter */ void showAbout() { IJ.showMessage("About ROISelect_...", "This sample allows a ROI of a specific size to be specified.\n" ); } }