Input data in a plugin pop-up during a loop

Totally new to Java,
From a video with changing light intensity I extract a stack of images. I would like to normalize the brigthness/contrast in this stack of images (beside doing other things). I found a plugin that might work for me Stack_Normalizer.class, that you can find here https://imagej.nih.gov/ij/plugins/normalizer.html

I have many images to process so I created a loop. Here it comes my problem: when I run the plug in in the for loop it asks me to input the minimum and maximum values wanted (always the same in may case), since I have many thousands images I cannot to it manually (it works but it’s super long), how can I set this values automatically in the pup up window? I tried setMinimum(0), setMaximum(255) but no success. Here is part of the loop:

       makeRectangle(126, 20, 433, 422);
       run("Crop");
       run("Stack Normalizer");

here I would need to set the values in the pop up window. Then the loop continue…

       run("8-bit");
       setThreshold(0,79);
       run("Threshold");

Thanks a lot for any input, I hope is not a too common question, unfortunately I could not find an answer in the forum!!

Hi @havefun,
Without having tested the Stack Normalizer myself, I would guess that if you record the plugin it should also record the parameters you set and avoid the popup. The recorder you find under >plugins >macros >record…

Hi @havefun and welcome,

The problem that you have is that this plugin was written in a way that is not very modular because it relies on GenericDialog, which is necessarily showing a window (this is not true - see edit). (source code is here : https://imagej.nih.gov/ij/plugins/download/Stack_Normalizer.java).

As I am learning how to make things modular in ImageJ2, I “translated” it in a more modern way, by using ImageJ2 commands. You can find this version here:

To get it to work on FIJI, copy the file Stack_Normalizer.groovy into ImageJ.app/plugins/Scripts as detailed here : https://imagej.net/Scripting

Then the command can be found in the main gui under Plugins > Scripts > Stack Normalizer (and it is recordable, and no window should show up if you provide the argument).

Let us know how it goes!

Edit : @anon96376101’s solution is easier (I just understood that the macro’s parameters are given to the GenericDialog with this line: GenericDialog gd = new GenericDialog("Normalize to:", IJ.getInstance()); )

1 Like

Good day Nicolas Chiaruttini,

as Jan Brocher has suggested, plugin “Stack_Normalizer” is perfectly macro-recordable.
Here is an example:

run( "Stack Normalizer", "minimum=22 maximum=220" );

As always, the variables can be introduced by string concatenation:

min = 22;
max = 220;
run( "Stack Normalizer", "minimum=" + min + " maximum=" + max );

Consequently, I see no reason to recode the plugin.

Regards

Herbie

2 Likes

Hi, if I run this line I receive the Macro error code: undefined variable in line2. Any idea why it might be wrong?

I still learned something :wink: . But thanks!

Good day,

no idea what’s wrong on your side …

This simple example macro

run("Bat Cochlea Volume (19K)");
run( "Stack Normalizer", "minimum=22 maximum=220" );

works for me and gives the following output to the Log-window:
0.0 255.0 0.7764705882352941 22.0

Paste the above two macro code lines to an empty macro window (Plugins >> New >> Macro) and run it.
(You must have an open internet connection to load the example stack!)

Maybe you post your macro here.

Regards

Herbie

1 Like

When I run this a Console opens saying :
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 9: unexpected char: ‘#’ @ line 9, column 1.
#@ImagePlus(label=“Image to be normalized”, description=“your image”) myImage
^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)

Re-hello @havefun,

Arf, do you have a fully updated Fiji ?

Anyway you should follow @anon96376101 's solution which is much more simple.

Best,

It runs but it gives me the error.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1329.groovy: 9: unexpected char: ‘#’ @ line 9, column 1.
#@ImagePlus(label=“Image to be normalized”, description=“your image”) myImage
^

1 error

	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
	at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149)

The funny thing is that if I run it manually (using only, run( “Stack Normalizer”) )it works.

Here is my macro (while I run it I have an open background pics):

    dir1 = getDirectory("Choose a Directory ");
    setBatchMode(true);
    dir2 = getDirectory("Choose Destination Directory ");
    list = getFileList(dir1);
    imgArray=newArray(nImages)
    imgArray[0]=getImageID();

    for (i=0; i<list.length; i++) {
    	showProgress(i+1, list.length);
    	open(dir1+list[i]);
    	makeRectangle(126, 20, 433, 422);
    	run("Crop");
    	run( "Stack Normalizer", "minimum=22 maximum=220");
    	run("8-bit");
    	setThreshold(0,79);
    	run("Threshold");
    	imageCalculator("subtract create", list[i], imgArray[0]);
    	run("Invert");
    	run("Make Binary");
    	saveAs("TIFF", dir2+list[i]);
    	run("Set Measurements...", "area mean center perimeter fit shape display redirect=None decimal=5");
    	run("Analyze Particles...", "size=100-1000 circularity=0.00-1.00 show=Masks display record slice");
    	close();
    }

Do you use stacks or single images?

Clueless

Herbie

Silly me, I had the Stack Normalizer.groove in the same folder and it didn’t like that…now it works, THANKS A LOT!!!