Taking user input for java plugin

macrorecorder
plugin
Tags: #<Tag:0x00007fa307595950> #<Tag:0x00007fa307595770>

#1

Hello! I am getting started with ImageJ, the code below does bicubic interpolation and unsharp masking. I want to take the Scale of bicubic interpolation, radius of gaussian blur and mask coefficient as user input. Please suggest the changes needed?

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {

	public void run(String arg) {
		ImagePlus imp = IJ.getImage();
		IJ.run(imp, "Scale...", "x=3 y=3 interpolation=Bicubic average create");
		ImagePlus scaledImp = IJ.getImage();		
		IJ.run(scaledImp, "Unsharp Mask...", "radius=1 mask=0.90");
		imp.show();
	}

Can this be done through macro recorder?


#2

You could do this with a plugin if you want. You also can certainly accomplish this with the macro language or as a script quite easily. You can use script parameters to automagically generate the dialog for you. In Jython, you could implement the bicubic interpolation and dialog for the scaling factors as follows:

# @ImagePlus(label="Image to process") imp
# @float(label="x scaling factor: ") x_factor
# @float(label="x scaling factor: ") y_factor

from ij import IJ

IJ.run(imp, "Scale...", "x="+str(x_factor)+" y="+str(y_factor)+" interpolation=Bilinear average create");
imp.show()

I hyperlinked to a couple resources that may be handy! Also, there are a bunch of templates in the script editor that I found particularly useful for picking up syntax and structure (Plugins -> New -> Macro and click Templates). Also, an easy way to figure out the command needed for something is to open the macro recorder and set the “Recorder” drop down variable to JavaScript.

I hope this helps you out! Let me know how adding the additional processing steps go! :rocket:

Cheers,
Andy


#3

Hello Adit -

In addition to using script parameters as Andy suggested, you
can also simply provide an empty parameter string to IJ.run():

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {

	public void run(String arg) {
		ImagePlus imp = IJ.getImage();
		// IJ.run(imp, "Scale...", "x=3 y=3 interpolation=Bicubic average create");
		IJ.run(imp, "Scale...", "");
		ImagePlus scaledImp = IJ.getImage();		
		// IJ.run(scaledImp, "Unsharp Mask...", "radius=1 mask=0.90");
		IJ.run(scaledImp, "Unsharp Mask...", "");
		imp.show();
	}
}

When you give a blank parameter string to IJ.run(), as in the
above sample code, the commands being called are generally
smart enough to open up their own standard parameter dialogs
to let the user enter the parameter values interactively.

(I don’t know how to make this happen only using the macro
recorder, but it’s certainly quite easy to edit the code to put
in the empty parameter string by hand.)

Also, if you want greater control over getting the parameters
interactively than offered by this method (or script paramters),
it is also relatively easy to use a Generic Dialog to customize
how you read in the parameters.

Thanks, mm


#5

Thanks a lot for your prompt reply,
It gives the following error, when run as a python file.

Traceback (most recent call last):
  File "<string>", line 7, in <module>
NameError: name 'imp' is not defined

Have a look


#6

Thanks for your prompt reply!

It works well, if we pass an empty string, the in-built dialog box is called.


#7

How are you running it? It works for me when I do the following:

I ran it as follows:

  1. File -> New -> Script.
  2. Copy and paste the script above.
  3. Set the language to Python.
  4. Run.

Bonus Feature :+1:
I have found the following feature of Fiji very useful. If I then save my Python script as a file in the …/Fiji.app/plugins/Scripts/Plugins/ in a new folder, perhaps “My_Scripts” as filename_.py then I can run it from the ImageJ menu by navigating through Plugins -> My_Scripts -> filename. You must have the underscore at the end of the filename. More on that can be found here.

Let me know if you get it working!

Cheers,
Andy