Most processes in ImageJ can be recorded, enabling the fast and simple construction of code to automate analysis or image processing pipelines.
Let's try automating some of the processes we've already covered as an example.
Exercise 1: Using the Recorder
Exercise 2: Building a script with the Recorder
Here we're going to try automate the construction of our figure from Page 2.
The only two parts of the process which required manual involvement were the adjustment of the image brightness and the selection of an ROI to crop the image.
It is possible to build manual intervention steps into a macro, but for simplicity we'll skip that step for now.
These were the steps. You can type the underlined commands into the search bar to find them again. Try to build a script to replicate this process:
Your code should look something like this:
run("Z Project...", "projection=[Max Intensity] all");
run("Duplicate...", "duplicate frames=25-28");
run("RGB Color", "frames");
run("Stack to Images");
selectWindow("MAX_mitosis-1-0001");
run("Scale Bar...", "width=5 height=7 thickness=2 font=14 color=White background=None location=[Lower Right] horizontal bold hide overlay");
run("Flatten");run("Mitosis (5D stack)");
Being a human, it can be difficult to keep track of what all the steps of a program are for. Particularly if they have been written by someone else!
It's good practice to write comments throughout the code, by preceedng text with //
to annotate it.
You'll thank yourself later. E.g.
run("Mitosis (5D stack)"); //Open mitosis image
run("Z Project...", "projection=[Max Intensity] all"); //Maximum intensity projection to remove z-dimension
run("Duplicate...", "duplicate frames=25-28"); //Duplicate only frames 25-28 of the image
run("RGB Color", "frames"); //Convert image to RGB
run("Stack to Images"); //Separate stack to individual RGB images for each timeframe
selectWindow("MAX_mitosis-1-0001"); //Select the first image separated from the stack
run("Scale Bar...", "width=5 height=7 thickness=2 font=14 color=White background=None location=[Lower Right] horizontal bold hide overlay"); //Add scale bar
run("Flatten"); //Stamp scale bar on image//This script will open the stock image "mitosis (5D stack)" and process it to make images for a figure
Note: You can also select a block of text and press Ctrl+/ to comment or remove comment status. This is very useful if you want to temporarily remove a step in your pipeline
The script is working, but there are loads of other windows still open. This is messy and confusing.
When working manually you can close individual windows with the shotcut Ctrl+W or close all open windows with Ctrl+Shift+W.
To tidy up our script we can close the unwanted windows by adding close("mitosis.tif") ;close("MAX_mitosis.tif") ;close("MAX_mitosis-1-0001") ; //close unwanted images
Easy isn't it?
Before going any further let's think about the basics of what is in your script:
Generally ImageJ deals with thee main data types:
"Hello World!";
into the script editor and run+
, e.g. "Hello" + " World!";
b=" World!";
c=a+b;
print(c);a="Hello";
Note: Once assigned to a variable, strings and numbers need to be printed out to view, using print()
Exercise 4: Putting string to work
getDirectory
. Type it into the script and look at the options which pop upgetDirectory(string)
getDirectory("Select a directory");
dir=getDirectory("Select a directory");
print(dir);
to check it's workedsaveAs("tiff", path);
, where "path" is a string of the addresssaveAs
function described, combining dir
to a filename of your choosing which must end ".tif" e.g. saveAs("tiff",