You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix header levels and remove explicit numbering.
* Remove unneeded Fiji mentions. This article is about ImageJ features.
* Use notice includes for notice blocks.
* Use img includes for images. Particularly to ensure max-width=100%.
* Use bc include for menu command references.
Copy file name to clipboardExpand all lines: _pages/tutorials/batch-processing-with-ij-macro.md
+44-48Lines changed: 44 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,9 @@ project: /software/imagej
5
5
6
6
{% include notice glyph="🎓" content="See also the [Introduction into Macro Programming](/scripting/macro) and [Batch Processing](/scripting/batch) articles." %}
7
7
8
-
# Introduction
8
+
##Introduction
9
9
10
-
One of the great strengths of Fiji is its ability to automate workflows. If you have a workflow you wish to automate, Fiji provides you with a number of options:
10
+
One of the great strengths of ImageJ is its ability to automate workflows. If you have a workflow you wish to automate, there are a number of options:
@@ -16,7 +16,7 @@ One of the great strengths of Fiji is its ability to automate workflows. If you
16
16
17
17
Please note that the above list is not exhaustive! In this tutorial, we will be exploring option #1. However, it is advisable to experiment with the other options above - you may find an alternative approach (or perhaps a combination of approaches) is more suitable for your requirements.
18
18
19
-
## Overview of this tutorial
19
+
###Overview of this tutorial
20
20
21
21
This tutorial demonstrates how to
22
22
1. Use [the macro recorder](/scripting/macro#the-recorder) to record a series of commands to form the basis of a macro
@@ -25,63 +25,61 @@ This tutorial demonstrates how to
25
25
4. Add some progress updates
26
26
5. Add a dialog so that a user can modify the parameters to the macro prior to execution
27
27
28
-
> [!NOTE]
29
-
> Data from the [Image Data Resource](https://idr.openmicroscopy.org/) is used in this tutorial, [which is browsable online](https://idr.openmicroscopy.org/webclient/?show=image-2874779). Instructions on downloading images from the IDR are [here](https://idr.openmicroscopy.org/about/download.html). Below we outline a simple macro designed to count nuclei in 10 such images, an example of which is shown below.
28
+
Data from the [Image Data Resource](https://idr.openmicroscopy.org/) is used in this tutorial, [which is browsable online](https://idr.openmicroscopy.org/webclient/?show=image-2874779). Instructions on downloading images from the IDR are [here](https://idr.openmicroscopy.org/about/download.html). Below we outline a simple macro designed to count nuclei in 10 such images; here is an example such image:
30
29
31
-

30
+
{% include img src="IDR0028-LM2_siGENOME_1A_Well_C3_Field_10.png" alt="IDR0028 LM2_siGENOME_1A Well C3 Field 10" %}
32
31
33
-
#1. Record Commands with the Macro Recorder
32
+
##Record Commands with the Macro Recorder
34
33
35
-
##1.1 Start the macro recorder
34
+
###Start the macro recorder
36
35
37
-
To start the macro recorder, go to `Plugins > Macros > Record`:
36
+
To start the macro recorder, go to {% include bc path="Plugins|Macros|Record" %}:
38
37
39
-

38
+
{% include img src="screenshot-plugins-macro-record.PNG" alt="Macro Recorder location on plugins menu" %}
40
39
41
40
Every command you now access through ImageJ's menu will be recorded as a line of text in the macro recorder.
42
41
43
-
> [!IMPORTANT]
44
-
> The vast majority of the functionality in ImageJ/Fiji's menus is macro-recordable. Occasionally, some commands will not be recorded, or not recorded correctly. Please refer to the documentation and [image.sc](https://forum.image.sc/) in such cases.
42
+
{% include notice icon="note" content="The vast majority of the functionality in ImageJ/Fiji's menus is macro-recordable. Occasionally, some commands will not be recorded, or not recorded correctly. Please refer to the documentation and [image.sc](https://forum.image.sc/) in such cases." %}
45
43
46
-
##1.2 Perform a simple workflow
44
+
###Perform a simple workflow
47
45
48
46
Perform a series of commands that you would like to automate with a macro.
{% include img src="screenshot-workflow-output.PNG" alt="Workflow output" %}
75
73
76
-
#2. Edit the Output from the Macro Recorder
74
+
##Edit the Output from the Macro Recorder
77
75
78
76
It's possible to edit commands directly within the Macro Recorder, but it's probably easier to use the [Script Editor](/scripting/script-editor). You can launch the Script Editor directly from the Macro Recorder by clicking the `Create` button.
{% include img src="screenshot-macro-recorder-create.PNG" alt="Macro Recorder create button" %}
81
79
82
-
##2.1 Save your macro and run it
80
+
###Save your macro and run it
83
81
84
-
Give your macro a sensible name and save it by going to `File > Save As...`in the Script Editor. Now try running your macro by selecting `Run > Run` from the menu. Your macro should produce the same output as the series of commands you recorded earlier. The code in the image above is reproduced below should you wish to copy it:
82
+
Give your macro a sensible name and save it by going to {% include bc path="File|Save As..." %} in the Script Editor. Now try running your macro by selecting {% include bc path="Run|Run" %} from the menu. Your macro should produce the same output as the series of commands you recorded earlier. The code in the image above is reproduced below should you wish to copy it:
The obvious problem with the macro in its current form is that it will only ever work on the image that was loaded when the commands that form the basis of the macro were originally recorded. Modifying the first two lines is the first step in "generalising" the macro, so that it runs on _any_ image:
101
99
```javascript
@@ -117,11 +115,11 @@ There are three changes above:
117
115
118
116
While this macro will now run on any image, it only allows us to process one image at a time, which is not ideal!
119
117
120
-
#3. Create a Loop to Run on Multiple Images
118
+
##Create a Loop to Run on Multiple Images
121
119
122
120
The macro we have so far works on one image at a time, by asking the user to select it as input. To automate the analysis on multiple images in a folder, we will have to setup a `for` loop.
123
121
124
-
##3.1 Enclose code within a `for` loop
122
+
###Enclose code within a `for` loop
125
123
We can run our code multiple times, to process multiple images, by enclosing it in a `for` loop:
126
124
127
125
```javascript
@@ -146,7 +144,7 @@ However, there are a number of problems with the above code:
146
144
147
145
Let's deal with each of these one at a time.
148
146
149
-
##3.2 Obtain an input directory
147
+
###Obtain an input directory
150
148
151
149
Let's add some code before the `for` loop to get an input directory and obtain a list of files from that input directory. We can also move the line of code specifying the output directory here so it doesn't get called every time the loop is executed:
152
150
@@ -161,10 +159,9 @@ Now we need to update the command that runs the Bio-Formats Importer, such that
> The `+` sign allows for concatenating strings, while using the `File.separator()` command takes care of differences between operating systems in conventional file separator characters. Enclosing a folder path or file name with squared brackets `[...]` ensures that they are read as a single string even in the presence of spaces.
162
+
{% include notice icon="tip" content="The `+` sign allows for concatenating strings, while using the `File.separator()` command takes care of differences between operating systems in conventional file separator characters. Enclosing a folder path or file name with squared brackets `[...]` ensures that they are read as a single string even in the presence of spaces." %}
166
163
167
-
##3.3 Close windows when we're done with them
164
+
###Close windows when we're done with them
168
165
169
166
The macro in its current form will open four windows every time the `for` loop is executed (assuming the input images have four channels). Multiply this by the number of times the loop gets executed (currently 10) and that's a lot of windows. We can deal with this by adding a `close` statement to the end of the code block within the `for` loop. Using a wildcard character (`*`) with the `close` statement instructs ImageJ to close _all_ image windows:
170
167
```javascript
@@ -191,17 +188,17 @@ for (i = 0; i < 10; i++) {
191
188
```
192
189
...and should now produce some meaningful output when run:
{% include img src="screenshot-particle-analyzer-summary-output.PNG" alt="Particle Analyzer summary output" %}
195
192
196
-
##3.3 Run the loop for the required number of times
193
+
###Run the loop for the required number of times
197
194
198
195
At present, the code within the `for` loop will always be executed exactly 10 times, regardless of how many images there are in the input directory. We can change this behaviour for placing something more meaningful in the conditional statement `i < 10`, such as:
199
196
```javascript
200
197
for (i =0; i <lengthOf(images); i++) {
201
198
```
202
199
Here, the `lengthOf` command returns the length of the `images` array, so the `for` loop will continue to be executed until all images in the array have been analysed.
203
200
204
-
## 3.4 Change the name of the output image
201
+
### Change the name of the output image
205
202
206
203
Finally, in order to have a fully functional (if rudimentary) macro, we need the name of the segmentation output image to be updated on each iteration of the `for` loop - at present, an image with the name `segmentation_output.png` is repeatedly overwritten. We could modify the `saveAs` statement to include the current value of `i` in the filename as follows:
207
204
```javascript
@@ -239,7 +236,7 @@ for (i = 0; i < lengthOf(images); i++) {
239
236
close("*");
240
237
}
241
238
```
242
-
# 4. Add Some Progress Updates
239
+
## Add Some Progress Updates
243
240
244
241
It's generally a good idea to keep the user informed of progress when code is running. We can do this by adding `print` statements at different points in the macro, so updates get printed to the Log window:
245
242
```javascript
@@ -272,9 +269,9 @@ setBatchMode(false);
272
269
```
273
270
The `setBatchMode` statements cause ImageJ to enter, then exit, "Batch Mode", which suppresses image windows. This allows the macro to execute faster.
274
271
275
-
# 5. Add Comments
272
+
## Add Comments
276
273
277
-
Adding comments to the macro will improve reusability both by others and our future selves. You can use the `//` sign before a line to add a comment in a Fiji macro: this will ensure the line is not executed. The macro with added comments would look like this:
274
+
Adding comments to the macro will improve reusability both by others and our future selves. You can use the `//` sign before a line to add a comment in an ImageJ macro: this will ensure the line is not executed. The macro with added comments would look like this:
278
275
279
276
```javascript
280
277
// Ask user for input directory and obtain file list
@@ -332,11 +329,11 @@ print("\\Update:100% of images processed.");
332
329
setBatchMode(false);
333
330
```
334
331
335
-
#6. Create a Dialog to Obtain User Input
332
+
## Create a Dialog to Obtain User Input
336
333
337
334
As an alternative to the `getDirectory` statements used above, it is possible to create a more functional, self-contained dialog to receive input from the user.
338
335
339
-
##6.1 Specify inputs and outputs
336
+
### Specify inputs and outputs
340
337
341
338
We can create and customise a [Generic Dialog](/scripting/generic-dialog) to obtain a variety of different inputs from the user. We can also use thisinterface to provide instructions to the user. Let's begin with a simple dialog that prompts the user to specify input and output directories:
342
339
@@ -363,9 +360,9 @@ The code above does three things:
363
360
364
361
Running the macro now should produce the following dialog:
{% include img src="screenshot-macro-dialog-1.png" alt="Macro simple dialog" %}
367
364
368
-
## 6.2 Modifying parameters via a dialog
365
+
### Modifying parameters via a dialog
369
366
370
367
In addition to specifying input and output directories, there are a range of other controls that can be added to a dialog. For example, we can add fields allowing the user to specify...
> For a full list of controls that can be added to a Dialog, see the [relevant macro language documentation](https://wsr.imagej.net/developer/macro/functions.html#dialog).
398
+
399
+
{% include notice icon="tip" content="For a full list of controls that can be added to a Dialog, see the [relevant macro language documentation](https://imagej.net/ij/developer/macro/functions.html#dialog)." %}
403
400
404
401
Running the macro will now produce a dialog that looks like this:
{% include img src="screenshot-macro-dialog-2.png" alt="Macro advanced dialog" %}
407
404
408
405
In order for the variables captured from the dialog to have any effect, we must modify the remainder of the code, placing the variables where they are needed.
409
406
@@ -541,8 +538,7 @@ print("\\Update:100% of images processed.");
541
538
setBatchMode(false);
542
539
```
543
540
544
-
545
-
# 7. Installing the Macro
541
+
## Installing the Macro
546
542
547
543
It is possible to ["install" macros in ImageJ](/scripting/macro#installing-macros), such that they appear on the Plugins menu. While this is not necessary to run a macro, which can always be opened and executed as is, this can be a good idea if you need to run the script regularly. In order to do so, we first need to wrap our macro in `macro` blocks. With the `macro` blocks added, our complete macro now looks as follows:
Find the _scripts_ folder within your ImageJ/Fiji installation and save your macro within the _Plugins_ subdirectory. You should now see your macro appear at the bottom of the Plugins menu when you restart the application:
0 commit comments