Search code examples
pluginsscaleimagej

ImageJ: How convert txt file into a plugin


this is really easy question for someone to answer, but I can't seem to resolve it. I downloaded the source code (.java) for the Microscope_Scale plugin (https://imagej.nih.gov/ij/plugins/microscope-scale.html). I edited the file by adding the calibrations for several microscope objectives. I can't seem to figure out how to compile (?) the .txt file into a plugin that ImageJ (also tried FIJI) can run. This is such a fundamental concept, I really can't understand why it doesn't work.

Question is how to convert a previously edited .java file (that is now a .txt file) into a plugin?

The unedited .class file works perfectly well.

Thanks very much, I really appreciate your help!

-jh

Below is the code for the edited plugin:

import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.gui.*;
import ij.measure.Calibration;
import java.awt.*;
import ij.plugin.*;

public class Microscope_Scale implements  PlugInFilter {

/* DESCRIPTION & CUSTOMIZATION INSTRUCTIONS
        This plugin lets you calibrate images spatially using hard-coded arrays of magnifications,
        calibration values and length units. The calibration can optionally be set as global. After
        spatial calibration the plugin will also optionally run Wayne's new scale-bar plugin, available
        in version 1.28h. To customize the plugin for your specific microscope, edit the arrays
        between the "START EDIT" and "END EDIT" comments below. Save anywhere in the
        "plugins" directory, then use "Compile and Run" option under the "plugins" menu to create
        a java class. Restart ImageJ.
**/
        ImagePlus imp;
        private static boolean addScaleBar = false;  //  if true run scalebar plugin - available in version 1.28h
        private static boolean isGlobalCal = false; // if true, set selected calibration as the global calibration
        private static int magIndex = 4; // index of initial selected magnification in dropdown menu

/* START EDIT
        Edit the following arrays using your microscope's nominal magnification steps, the
        corresponding spatial calibration and the length units of the spatial calibration.
        Make sure the arrays are of equal length.
**/
        // nominal magnification settings of the microscope
        private static String[] mags =  { "PLM_10", "PLM_25", "PLM_50", "PLM_100", "Stereo_8", "Stereo_10", "Stereo_12.5", "Stereo_16", "Stereo_20", "Stereo_25", "Stereo_30", "Stereo_35", "Comp_10", "Comp_20", "Comp_40", "Fluoro_10", "Fluoro_20", "Fluoro_40", "Fluoro_100"};
        // spatial calibration for the nominal magnification settings - width of one pixel (pixelWidth)
        private static double[] xscales = {0.193, 0.077, 0.040, 0.020, 0.0066, 0.0053, 0.0043, 0.0034, 0.0027, 0.0022, 0.0018, 0.0015, 0.36337, 0.179533, 0.09107, 0.36630, 0.18450, 0.09208, 0.036144};
        // units for the spacial calibrations given in xscales array above
        private static String[] units =  { "um",  "um",  "um",  "um",  "mm", "mm",  "mm",  "mm",  "mm",  "mm",  "mm",  "mm", "um", "um", "um", "um", "um", "um", "um"};
/* END EDIT **/

        public int setup(String arg, ImagePlus imp) {
                this.imp = imp;
                if (imp==null)
                        {IJ.noImage(); return DONE;}
                return DOES_ALL;
        }

        public void run(ImageProcessor ip) {
                if (doDialog()) {
                        Calibration oc = imp.getCalibration().copy();
                        oc.setUnit(units[magIndex]);
                        oc.pixelWidth=xscales[magIndex];
                        oc.pixelHeight=oc.pixelWidth;
                        if (isGlobalCal) {
                                imp.setGlobalCalibration(oc);
                                int[] list = WindowManager.getIDList();
                                if (list==null) return;
                                for (int i=0; i<list.length; i++) {
                                        ImagePlus imp2 = WindowManager.getImage(list[i]);
                                        if (imp2!=null) imp2.getWindow().repaint();
                                }
                                } else {
                                imp.setGlobalCalibration(null);
                                imp.setCalibration(oc);
                                imp.getWindow().repaint();
                        }
                        if (addScaleBar){
                                IJ.run("Scale Bar...");
                        }
                }
        }

        private boolean doDialog() {
                GenericDialog gd = new GenericDialog("Scale Microscope Image...");
                gd.addChoice("Nominal Magnification", mags, mags[magIndex]);
                gd.addCheckbox("Global Calibration", isGlobalCal);
                gd.addCheckbox("Add Scale Bar", addScaleBar);
                gd.showDialog();
                if (gd.wasCanceled()) {return false;}
                magIndex=gd.getNextChoiceIndex();
                isGlobalCal = gd.getNextBoolean();
                addScaleBar = gd.getNextBoolean();
                return true;
        }
}

Solution

  • I was able to solve my problem by using a different plugin package: Microscope Measurement Tools (https://imagej.net/plugins/microscope-measurement-tools). Very easy to customize to objective-specific calibrations. Note this plugin is for FIJI.