Skip to content

Commit de1956c

Browse files
committed
Clean up step-by-step batch processing tutorial
* 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.
1 parent c23c16b commit de1956c

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

_pages/tutorials/batch-processing-with-ij-macro.md

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ project: /software/imagej
55

66
{% include notice glyph="🎓" content="See also the [Introduction into Macro Programming](/scripting/macro) and [Batch Processing](/scripting/batch) articles." %}
77

8-
# Introduction
8+
## Introduction
99

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:
1111

1212
1. [The macro recorder](/scripting/macro#the-recorder)
1313
2. [Batch Processing](/scripting/batch#option-1---process--batch--macro)
@@ -16,7 +16,7 @@ One of the great strengths of Fiji is its ability to automate workflows. If you
1616

1717
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.
1818

19-
## Overview of this tutorial
19+
### Overview of this tutorial
2020

2121
This tutorial demonstrates how to
2222
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
2525
4. Add some progress updates
2626
5. Add a dialog so that a user can modify the parameters to the macro prior to execution
2727

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:
3029

31-
![IDR0028 LM2_siGENOME_1A Well C3 Field 10](/media/tutorials/IDR0028-LM2_siGENOME_1A_Well_C3_Field_10.png)
30+
{% include img src="IDR0028-LM2_siGENOME_1A_Well_C3_Field_10.png" alt="IDR0028 LM2_siGENOME_1A Well C3 Field 10" %}
3231

33-
# 1. Record Commands with the Macro Recorder
32+
## Record Commands with the Macro Recorder
3433

35-
## 1.1 Start the macro recorder
34+
### Start the macro recorder
3635

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" %}:
3837

39-
![Macro Recorder location on plugins menu](/media/tutorials/screenshot-plugins-macro-record.PNG)
38+
{% include img src="screenshot-plugins-macro-record.PNG" alt="Macro Recorder location on plugins menu" %}
4039

4140
Every command you now access through ImageJ's menu will be recorded as a line of text in the macro recorder.
4241

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." %}
4543

46-
## 1.2 Perform a simple workflow
44+
### Perform a simple workflow
4745

4846
Perform a series of commands that you would like to automate with a macro.
4947

50-
![ImageJ Macro Recorder](/media/tutorials/screenshot-macro-recorder-with-commands.PNG)
48+
{% include img src="screenshot-macro-recorder-with-commands.PNG" alt="ImageJ Macro Recorder" %}
5149

5250
The commands recorded below resulted from:
53-
1. Opening of an image with [Bio-Formats](https://www.openmicroscopy.org/bio-formats/), splitting channels into separate windows.
51+
1. Opening of an image with [Bio-Formats](/formats/bio-formats), splitting channels into separate windows.
5452
2. Selecting the first channel containing the nuclei
5553

56-
![Raw image](/media/tutorials/00raw.png)
54+
{% include img src="00raw.png" alt="Raw image" %}
5755

5856
3. Applying a Gaussian blur
5957

60-
![Gaussian blur](/media/tutorials/01gaussian_blur.png)
58+
{% include img src="01gaussian_blur.png" alt="Gaussian blur" %}
6159

6260
4. Thresholding the image with the default method
6361

64-
![Threshold](/media/tutorials/02threshold.png)
62+
{% include img src="02threshold.png" alt="Threshold" %}
6563

6664
5. Using the Watershed algorithm to separate adjacent objects
6765

68-
![Watershed](/media/tutorials/03watershed.png)
66+
{% include img src="03watershed.png" alt="Watershed" %}
6967

7068
6. Generating a particle count using the `summarize` option in the [Analyze Particles](/ij/docs/guide/146-30.html#sub:Analyze-Particles...) tool.
7169

7270
The result of the commands recorded above should look like this:
7371

74-
![Workflow output](/media/tutorials/screenshot-workflow-output.PNG)
72+
{% include img src="screenshot-workflow-output.PNG" alt="Workflow output" %}
7573

76-
# 2. Edit the Output from the Macro Recorder
74+
## Edit the Output from the Macro Recorder
7775

7876
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.
7977

80-
![Macro Recorder create button](/media/tutorials/screenshot-macro-recorder-create.PNG)
78+
{% include img src="screenshot-macro-recorder-create.PNG" alt="Macro Recorder create button" %}
8179

82-
## 2.1 Save your macro and run it
80+
### Save your macro and run it
8381

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:
8583
```javascript
8684
run("Bio-Formats Importer", "open=[C:/Users/barryd/Downloads/FrancisCrickInstitute-introduction-to-image-analysis-be5d061 (1)/FrancisCrickInstitute-introduction-to-image-analysis-be5d061/Data/idr0028/003003-10.tif] autoscale color_mode=Default rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");
8785
selectImage("003003-10.tif - C=0");
@@ -95,7 +93,7 @@ run("Analyze Particles...", "exclude summarize");
9593
saveAs("PNG", "C:/Users/barryd/Downloads/FrancisCrickInstitute-introduction-to-image-analysis-be5d061 (1)/FrancisCrickInstitute-introduction-to-image-analysis-be5d061/Data/segmentation_masks/003003-10.tif - C=0.png");
9694
```
9795

98-
## 2.2 Generalise your macro
96+
### Generalise your macro
9997

10098
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:
10199
```javascript
@@ -117,11 +115,11 @@ There are three changes above:
117115

118116
While this macro will now run on any image, it only allows us to process one image at a time, which is not ideal!
119117

120-
# 3. Create a Loop to Run on Multiple Images
118+
## Create a Loop to Run on Multiple Images
121119

122120
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.
123121

124-
## 3.1 Enclose code within a `for` loop
122+
### Enclose code within a `for` loop
125123
We can run our code multiple times, to process multiple images, by enclosing it in a `for` loop:
126124

127125
```javascript
@@ -146,7 +144,7 @@ However, there are a number of problems with the above code:
146144

147145
Let's deal with each of these one at a time.
148146

149-
## 3.2 Obtain an input directory
147+
### Obtain an input directory
150148

151149
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:
152150

@@ -161,10 +159,9 @@ Now we need to update the command that runs the Bio-Formats Importer, such that
161159
run("Bio-Formats Importer", "open=[" + inputDir + File.separator() + images[i] + "] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
162160
```
163161

164-
> [!TIP]
165-
> 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." %}
166163

167-
## 3.3 Close windows when we're done with them
164+
### Close windows when we're done with them
168165

169166
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:
170167
```javascript
@@ -191,17 +188,17 @@ for (i = 0; i < 10; i++) {
191188
```
192189
...and should now produce some meaningful output when run:
193190

194-
![Particle Analyzer summary output](/media/tutorials/screenshot-particle-analyzer-summary-output.PNG)
191+
{% include img src="screenshot-particle-analyzer-summary-output.PNG" alt="Particle Analyzer summary output" %}
195192

196-
## 3.3 Run the loop for the required number of times
193+
### Run the loop for the required number of times
197194

198195
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:
199196
```javascript
200197
for (i = 0; i < lengthOf(images); i++) {
201198
```
202199
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.
203200
204-
## 3.4 Change the name of the output image
201+
### Change the name of the output image
205202
206203
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:
207204
```javascript
@@ -239,7 +236,7 @@ for (i = 0; i < lengthOf(images); i++) {
239236
close("*");
240237
}
241238
```
242-
# 4. Add Some Progress Updates
239+
## Add Some Progress Updates
243240
244241
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:
245242
```javascript
@@ -272,9 +269,9 @@ setBatchMode(false);
272269
```
273270
The `setBatchMode` statements cause ImageJ to enter, then exit, "Batch Mode", which suppresses image windows. This allows the macro to execute faster.
274271
275-
# 5. Add Comments
272+
## Add Comments
276273
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:
278275

279276
```javascript
280277
// Ask user for input directory and obtain file list
@@ -332,11 +329,11 @@ print("\\Update:100% of images processed.");
332329
setBatchMode(false);
333330
```
334331

335-
# 6. Create a Dialog to Obtain User Input
332+
## Create a Dialog to Obtain User Input
336333

337334
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.
338335

339-
## 6.1 Specify inputs and outputs
336+
### Specify inputs and outputs
340337

341338
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 this interface to provide instructions to the user. Let's begin with a simple dialog that prompts the user to specify input and output directories:
342339
@@ -363,9 +360,9 @@ The code above does three things:
363360
364361
Running the macro now should produce the following dialog:
365362
366-
![Macro simple dialog](/media/tutorials/screenshot-macro-dialog-1.png)
363+
{% include img src="screenshot-macro-dialog-1.png" alt="Macro simple dialog" %}
367364
368-
## 6.2 Modifying parameters via a dialog
365+
### Modifying parameters via a dialog
369366
370367
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...
371368
1. a filter radius for Gaussian smoothing
@@ -398,12 +395,12 @@ nucleiIndex = Dialog.getNumber();
398395
gaussRad = Dialog.getNumber();
399396
thresholdMethod = Dialog.getChoice();
400397
```
401-
> [!TIP]
402-
> 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)." %}
403400
404401
Running the macro will now produce a dialog that looks like this:
405402
406-
![Macro advanced dialog](/media/tutorials/screenshot-macro-dialog-2.png)
403+
{% include img src="screenshot-macro-dialog-2.png" alt="Macro advanced dialog" %}
407404
408405
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.
409406
@@ -541,8 +538,7 @@ print("\\Update:100% of images processed.");
541538
setBatchMode(false);
542539
```
543540
544-
545-
# 7. Installing the Macro
541+
## Installing the Macro
546542
547543
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:
548544
```javascript
@@ -625,9 +621,9 @@ macro "Batch Nuclei Counter" {
625621
```
626622
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:
627623
628-
![Macro dialog](/media/tutorials/screenshot-plugins-installed-macro.PNG)
624+
{% include img src="screenshot-plugins-installed-macro.PNG" alt="Macro dialog" %}
629625
630-
# See also
626+
## See also
631627
- [Batch Processing](/batch)
632628
- [Scripting](/scripting) documentation and tutorials.
633629
- [How to apply a common operation to a complete directory](/tutorials/apply-operation-to-a-complete-directory)

0 commit comments

Comments
 (0)