Re: Bypassing plugin dialog within macro

Posted by Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Bypassing-plugin-dialog-within-macro-tp3682190p3682267.html

Hi Sidney,

it seems that your itext-1.4.8.jar is corrupted, try to move it out  
of the plugins folder before compiling (you can put it back later,  
but probably it won't work anyhow). Maybe a file downloaded with  
Internet Explorer? (Firefox does not corrupt .zip or .jar files on  
download).

I have also quickly created my own version of Local_Normalization  
(pasted at the end; the mailing list does not allow me to attach it).
It is similar to the one of Daniel Sage, but uses different grayscale  
for the output:
In my version, the background (zero) is always the same, at 128 (8-
bit, RGB), 32768 (16-bit) or 0 (float images), and all images are  
normalized the same way (full image range is 8 sigma).

In Daniel Sage's version, the grayscale is adjusted so that the image  
data fit into the range.

Also, there are small differences near the edge, but that should not  
matter.

Have fun with it,


Michael
________________________________________________________________

On 3 Mar 2011, at 22:44, Sidney Rempel wrote:

> Hi Michael,
> I tried to compile and install the High Pass filter but received  
> these errors:
>
> error: error reading C:\PROGRA~1\ImageJ\plugins\Input-Output
> \itext-1.4.8.jar; cannot read zip file
> C:\Program Files\ImageJ\plugins\Filters\High_pass.java:125:  
> warning: [deprecation] getBoundingRect() in ij.gui.Roi has been  
> deprecated
>                 roi.getBoundingRect().width==ip.getWidth() &&  
> roi.getBoundingRect().height==ip.getHeight()));
>                    ^
> C:\Program Files\ImageJ\plugins\Filters\High_pass.java:125:  
> warning: [deprecation] getBoundingRect() in ij.gui.Roi has been  
> deprecated
>                 roi.getBoundingRect().width==ip.getWidth() &&  
> roi.getBoundingRect().height==ip.getHeight()));
>
> Not sure what I'm missing here...

________________________________________________________________


import ij.plugin.filter.ExtendedPlugInFilter;
import ij.plugin.filter.PlugInFilterRunner;
import ij.plugin.filter.GaussianBlur;
import ij.*;
import ij.gui.GenericDialog;
import ij.gui.DialogListener;
import ij.process.*;
import java.awt.*;
import java.awt.event.*;

/** Local Normalization, as described in
*   http://bigwww.epfl.ch/sage/soft/localnormalization/
*
* Restrictions:
* Only works on the whole image, ROIS are ignored
*
* Code by Michael Schmid, version 2011-03-03
*/

public class Local_Normalization implements ExtendedPlugInFilter,  
DialogListener {
     private static double sigma1 = 10, sigma2 = 20; //filter parameters
     private final int flags = DOES_ALL|CONVERT_TO_FLOAT|SNAPSHOT|
KEEP_PREVIEW|FINAL_PROCESSING;
     private ImagePlus imp;
     private boolean previewing;
     private GaussianBlur gb = new GaussianBlur();


     /**
      * This method is called by ImageJ for initialization.
      * @param arg Unused here. For plugins in a .jar file this  
argument string can
      *            be specified in the plugins.config file of  
the .jar archive.
      * @param imp The ImagePlus containing the image (or stack) to  
process.
      * @return    The method returns flags (i.e., a bit mask)  
specifying the
      *            capabilities (supported formats, etc.) and needs  
of the filter.
      *            See PlugInFilter.java and ExtendedPlugInFilter in  
the ImageJ
      *            sources for details.
      */
     public int setup(String arg, ImagePlus imp) {
         if (IJ.versionLessThan("1.38x"))        // generates an  
error message for older versions
             return DONE;
         this.imp = imp;
         if (arg.equals("final"))
             adjustGrayScale();
         return flags;
     }

     /** Called by ImageJ after setup. */
     public int showDialog(ImagePlus imp, String command,  
PlugInFilterRunner pfr) {
         ImageProcessor ip = imp.getProcessor();
         double min = ip.getMin();
         double max = ip.getMax();
         GenericDialog gd = new GenericDialog(command+" Options");
         gd.addSlider("High Pass Radius (local mean)", 1.0, 100.0,  
sigma1);
         gd.addSlider("Variance Radius", 1.0, 100.0, sigma2);
         gd.addPreviewCheckbox(pfr);     // makes the filter ready  
for preview
         gd.addDialogListener(this);     // the DialogItemChanged  
method will be called on user input
         previewing = true;
         gd.showDialog();                 // display the dialog;  
preview runs in the background now
         previewing = false;
         if (gd.wasCanceled()) {
             ip.setMinAndMax(min, max);          // revert to  
original settings
             return DONE;
         }
         IJ.register(this.getClass());   // protect static class  
variables (filter parameters) from garbage collection
         return IJ.setupDialog(imp, flags);   // ask whether to  
process all slices of stack (if a stack)
     }

     /** Called after modifications to the dialog. Returns true if  
valid input. */
     public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
         sigma1 = gd.getNextNumber();
         sigma2 = gd.getNextNumber();
         return (!gd.invalidNumber() && sigma1>0 && sigma2>0);
     }

     /** Process a FloatProcessor (with the CONVERT_TO_FLOAT flag,  
ImageJ does the conversion to float).
      *  Called by ImageJ for each stack slice (when processing a  
full stack); for RGB also called once for each color. */
     public void run(ImageProcessor ip) {
         if (Thread.currentThread().isInterrupted()) return;
         gb.blurGaussian(ip, sigma1, sigma1, 0.01);
         if (Thread.currentThread().isInterrupted()) return;
         snapshotMinusCurrent(ip);
         if (Thread.currentThread().isInterrupted()) return;
         ImageProcessor ip2 = ip.duplicate();
         if (Thread.currentThread().isInterrupted()) return;
         ip2.sqr();
         if (Thread.currentThread().isInterrupted()) return;
         gb.blurGaussian(ip2, sigma2, sigma2, 0.01);
         if (Thread.currentThread().isInterrupted()) return;
         ip2.sqrt();
         if (Thread.currentThread().isInterrupted()) return;
         float offset = 128;
         float factor = 32;  //so we have 4 sigma inside the  
displayable grayscale range
         if (imp.getType()==ImagePlus.GRAY32) {
             offset = 0; factor = 1;
         } else if (imp.getType()==ImagePlus.GRAY16) {
             offset = 128*256; factor = 32*256;
         }
         divide(ip, ip2, factor, offset);
         if (previewing) adjustGrayScale();
         return;
        }

     private void snapshotMinusCurrent(ImageProcessor ip) {
         float[] pixels = (float[])ip.getPixels();
         float[] snapshot = (float[])ip.getSnapshotPixels();
         for (int i=0; i<pixels.length; i++)
             pixels[i] = snapshot[i]-pixels[i];
     }
     private void divide(ImageProcessor ip1, ImageProcessor ip2,  
float factor, float offset) {
         float[] pixels1 = (float[])ip1.getPixels();
         float[] pixels2 = (float[])ip2.getPixels();
         for (int i=0; i<pixels1.length; i++)
             pixels1[i] = pixels1[i]*factor/pixels2[i]+offset;
     }

     //make FloatProcessor nice to see
     private void adjustGrayScale() {
         ImageProcessor ip = imp.getProcessor();
         if (imp.getType()==ImagePlus.GRAY32)
             ip.setMinAndMax(-2, 2);
         else if (imp.getType()==ImagePlus.GRAY16)
             ip.setMinAndMax(0, 0xffff);
     }

     /** Called by ImageJ to set the number of calls to run(ip)  
corresponding to 100% of the progress bar */
     public void setNPasses(int nPasses) {
         gb.setNPasses(2*nPasses);
     }

}