Correcting the record

Unless your imaging facility is in a clean room (and you never touch it), from time to time, we all end up with unsightly splotches on our transmitted light images. The best fix for this is to clean the microscope but sometimes you just have to do what you can with what you’ve got.

Thankfully there’s a fairly easy way to correct it post-acquisition. Let’s Flat-field correct!

The Theory

OK, let’s say we have our image and it looks something like this:

It looks like there is some smudge or dirt in the light path which is casting a shadow on the transmitted detector.

To understand the theory of what we’re doing, it’s important to think of the image as having two components: the stuff we want and the stuff we don’t want.

A simple geometric example

Below we’re trying to image our lovely squares but quelle horreur! some dastardly circles have crept into the field. They must be cowering somewhere in the microscope’s lightpath.

If we remove the sample, (our squares) and just take a picture of everything else, all we will see are the circles.

To get rid of the unwanted circles we can divide (pixel by pixel) the sample image by the background image. This will reduce or remove features common to both images and emphasise unique features.

Enough of Squares and Circles already

OK, let’s get back to a (sort of) real-world example. In order to get an image with which to correct, get your acquisition settings right with the sample, take a picture (below left) then remove the sample from the stage completely. Without changing anything, take another picture (below right):

Now, open both images in Fiji, and run [ Process > Image Calculator ]. Divide one image by the other and make sure both check boxes are checked.

The result will be created in a new window and should look something like this:Ta da! By dividing the two images, we’ve removed anything that is common to both images (IE the big shadow, but did you also notice the small black spot in the top left?) leaving just the sample.

Think of the numbers!

In producing the image above, we’ve change the data type of our image. By checking the “32-bit” box, we’ve converted the original from a 16-bit to a 32-bit image. If we keep the image as 16-bit the result would look like this:

There are only two pixel values here: zero and one. The reason for this is that division of the pixel values creates fractional decimal numbers, and a 16 bit image can only display integer values from 0 to 65565. As a result the decimals get rounded and end up as zero or one.

Contrary to intuition, a 32 bit image does not stick to the usual rule of having 2^n grey levels (you were paying attention during the Bit Depth lesson right?). Instead it produces a floating point number.

A floating point whatnow?

Time for a quick aside into the fascinating world of floating point number systems. Anyone familiar with Scientific notation will basically have the gist and may want to skip this part.

Most people are familiar with fixed-point notation. So if I want to express three constants: pi, the Planck length and Avogadro’s number they look like this:




Rather ungainly but importantly, the decimal place is fixed. The idea of floating point notation is to represent a number by giving a significand (the number part) and a multiplier (called an exponent) where the decimal place can be moved about freely.

By picking a base for the exponent (in this case 10) the numbers above can thus represented with significand (S) and exponents (E) as:

S = 3.14159 E = 0

S = 6.62606979 E = -34

S = 6.022140857 E = +23

…and you can calculate the number by multiplying your significand by the base raised to the exponent. Using pi as an example:

significand * base ^ exponent

3.14159 * 10 ^ 0 = 3.14159

When we use base 10 (as above) and there is exactly one digit to the left of the radix point we call this Scientific notation. This doesn’t have to be the case as a further for example we can correctly express pi as any of the following floating point numbers:

S = 3.14159 E = 0

S = 0.000314159 E = 4

S = 314159 E = -5

So what does this mean for an image?

Basically nothing. Fiji can display and manipulate floating point numbers just fine, however if you want to combine the image with others, say some other channels from the original stack. You will have to convert the image back into its original bit depth.

You can do this very easily by running [ Image > Type > 16-bit ] or [ Image > Type > 8-bit ]. The pixel values will be scaled to the correct integer value based on the range of the image.

Going faster

If that all sounds good but you just don’t have the time, of course these steps can be automated with the wonder of scripting:

//@ File (label = "Image File", description="Image file, can be single image or stack.") imFile
//@ File (label = "FF Image", description="Image file, can be single image or stack.") ffFile
//@ String (label = "Transmitted Channel", value = "4") tChan

//-- Open both files and record names
//-- catch if FF image has more than one channel
if (nSlices()>1){Stack.setDisplayMode("grayscale");Stack.setChannel(tChan);}

if (nSlices()>1){Stack.setDisplayMode("grayscale");Stack.setChannel(tChan);}

//-- create the FF corrected transmitted channel
imageCalculator("Divide create 32-bit", imgTitle,ffTitle);
selectWindow("Result of "+imgTitle);

//-- match original bit depth

//-- Copy the adjusted image back into the original stack
run("Select None");

//-- Reset brightness and contrast and save
close("Result of "+imgTitle);
saveAs("Tiff", imgDir+File.separator+imgName+"_ff.tif");

You can find this code in my online repo with the example data used in this post.

Not familiar with git and repos? You may want to checkout this post.


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

You are commenting using your 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.