Recording macros & batch processing

Imagej has a macro system to allow the user to rapidly repeat a set of commands without having to execute each one individually.

For example, you may want to automate counting “blob”-like objects in images, which requires that you blur the image, then find maxima, and finally count the number of peaks.

Instead of running all three operations on each input image, the easiest way to create a macro is to let imagej record it when you perform the sequence of actions once.

There is a separate course on ImageJ Macros, but in the following section we’ll look at creating macros via the User Interface.

Even without looking at the macro code at all, Recording Macros is a good way of keeping track of the processing that you apply to data.

Recording macros

First open an image that you will process. This operation will not be included in the macro at this point.

Next press Plugins > Macros... > Record ... to open up the macro recording dialog (“Recorder”).

The recording has begun; any subsequent operations will appear in the Recorder dialog.

As an example after having opened Blobs from the samples (Hint: Use Ctrl+Shift+B), start the recorder.

Next perform the following operations:

  1. Image > Duplicate ...
  2. Set the title to “Blurred”
  3. Process > Filters > Gaussian Blur...
  4. Enter sigma = 5
  5. Press OK
  6. Process > Find Maxima...
  7. Check “Light Background”
  8. Press OK
  9. Analyze > Measure

The Results window will show various measurements for the detected peaks (there should be 59!).

The Recorder window should show

run("Duplicate...", "title=blurred");

run("Gaussian Blur...", "sigma=5");

run("Find Maxima...", "noise=10 output=[Point Selection] light");

run("Measure");

ImageJ toolbar

Recorder window

Original blobs image

Blurred image with peaks

Measurement results

which summarizes the operations we performed.

This macro can be saved using the Create button to save the macro with the name specified in the “Name” field. This will open a save file dialog. Confirm that the filename is one that makes sense to you and a usual location is the imagej/Macros folder, then go ahead and save the Macro.

The macro can be applied on any loaded image by calling

Plugins > Macro > Run...

and finding and selecting the macro we just saved.

Exercise 2.4

Use the above approach to apply the Gaussian filter to the blobs data set over a short range of blur radii. At each scale, find the peak locations.

Batch processing

Working with large data sets

In the previous section, we didn’t automate opening the file with the recorder because otherwise the macro would have recorded:

run("Blobs (25K)");

which is only relevant for that data set. Often we are working with many data sets, or a data set with many frames. In such cases, hard coding the filename, or slice number, is not useful as the macro would run the operations on the same file/frame each time it is run.

Probably the simplest option is to use imagej’s

Process > Batch > Macro...

functionality.

This allows one to select a source folder that contains a set of input files, and a source folder to store results.

In addition the edit box allows selection of the operations that are to be performed.

The same functionality for a loaded stack can be obtained by using

Process > Batch > Virtual Stack...

Exercise 2.5

Download a stack composed of multiple images from the ImageJ website such as some of the images in

http://imagej.nih.gov/ij/images/pet-series/

to a new folder (called, e.g. “Stack files”).

Next start the batch processor, set the input directory to the Location you downloaded the stack files to, and select a macro to perform on the series. Create a new directory called “output” and use that as the output directory for the batch operation.