Professional Documents
Culture Documents
Practical 3: Morphology
Learning Goals
After this practical you should know by heart how to:
Set the pixel convention for binary morphology in ImageJ.
Visualize the structuring elements used by ImageJ.
Apply binary dilation, erosion, opening, and closing.
Apply gray-scale dilation, erosion, opening, and closing.
Select the right binary operator to remove small objects.
Use morphological operators to get the largest object.
Create a selection of a binary object automatically.
Specify a custom selection in any given image.
Clear or invert the intensities of any given image.
Remove boundary objects and fill holes in a binary image.
Use the particle analyzer functionality of ImageJ.
Write a macro to fully automate object measurements.
1
Process > Binary, and run the command Options… (the last one in the list).
Select the option Black background and click OK.
Structuring Element
As we have learned in today’s lecture, the structuring element used by morphological
operators can in principle have any shape. However, the commands in the Process
> Binary menu all use only one particular structuring element.
In linear image processing problems (remember the previous two lessons) we can
find the convolution kernel of a filter by feeding it with an impulse. Similarly, we can
find the structuring element by applying a dilation operation to an impulse.
Question
Create a binary impulse image (2D) and run the command Process > Binary >
Dilate. What structuring element is used by ImageJ? Draw the pixel mask.
Answer
ImageJ uses a 3 x 3 structuring element (all pixels “on” with center pixel as origin):
A wider variety of structuring elements is available in the ImageJ command for gray-
scale mathematical morphology: Process > Morphology > Gray Morphology.
Since binary images are actually gray-scale images (but with only two gray values: 0
or 255) we can also use this command to perform binary mathematical morphology.
Question
Run the command Process > Morphology > Gray Morphology to perform dilation
on your binary impulse image using a circular structuring element with radius 1. What
does the structuring element look like (draw the mask)? Also answer the question for
circular structuring elements with radii 1.5 and 2.0 and 2.5 pixels.
2
Answer
Please note that this command does not generally yield a good binary result for any
image, but it does so for this particular image, because its intensity histogram is
nicely bimodal (check for yourself). The command uses an automatic thresholding
method named IsoData. In the next lesson (on segmentation) you will learn much
more about different automatic thresholding methods, including this one. For now, it
is important to remember that it is just one specific thresholding method, and you
should use it with care (only when the histogram is bimodal).
3
The behavior of the basic morphological operators Erode, Dilate, Open, and Close
from the Process > Binary menu is determined by the settings in the window that is
opened by running the command Process > Binary > Options…
Let’s explore the effects of the different operators. But instead of running the
commands Erode, Dilate, Open, and Close every time, we can also experiment with
them using the Preview option of the Binary Options window. Run the command to
open that window (make sure it is applied to the binary image and not the original
gray-scale image) and start with the same settings as shown above.
Select Erode from the Do choice list in the window and look at its effect by switching
the Preview option on and off a few times. Repeat this for Dilate, Open, and Close.
The number specified in the Iterations field of the window determines how many
times the operator is applied.
Question
Which operator and how many iterations of this operator are needed to remove the
smallest object (the tiny noise spot close to the center of the image) while preserving
the morphology of the other objects (the real cells) as best as possible? And why do
we need this many iterations and not less (or more)?
Answer
Operator: Open
4
Iterations: 3
Why: The right choice in this case is the Open operator because it first erodes
objects pixels and then dilates the remaining objects more or less back to their
original morphology. Erode by itself only removes object pixels, Dilate only adds
object pixels, and Close first dilates and then erodes objects so that does not result
in a removal of small objects. The value of the Iterations parameter determines how
many erosions are first applied, followed by the same number of dilations. In this
case we need 3 iterations, because the “radius” of the spot is about 3 pixels, so 1 or
2 erosions (each of them “eating away” only one layer of pixels) are not sufficient to
remove it. And we should not use more than 3 iterations, because the Open operator
does change the morphology of the remaining objects a little bit, which gets worse
and worse as the number of iterations increases.
Question
Binarize the image and repeat the previous exercise to find out which operator and
how many iterations are needed to keep only the soma and nothing else.
Answer
Operator: Open
Iterations: 8
Why: The same argumentation as in the previous exercise. Now we need more
iterations because the diameters of the thickest dendrites (to be eroded away) are
larger. Using a few more iterations does not make much difference in this case.
Once you have found the optimal settings you can click the OK button of the Binary
Options window to perform the operation and get the processed image.
5
As you can see, the resulting binary image is quite blocky. This is because of the
square structuring element used by the Binary commands of ImageJ.
An alternative approach to get the soma of the neuron in the given image is to apply
gray-scale morphology first and then do the binarization afterwards.
Question
Reopen the original gray-scale image and apply the ImageJ command for gray-scale
mathematical morphology (see above). Select the circle as the structuring element.
Which operator and which radius are needed to keep only the soma and nothing else
after binarization of the processed image?
Answer
Radius: 12 yields a fairly good result. Smaller radii leave additional structures or
blobs in places where dendrites sprout from the soma. Further increasing the radius
would yield even smoother results but it does not work with the standard binarization
command: too many structures are then labeled as object. As explained above, this
command makes certain assumptions about the intensity statistics in an image and
automatically selects a threshold based on this, but apparently when using larger
radii the statistics change so much that these assumptions are no longer valid. A
different binarization command would be needed.
Comparing the results of the previous exercise (image on the left) and the current
exercise (image on the right) we see that the soma in the latter is smoother:
6
In previous practicals you have learned how to measure various properties of objects
in images using the Analyze > Measure command. However, if this command is run
without a selection (an object outline), it will measure the entire image.
Question
What is the area and perimeter (in pixels) of the soma in Single Neuron.tif obtained
using the binary morphology versus the gray-scale morphology approach?
Answer
As expected, the area and perimeter obtained with the gray-scale morphology
approach are smaller, because the soma contour is smoother there.
Make a duplicate of the image so that we can use the duplicate to test things while
keeping the original as a visual reference. Then make a binary image of the duplicate
(use the same command as before) to see if we get a nice separation of pixels into
object (white) versus background (black).
As you can see, this doesn’t really work very well: the result is rather noisy (the cells
contain many black pixels, the cell boundaries are not smooth, and the background
contains some white pixels). Undo the binarization using Edit > Undo.
7
The image contains too much noise to start analyzing things directly. In such
situations it is very helpful to first make the image a bit smoother by using Gaussian
blurring. Run Process > Filters > Gaussian Blur… with a Sigma of 2 pixels (that
means do not use the Scaled Units option). Make a binary image of the result.
Notice that some of the cells (the ones at the boundary) partly fall outside the image.
Since it does not make sense to measure their size, they should be excluded from
the binary image. As discussed in today’s lecture this can be done by applying binary
morphological reconstruction using the boundary pixels as seed points.
We are now going to implement this operation. But first make sure the settings of the
Binary Options window are put back to their initial values (as shown before). Most
importantly, the number of Iterations must be 1 again, as this setting is also used by
Process > Binary > Dilate (and other commands).
If everything went well, the Boundary image should now be black (background
value) everywhere except for the boundary pixels, which should still have the same
values as in the original Objects image. If not, repeat the above steps correctly.
In the new macro window you can now simply click the Run button to repeat the
dilation operation followed by the AND operation to see the Boundary image grow.
Repeat this until there is no more change.
8
At this point the Boundary image contains all the boundary objects of the original
Objects image. Now use the Image Calculator to subtract Boundary from Objects
so that the resulting image contains only the cells that fall entirely within the image.
The only thing that is not so nice about the above approach is that you have to
manually click the Run button of the macro window many times and you need to
visually check whether there is no more change. It would be much better if we could
automate all of this, so that only one click of the Run button is needed.
Question
Extend the macro code so that if you Run the macro on the initial Boundary image it
will automatically repeat the dilation + AND operation again and again until there is
no more change in the image. Write down the resulting macro code below and also
save the macro using File > Save as… in the macro window (save it wherever you
want but use the .ijm extension of the file name).
Answer
The following macro reconstructs the boundary objects fully automatically (but
different implementations are possible that will give the same result):
9
// Algorithm:
oldmean = 0;
getStatistics(area,mean);
while(mean != oldmean) {
oldmean = mean;
run("Dilate");
imageCalculator("AND", "Boundary","Objects");
getStatistics(area,mean);
}
Question
What series of steps is needed to fill the holes in all objects of image Objects?
Hint: See today’s lecture. And you can reuse the macro you wrote above.
Answer
Apply these steps to fill the holes in Objects. Notice that inversion of intensities in an
image can be done using the command Edit > Invert.
10
pixel masks for doing measurements on the cells in the original gray-scale image
CHO PCNA-GFP 1.tif that we started with.
Run the command Analyze > Analyze Particles… to open the window shown below
and use the exact same settings. When you click OK, ImageJ will search for blobs of
connected pixels and put them as separate selections in the ROI Manager.
If you select the options Show All and Labels in the ROI Manager you will see the
labeled selections as an overlay in the image.
Activate the original gray-scale image CHO PCNA-GFP 1.tif and make the selections
visible in that image by clicking Show All again in the ROI Manager.
11
Question
What is the area and the mean fluorescence intensity in each cell nucleus? Compare
your measurements with those of your neighbor. Are there any differences?
Answer
There should not be any differences between your results and those of your
neighbors because you both used the exact same computations. The higher the level
of automation and standardization of methods and parameters, the better the
reproducibility of the measurements.
12
Fully Automated Measurements
In the previous exercises you have learned several useful image processing steps to
get biologically meaningful and fully reproducible measurements from a gray-scale
image without making any manual selections.
However, even though you wrote a macro to automate some of the steps, you still
needed to do quite a bit of manual work, such as picking commands from the ImageJ
menus and setting the right parameter values. Also, some of these parameter values
were specific for the given image.
Challenge
The macro should work also for other (but similar) images. That is, after loading CHO
PCNA-GFP 2.tif, and without changing the macro code, a single click of the Run
button should produce sensible measurements also for that image.
13
Defined Functions for more information on how to do this.
Do not try to write the entire macro script at once, but build it up step by step, and
test the intermediate versions to see if the code does what it should do.
Solution
The following macro fully automates the measurement process. Empty lines are
added just for better readability. And some commands are added to make the macro
applicable also to other images (with a different name, width, height) and to close all
windows that we don’t need anymore once we have the ROIs.
run("Duplicate...", "title=Objects");
run("Gaussian Blur...", "sigma=2");
run("Make Binary");
run("Duplicate...", "title=Boundary");
w = getWidth() - 2; // Width of the selection
h = getHeight() - 2; // Height of the selection
run("Specify...", "width=&w height=&h x=1 y=1");
run("Clear", "slice");
run("Select None");
imageCalculator("Subtract", "Objects","Boundary");
selectWindow("Boundary");
close();
run("Invert");
run("Duplicate...", "title=Boundary");
run("Specify...", "width=&w height=&h x=1 y=1");
run("Clear", "slice");
run("Select None");
run("Invert");
rename("Masks");
14
run("Analyze Particles...", "pixel add");
roiManager("Show All with labels");
selectWindow(input);
roiManager("Show All with labels");
selectWindow("Objects");
close();
selectWindow("Masks");
close();
function reconstruct() {
run("Options...", "iterations=1 count=1 black do=Nothing");
oldmean = 0;
getStatistics(area,mean);
while(mean != oldmean) {
oldmean = mean;
run("Dilate");
imageCalculator("AND", "Boundary","Objects");
getStatistics(area,mean);
}
}
15