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.
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:
Image > Duplicate ...
Process > Filters > Gaussian Blur...
Process > Find Maxima...
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");
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.
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.
File > Open Samples > Blobs
Plugins > Macros > Record...
Process > Filters > Gaussian Blur ...
Ok
Process > Find Maxima
Light background
Ok
Analyze > Measure
Now either copy and paste the code recorded in the macro window multiple times to end up with, e.g.
run("Gaussian Blur...", "sigma=1");
run("Find Maxima...", "noise=10 output=[Point Selection] light");
run("Measure");
run("Gaussian Blur...", "sigma=2");
run("Find Maxima...", "noise=10 output=[Point Selection] light");
run("Measure");
run("Gaussian Blur...", "sigma=3");
run("Find Maxima...", "noise=10 output=[Point Selection] light");
run("Measure");
run("Gaussian Blur...", "sigma=4");
run("Find Maxima...", "noise=10 output=[Point Selection] light");
run("Measure");
Extension
If you’re happy with trying Macro scripting, you can try
the for
loop syntax:
for(scale=1; scale <= 4; scale = scale + 1){
run("Gaussian Blur...", "sigma="+scale);
run("Find Maxima...", "noise=10 output=[Point Selection] light");
run("Measure");
}
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...
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.