Javascript how-to noob


Can someone point me int he right direction on how to use image calculator in javascripting? I’m trying to convert macro language to something faster in current FIJI. I’ve recorded the step via script recorder (in javascript mode) and see:
ic = new ImageCalculator();
imp1 = WindowManager.getImage(“wrk”);
imp2 = WindowManager.getImage(“MED”);
imp3 =“Subtract”, imp1, imp2);;

I import Classes importClass(Packages.ij.IJ); importClass(Packages.ij.plugin.ImageCalculator);
I have already created imagePlus 3 and 4, so I thought I could write:
imp5 =“Subtract”, imp4, imp3);

But that fails:
java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to ij.ImagePlus
at java.lang.invoke.MethodHandleImpl.newClassCastException(
at java.lang.invoke.MethodHandleImpl.castReference(
So I tried to add the recorder text WindowManager.getImage and the imp name, but that threw error that it didn’t recognize WindowManager. If I used IJ.WindowManager.getImage, then it didn’t recognize the imp name. (my guess is I need to SHOW the imp as a window for this to work; that would be slow for my purpose)

If I have imp already defined, how can I run an imageCalculator? Example, please?


I get this from the macro recorder (‘Substract create’) which works fine:


ic = new ImageCalculator();
imp1 = WindowManager.getImage("MED");
imp2 = WindowManager.getImage("wrk");
imp3 ="Subtract create", imp1, imp2);;
imp5 ="Subtract create", imp2, imp3);;

Please note that the imports have to be made for javascript in FIJI. In Imagej they are default:


thanks for the suggestions. Can this be done without windowmanager get image calls since the imp’s are pre-created? In other words, does mean I must open a window display for each image for this work?


I found an older thread in the ImageJ mailing list which worked by setting the javascript in batch mode or using the -batch argument on the command line:


Packages.ij.macro.Interpreter.batchMode = true;
ic = new ImageCalculator();
imp1 = WindowManager.getImage("MED");
imp2 = WindowManager.getImage("wrk");
imp3 ="Subtract create", imp1, imp2);
imp4 ="Subtract create", imp2, imp3);

Packages.ij.macro.Interpreter.batchMode = false; 


No need to use WindowManager at all if you have your ImagePlus objects in variables already. Just try it :slight_smile:

imp1 = // get your first image from somewhere
imp2 = // get your second image from somewhere else

ic = new ImageCalculator();
imp3 ="Subtract create", imp1, imp2);
imp4 ="Subtract create", imp2, imp3);

You won’t need to use any batch mode settings for this.


Thanks, I tried that and got strange errors indicating the result imp was ‘null’; up to today. Today the code works with really no changes… [strange]

I’m almost running! One last hurdle is I create a running median IMP that I need to close to avoid building up images when running on a stack. I tried “imp4.close()” but the run now requires the user to hit ‘OK’ to close the image (it’s interactive). How to have it close the IMP without a prompt?
I did try the batchmode suggested above, and that does run without prompting, but my code crashed at the end where somehow an input imp got closed inappropriately. Being total noob, I am hoping to close without prompts vs trying to figure out the wrong-close-in-batch issue.


Have a look at the ImageJ API:

To avoid the “Save changes?” dialog, first set the public ‘changes’ variable to false.


Thank you Bio7! That tip saved me weeks of digging, no doubt.
imp3.changes = false; that turns off the ‘save changes’ nags. NICE

I’m using macro code to copy/paste between IMPs, but would “insert” be faster/more direct?
“insert(ImageProcessor ip, int xloc, int yloc)”
"Inserts the image contained in ‘ip’ at (xloc, yloc)."
versus, "Copy", "");
imp2.setSlice(i);, "Paste", "");

(so I would need to getProcessor for the IMP, then run this?)


If that function is what you need (I never used it). You could always measure rough the effciency by yourself, by using something like: