Search code examples
javaimagej

Different results with application and code in ImageJ


My problem appeared when I tried to run a picture analysis with ImageJ and try to calculate porosity.

I recorded the macros and run the same procedure in java. The results from these two executions are different and I don't know why. Could you help?

I tried to write the code again using ImageJ API. The two samples I run are shown below:

    ImagePlus imp = new ImagePlus();
    imp = IJ.openImage("path_to_image");
    int measurements = Measurements.AREA + Measurements.MEAN +Measurements.STD_DEV +    Measurements.AREA_FRACTION;
    ResultsTable rt = new ResultsTable();
    Analyzer analyzer = new Analyzer(imp, measurements, rt);
    IJ.setAutoThreshold(imp, "Default");
    System.out.println(rt.MIN);

And with recorded code

    imp = IJ.openImage("path_to_image");
    IJ.run("Set Measurements...", "area mean standard modal min median area_fraction limit redirect=None decimal=3");
    IJ.setAutoThreshold(imp, "Default");
    IJ.run(imp, "Measure", "");

Solution

  • Your code has several problems:

    The code that you recorded seems to be Javascript, not ImageJ macro code, so here are some versions of your script that all take the Blobs sample image (File > Open Samples > Blobs (25K)), set the default threshold and measure the same parameters.

    ImageJ1 macro:

    run("Blobs (25K)");
    setAutoThreshold("Default");
    run("Set Measurements...", "area mean standard modal min median area_fraction limit redirect=None decimal=3");
    run("Measure");
    

    Javascript:

    importClass(Packages.ij.IJ);
    
    imp = IJ.openImage("http://imagej.nih.gov/ij/images/blobs.gif");
    IJ.setAutoThreshold(imp, "Default");
    IJ.run("Set Measurements...", "area mean standard modal min median area_fraction limit redirect=None decimal=3");
    IJ.run(imp, "Measure", "");
    imp.show();
    

    Beanshell (which is closest to Java code):

    import ij.IJ;
    
    imp = IJ.openImage("http://imagej.nih.gov/ij/images/blobs.gif");
    IJ.setAutoThreshold(imp, "Default");
    IJ.run("Set Measurements...", "area mean standard modal min median area_fraction limit redirect=None decimal=3");
    IJ.run(imp, "Measure", "");
    imp.show();
    

    If you want to avoid calls to IJ and use the lower level ImageJ API, this is how it looks in Beanshell:

    import ij.ImagePlus;
    import ij.IJ;
    import ij.measure.Measurements;
    import ij.measure.ResultsTable;
    import ij.plugin.filter.Analyzer;
    
    ImagePlus imp = IJ.openImage("http://imagej.nih.gov/ij/images/blobs.gif");
    // IJ.setAutoThreshold(imp, "Default");
    imp.getProcessor().setAutoThreshold("Default");
    
    // IJ.run("Set Measurements...", "area mean standard modal min median area_fraction limit redirect=None decimal=3");
    int measurements = Measurements.AREA + Measurements.MEAN + Measurements.MIN_MAX + Measurements.STD_DEV + Measurements.MODE + Measurements.MEDIAN + Measurements.AREA_FRACTION + Measurements.LIMIT;
    
    // IJ.run(imp, "Measure", "");
    ResultsTable rt = new ResultsTable();
    Analyzer analyzer = new Analyzer(imp, measurements, rt);
    analyzer.measure();
    
    Double result = rt.getValue("Min", rt.getCounter() - 1); // get value of interest
    IJ.log(result.toString()); // print to log window
    // Alternatively, show the full results table
    // rt.show("New Results");