Navigating Multidimensional Datasets with Simple Macros

Replication is key to the Scientific method. This is especially true when dealing with imaging, when a single field (at any magnification) is rarely representative of the entire experiment, let alone the underlying phenomenon as a whole. The easy solution to this is to take many fields of many samples, but visualising and manipulating a whole dataset can be a time consuming endeavour. In this post, we’re going to cover some of the tricks for working with a dataset in Fiji and take the first steps into the big wide world of scripting.

Problems and Solutions

If you open a whole bunch of images in Fiji, you will usually end up with something like this:

2016-03-DataSets_001

One way to quickly tidy this up is to use [ Window > Tile ] which fills the screen so that you can see all of your image windows.

2016-03-DataSets_002

Much better! These however, are multichannel images (each has 2 fluorescent and 1 transmitted light channels). In order to compare across conditions or between images in a dataset, it’s helpful to change the brightness and contrast, but of course we would have to do this the same on each image which is a time consuming proposition.

You would think Fiji would have an option to change all of the intensities of all of the open images at the same time and it does…sort of.

If you run [ Image > Adjust > Brightness & Contrast ] you get the familiar dialog where you can set the white and black points of an image. A useful option is available if you hit the ‘Set’ button where you can specify these values.

2016-03-DataSets_003

Handily, there are checkboxes here that let you propagate the values to other open images and the other channels of the same image. Unfortunately, the former option doesn’t seem to work, so you will have to repeat the process for each channel (which is probably no bad thing anyway).

2016-03-DataSets_004

Channel Histograms after setting the white and black points on the green channel to 0-2000 and checking the propagate checkboxes.

Changing the Channel

Intensity changes are fine if you only want to view the original channel but if you want to skip over to the second or third channel you’ve a lot of clicking ahead of you, unless there’s a way to apply a command to every open image. Huzzah for macros!

If this is your first venture into macros and scripting then don’t panic. We’re going to stick to the basics. At their simplest, macros (or scripts) are just a list of instructions that the program follows one by one.

To access the script editor run [ Plugins > New > Macro ].

2016-03-DataSets_005

If you’re following along, you can copy the following code into your script editor.

for (i=0;i<nImages;i++){
    selectImage(i+1);
    Stack.setChannel(2);
}

If you open some multichannel images then run this script (somewhat predictably using the ‘Run’ button), it will change to the second channel on every open image. To understand what’s going on, let’s break this down one line at a time.

QUICK NOTE: in the ImageJ macro language all commands need to end with a semicolon!

The first thing we’re doing here is setting up a loop. In the example above, the for loop uses a variable (we’ve chosen the letter ‘i‘ but it can be almost any letter or word) to run through from a starting value (i=0), as long as a condition is true (i<nImages – here nImages is a dynamic variable that holds the number of open images at any time). The loop progresses by taking steps of a certain size (i++ which just means add 1 each time).

Everything in the {curly braces} is what the loop does on each iteration. The first line selects an image (all open images can be addressed with an integer value from 1 to nImages) and the second line selects the second channel of that image.

These two lines (selectImage and Stack.setChannel) are Built-In Functions of Fiji (or technically of ImageJ) and there is a function for almost every command you could want to run.

Let’s add in the brightness and contrast with which we started the post. Add the following line after the setChannel command:

setMinAndMax(0, 2000);

This will now scale the images to the same intensity range. What about downsampling images from 16 to 8 bit? Easy, just add

run("8-bit");

Want to change the look-up table to green?

run("Green");

The best way to find out these commands is to open the macro recorder [ Plugins > Macros > Record ], then perform your actions using the commands on the menu. The macro versions of the commands will be shown in the recorder window.

2016-03-DataSets_006

From here you can either copy the text out into the script editor or hit ‘Create’ to make a new script.

Epilogue

This is the very tip of the tip of the iceberg for macro scripting. Just about anything you can do with the menus can be scripted and you can do some quite complex user-interaction and image/data processing. Below are some great references to get started but without a doubt the best way is to find a guru or just try things out. Get coding!

ImageJ Programming Language

ImageJ Built-In Functions

Advertisements

Comment!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.