Macro Some additional flexibility is available when calling TurboReg from ImageJ's macro language, which may be suitable in certain specialized situations. Most importantly, the landmark positions are directly accessible to further processing. The ImageJ macro language is 'typeless'. Variables do not need to be declared and do not have explicit data types. They are automatically initialized when used in an assignment statement. A variable can contain a number, a boolean, a string or an array. Projects for 2021 haven't been selected yet. You can see past years in the archive. ImageJ comes with a built in tool for recording the steps you perform on an image into the ImageJ macro language. This is a convenient way to grab the actions you take so that they can be incorporated into a batch macro. To open the Macro recorder, click on the Plugins menu item Macros Record.
ImageJ Macro language (IJM) which is designed to be easy to read, learn and use. Programs written in the IJM, or macros, can be used to perform a sequence of actions in ImageJ. ImageJ also supports several other scripting languages.
home | news | docs | download | plugins | resources | list | links
There are more than300 example macros on the ImageJWeb site. To try one, open it in a browser window, copy it to the clipboard (crtl-a, ctrl-c), switch to ImageJ, open an editor window (ctrl-shift-n), paste (ctrl-v), then run itusing the editor's Macros>Run Macro command (ctrl-r). Most of the example macros are also available in themacros folder, inside the ImageJ folder.
print('Hello world');To test the macro, use the editor's Macros>Run Macro command (or press ctrl-r).To save it, use the editor's File>Save As command.In the Save As dialog box, enter 'Hello_World.txt' as file name, then click 'Save'. The macro will be automatically installed as a 'Hello World' command in the Plugins menu when you restart ImageJ, assuming the file name has an underscore in it and the macro was saved in the plugins folder or a subfolder. You can run this macro by pressing a single key by creatinga shortcut using Plugins>Shortcuts>Create Shortcut.
To re-open a macro, use the File>Open or Plugins>Macros>Edit commands,or drag and drop it on the 'ImageJ' window.
Use the Command Recorder (Plugins>Macros>Record) to generate run() function calls.For example, this code was recorded by running the Process>Find Maxima... command:
This code runs Find Maxima... with 'Noise tolerence' set to 50,'Output type' set to 'Point Selection' and 'Light background' enabled.String parameters containing spaces, such as 'Point Selection', must beenclosed in brackets.To test it, copy it to the clipboard, switch to ImageJ, pressshift-b (File>Open Samples>Blobs),press shift-n (File>New>Text Window),press ctrl-v (Edit>Paste),then run it by pressing ctrl+r (File>Macros>RunMacro).
The input fields of a dialog can be set to the contents of a macro variablesby using string concatentation:
With ImageJ 1.43 and later, there is an easier method that only requires adding '&' to thevariable name in the options string:String variables passed using this method do not have to be enclosed in brackets.For more examples, see theArgumentPassingDemo macro.run('Measure');is generated when you use the Analyze>Measure and Analyze>Label commands with the recorder running. Save this macro in the plugins folder, or a subfolder, as 'Measure_And_Label.txt', restart ImageJ and there will be a new 'Measure And Label' command in the Plugins menu. Use the Plugins>Shortcuts>Create Shortcut command to assign this new command a keyboard shortcut.
run('Label');
In this example, the 'Measure And Label' macro is assigned to the F1 key. Note how the underscores in the macro filename (at least one is required) are converted to spaces in the command name.
Macros in a macro set can communicate with eachother using global variables.
Use the editor's File>Save As command to create a macro file containing these two macros. Name it something like 'MyMacros.txt' and save it in the macros folder inside the ImageJ folder. (Note that the '.txt extension is required.) Then, to install the macros in the Plugins>Macros submenu, usethe Plugins>Macros>Install command and select 'MyMacros.txt' in the file open dialog. Change the name to'StartupMacros.txt' and ImageJ will automatically install the macros when it starts up.Note that keyboard shortcuts will not work unless the macros are installed and the 'ImageJ' window, or an image window, is the active (front) window and has keyboard focus.You install macros using the macro editor's Macros>Install Macros command or the Plugins>Macros>Install command.Install the two macros in the above example and you will see that the commands
get added to Plugins>Macros submenu. Save these macros in a file named 'StartupMacros.txt'in the macros folder and ImageJ will automatically install them when it starts up.Function keys ([f1], [f2]...[f12]) and numeric keypad keys ([n0], [n1]..[n9], [n/], [n*], [n-], [n+] or [n.]) can also be used for shortcuts.ImageJ will display an error message if a function key shortcut duplicatesa shortcut used by a plugin. Numeric keypad shortcuts are only used by macros so duplicates are not possible.With ImageJ 1.53a and later, use [&n] to createshortcuts that work on keyboards with and withoutnumeric keypadsNote that on PCs, numeric keypadshortcuts might only work when the Num Lock light is on.A more extensive example(KeyboardShortcuts)is available.
You can also permanently install a macro tool by adding it to the'>>' menu in the toolbar. As an example, save the 'Sample Tool' macroin the ImageJ/plugins/Tools folder as 'Sample_Tool.txt', restartImageJ, and select 'Sample Tool' from the toolbar's '>>' menu.
A tool can display a configuration dialog box when the user double clicks on it. To set this up, add a macro that has the samename as the tool, but with ' Options' added, and that macro will be called each time the user double clicks on the tool icon. In this example, the getNumber dialog isdisplayed when the users double clicks on the circle tool icon.
More examples are available at rsb.info.nih.gov/ij/macros/tools/or in the ImageJ/macros/tools folder.Use File>Open (or drag and drop) to open one of these files and the tools defined in it will be automatically installed,or use the Plugins>Macros>Install command to install tools without openingan editor window.
The /ij/macros/toolsets folder on the ImageJ website contains 13 example tool sets.
Alternatively, starting with ImageJ 1.53b, tool icons can be providedby a 24x24 image located in the macros/toolsets/icons/folder using the syntax
macro 'Test Tool - icon:name.png' { }
Icon files can be created with ImageJ by editing a 24x24 image andsaving it in JPG, BMP, GIF or PNG formats. Third party software, suchas Inkscapecan be used to produce high quality icons with transparency.Simply export the current selection usingFile>Export PNG Image with the image size set to 24x24 pixels.
Variable names are case-sensitive.'Name' and 'name' are different variables.
In the following example, a number, a string and an array are assigned to the same variable.
You can run this code by selecting it, copying it to the clipboard (ctrl-C), switching to ImageJ, opening an editor window (Edit>New), pasting (ctrl-V), then pressing ctrl-R. (Note: on the Mac, use the apple key instead of the control key.)Global variables should be declared before the macros that use themusing the 'var' statement. For example:
The 'var' statement should not be used inside macro or function code blocks. Using 'var' in a macroor function may cause it to fail.In this example, a for loop is used to print the values 0, 10, 20...90.
In this example, a while loop is used to print the values 0, 10, 20...90.
Instead of evaluating the condition at the top of the loop, do-while evaluates it at the bottom. Thus the code block is always executed at least once.In this example, a do...while loop is used to print the values 0, 10, 20...90.
Functions can use the return statement to return a value.The number of arguments given when calling a function mustcorrespond to the number of arguments in the function definition. Thesum() function has two arguments so it must called with two arguments.A function definition with no arguments must include the parenthesesand, unlike built-in functions, must be called with parentheses.Basic data types (strings and numbers) are passed to a function by value; arrays are passed by reference.Use the ', '!=', '>' and 'true.
A description of all the ImageJ command line options is available atrsb.info.nih.gov/ij/docs/install/linux.html#options.
There are seven commands in the Debug menu:
top | home | news | docs | download | plugins | resources | list | links