The macro recorder

The great thing about ImageJ macros is that you don't need any prior programming experience to code them. We will also be doing some programming in any case, but the important thing to note here is that non-technical users can create their own macros effortlessly.

Remember the example that ended the previous chapter and helped us in understanding how the particle analyzer is used. Imagine that you do the analysis, store the results, and forget about it. Six months from now we need to go back to the original data and repeat the analysis (for instance, because some journal reviewers asked us to change some parameter or recheck the original procedure). Chances are that we will get an approximate result, but not the exact one, as there are several parameters that need to be set and you might not remember them. Also, suppose you need to analyze not one but hundreds of images. The semi-automatic method we used is good enough for a few images, but it is still very slow for analyzing many of them. The ImageJ macro system can help us to replicate a large number of operations in a very easy way.

The easiest way of creating a new ImageJ macro is by simply recording all the steps a given analysis requires. Think of an operation in the ImageJ menu option as a command that is executed with the appropriate parameters. All these commands can be recorded and rerun again when needed.

Now we are going to repeat the analysis of the previous chapter but recording what we do. The first thing we need to do is start the macro recorder. This is done by navigating to the Plugins | Macros | Record... menu option. An empty recorder window will appear as shown in the following screenshot:

The macro recorder

This window will do nothing by itself. You can see that it is possible to type inside the textbox, but nothing happens. The purpose of the macro recorder is to keep track of every command run from the ImageJ menu options (or the equivalent shortcuts). Indeed, we can see that different commands are added to this window as we use different menu options. This is what we should have in this window after performing the analysis (this time, we used the Image | Color | Split channels option, so the first operation gives us three images and not a single stack with three slices):

run("Split Channels");
selectWindow("tuberculosis_full.tif (blue)");
close();
selectWindow("tuberculosis_full.tif (red)");
close();
selectWindow("tuberculosis_full.tif (green)");
run("Subtract Background...", "rolling=30");
setAutoThreshold("Moments dark");
run("Analyze Particles...", "size=5-70 circularity=0.00-0.90 show=[Overlay Masks] clear summarize");

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

We accomplished two things: we have run the desired analysis and at the same time we recorded everything we did. See that every action we took generated a command on the recorder. The command not only runs a given tool on the image but also keeps track of the particular parameters that were used at that time. We encourage you to run other commands on the image to see what the recorder does. There are a few commands that do not have an equivalent command line (for example, the Freehand selections tool, it will emit a line when it is selected, but will not record the actual contour being drawn). Feel free to explore all the options you can think about, from opening a file to filtering it, duplicating, renaming, resizing, converting its type, and so on.

Now you can save the macro with all the recorded commands. There are two ways of doing that, which are as follows:

  • From the recorder window, click on the Create button. A new window will appear with the contents of the recorder in it.
  • Navigate to Plugins | New | Macro from the main ImageJ window and copy and paste all or part of the contents of the recorder window. This option is useful when many commands have been run as a test and we only want to keep a few of them.

No matter what option you choose, you will end up with a text window that contains the commands just recorded. You can now save this file as an ImageJ macro to be run whenever you want.

Tip

If you create your macro by clicking on the Create button, ImageJ will suggest that you name the file as Macro.ijm (after ImageJMacro). Another method will suggest the name as Macro.txt. The file extension is irrelevant, you can select the full filename when you save it and use whatever extension you like, though .ijm is a bit more descriptive about the file contents and purpose.

It is a good coding practice to comment the code in order to understand what the macro is supposed to do, what input it should accept or any kind of information that may be useful to anyone running it. Comments start with a double slash, //. You can write whatever you want after those symbols and that will be ignored when the macro is run. If you need a comment that spans several lines, use // at the beginning of each one. Comments are only a human-readable explanation about what the code does when you open it after several months from now. So, use them to explain whatever you think a user may need to know when using your macro.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset