NB2121 Practical 2 Exercises
NB2121 Practical 2 Exercises
Practical 2: Processing
Learning Goals
After this practical you should know by heart how to:
Plot and interpret the histogram of an image.
Analyze the image histogram within live selections.
Improve image contrast using histogram equalization.
Smooth an image using Gaussian and median filtering.
Know whether to use Gaussian or median smoothing.
Filter an image with custom designed convolution kernels.
Estimate the possible range of output values of a kernel.
Know when and how to convert an image to 32-bits/pixel.
Understand the difference between convolution and correlation.
Figure out if a filter is applied as a convolution or a correlation.
Process an image using basic mathematical operators.
Duplicate an image for intermediate calculations.
Implement an algorithm that performs edge detection.
Use the macro recorder of ImageJ to make a script.
Record, edit, load, save, and run macros in ImageJ.
Split image stacks into separate image slices.
Set the number of parallel threads used by ImageJ.
Apply Gaussian smoothing via the Fourier transform.
Use variables in macro code and print their values.
Look up macro functions and use them in macros.
Intensity Histogram
A good way to get an impression of the distribution of the intensities within an image
is to look at the histogram. The ImageJ command is Analyze > Histogram.
Question
Open Images / Processing / Cell Nuclei 1.tif and look at its histogram. To what part
of the image does the peak on the left of the histogram correspond? And what about
1
the very low and broad “peak” in the middle of the histogram?
Answer
The peak on the left of the histogram, corresponds to the high number of pixels
inside the center of the cells that have a low intensity. The low and broad peak in the
middle corresponds to the pixels more on the perimeter of the cell, which
The histogram contains information about the image intensities but does not give any
spatial information. This is why it may happen that there is no clear relationship
between certain peaks in the histogram and certain regions within the image.
Question
Open Images / Processing / Cell Nuclei 2.tif and look at its histogram. Which peak
in the histogram corresponds to which regions in the image?
Answer the same question for the two images Images / Processing / CT Lung 1.tif
and Images / Processing / TEM Image.tif.
Hint: To get more insight into how the different regions in the image contribute to the
histogram, activate the Oval selections tool on the ImageJ toolbar, draw a circle in
the image, and then activate the Live button in the histogram window. If you now
move the circular selection over the image, the histogram window shows only the
intensity distribution within the selection. If you deactivate the selection (by clicking
outside the selection) the histogram of the whole image will be shown again.
Answer
2
Contrast Stretching
Image details can be made more clearly visible by applying local contrast stretching.
The ImageJ command is Process > Enhance Local Contrast (CLAHE).
Question
Open Images / Processing / CT Lung 1.tif and look at its histogram. Now apply the
CLAHE command using its default parameter settings. Look at the histogram of the
result image and compare it with the initial histogram. Can you explain from the
differences between the two histograms why the result image has more contrast
within each anatomical region (for example the lungs)?
Answer
Local contrast stretching is also called histogram equalization. To see why, repeat
the CLAHE command three more times (each run applied to the output of the
previous run) and look at the histogram of the final result.
Image Smoothing
The two most popular methods for reducing noise in an image are Gaussian
smoothing and median smoothing. The ImageJ commands are Process > Filters >
Gaussian Blur… and Process > Filters > Median… respectively.
Open Images / Processing / GFP Spots.tif and zoom in to better see the pixels in
the image. The image shows several small spots of fluorescent particles in the cell
cytoplasm. Suppose we are interested in measuring these spots (number and size),
but the computer has problems finding the spots because the image is so noisy. In
that case we would like to reduce noise somewhat but at the same time the spots
should of course be preserved as best as possible.
Show the histogram of the image and activate the Live button in the histogram
window. Click on the image window to select it and then run the command for
3
Gaussian filtering (if you do not first click on the image window, the command will be
applied to the histogram window, which ImageJ also sees as an image).
In the Gaussian Blur window, select Preview and fill in different values in the Sigma
(Radius) field to see the effects in the image and in the histogram.
Repeat this for median filtering. In the Median window, select Preview and use
different values for Radius to see the effects in the image and in the histogram.
Question
Both types of filters reduce noise in the image. But which of the two filters, Gaussian
or median, better preserves the relevant image information in this case?
Answer
Open Images / Processing / Cell Blobs.tif and run the command. In the Convolver
window, delete the default kernel, enter the 3 x 3 uniform filter kernel (all values 1),
and select the options Normalize Kernel and Preview (the latter allows you to
immediately get an impression of the effect of a kernel while entering its values).
Question
Enter the 5 x 5 uniform filter kernel and preview its effects. Now deselect the
Normalize Kernel option. Why is the resulting image all white?
Answer
4
Classical kernels to estimate the first derivatives of an image are the Sobel kernels:
1 0 -1 1 2 1
2 0 -2 0 0 0
S x =¿ 1 0 -1 and S y =¿ -1 -2 -1
Question
Measure the minimum and the maximum intensity value of image Cell Blobs.tif.
What will theoretically be the minimum and the maximum intensity value of the result
image if we apply the Sobel kernel S x shown above?
Now apply the kernel (enter it in the Convolver window and click OK) and do the
measurement. Does the range of intensity values in the result image indeed match
with what you expected? If not, why?
Answer
Notice that if an image is 8-bit, and you apply an ImageJ command that will store its
output into the same image (replacing the original values), the resulting pixel values
will be clipped to the 8-bit intensity range (0…255). To avoid clipping, the image
should first be converted to 32-bit floating-point using Image > Type > 32-bit.
5
Convert the original image Cell Blobs.tif to a 32-bit image and then apply the kernel
S x. Measure again the minimum and the maximum intensity value of the result.
Convolution:
O ( x , y )=∑ ∑ I ( x−p , y −q ) K ( p , q)
p q
Correlation:
O ( x , y )=∑ ∑ I ( x+ p , y +q ) K ( p , q)
p q
Here, I (x , y ) is the input image, O ( x , y ) the output image, and K ( x , y) the kernel.
Question
Assume that the input image is an impulse: I ( 0,0 )=1 and I ( x , y )=0 for all ( x , y ) ≠(0,0).
Using the above equations, deduce what will be the resulting output image, in the
case of convolution and in the case of correlation.
Answer
Question
6
Open Images / Processing / Impulse 2D.tif and apply the S x kernel.
Move the mouse cursor over the resulting image to check its intensity values.
Is this the result of a convolution or of a correlation?
Answer
Question
Under what condition (what property of the kernel) do convolution and correlation
yield exactly the same result?
Answer
Using the two Sobel filters we are going to build an edge detector. It requires a series
of steps to implement the formula for computing the gradient magnitude:
‖∇ I( x , y)‖=√ I 2x ( x , y )+ I 2y ( x , y )
Question
The above formula can be broken down into six basic steps (operations) to compute
the output image (gradient magnitude) from the input image.
7
Write down (in words) what these six steps are, and the ImageJ commands that are
needed to implement them. Use only commands from Process > Filters and
Process > Math and Process > Image Calculator…
Answer
To avoid having to manually select and execute these commands every time we
want to apply edge detection, we are going to record them, so that we end up with a
macro (a script) that we can easily run again by pressing a single button.
Open Images / Processing / Cell Blobs.tif and run the command Plugins >
Macros > Record… A new window is opened that will record everything we do next.
Now run the commands that perform the steps you have identified above to compute
the gradient magnitude image of the input image. Two notes: 1) Remember to deal
with the 8-bit versus 32-bit issue we encountered before; 2) Since some commands
overwrite the input image, but you may need that image for multiple steps, make a
8
duplicate of the image using Image > Duplicate… So in the end the macro will
actually contain a few more steps than the six steps listed above.
When you have performed all the steps, click the Create button of the Recorder
window to get the final macro, which you can then save (and later open again when
you need it) via the File menu of the macro window. Clicking the Run button of the
macro window will apply the macro to the currently active image window.
Open Images / Processing / Cell Colony 1.tif. We are first going to apply Gaussian
filtering in the spatial domain and then in the Fourier domain.
where x and y are the spatial coordinates and the Gaussian kernel is given by:
2 2
−x +y
1 2σ
2
G σ ( x , y )= 2
e
2π σ
The first thing we need to do here is construct the Gaussian kernel. We have already
made a macro for you that does this (see the Practicals / Macros section of the
Brightspace page of this course). Open Macros / Gaussian Mask Text.ijm. A new
macro window is opened showing the code.
Click the button Run at the bottom of the macro window to run the macro and use the
default parameters (sigma = 3 pixels and size = 21 pixels). The output of the macro is
a new Log window containing the 21 x 21 kernel values of the Gaussian.
To see what the kernel looks like, save the kernel values to a file named
Gaussian.txt using menu command File > Save As… of the Log window, and
reopen the file as an image using the ImageJ command File > Import > Text
9
Image… Analyze the image by drawing a straight-line selection and showing the
profile along that line (run the corresponding ImageJ command).
^ ( u , v )= ^I ( u , v ) G
O ^ (u , v )
σ
where u and v are the frequency coordinates (the image pixel coordinates are scaled
versions of these) and the Gaussian mask is given by:
^ σ ( u , v )=e−2 π σ (u + v )
2 2 2 2
The first thing we need to do here is to construct the Fourier transform image of the
Gaussian kernel. We have already made a macro for you that does this (see
Brightspace). Open Macros / Gaussian Mask Image.ijm. A new macro window is
opened showing the macro code.
Click the button Run at the bottom of the macro window to run the macro and use the
default parameters (sigma = 3 pixels and size = 512 pixels). The output of the macro
is a new 32-bit image containing the Fourier transform of the Gaussian kernel.
We also need the Fourier transform of the input image. Reopen Images /
Processing / Cell Colony 1.tif and run Process > FFT > FFT Options… (FFT is
short for Fast Fourier Transform). Select the option Complex Fourier Transform,
deselect everything else, and then click OK. Apply the (complex) Fourier transform
using Process > FFT > FFT. The result is an image containing two slices,
corresponding to the real and the imaginary part of the Fourier transform.
Now apply the Gaussian mask to the Fourier transform of the image:
Select the Fourier transform image window (click its title bar).
Run ImageJ command Process > Image Calculator…
Select the Fourier transform image as Image 1.
Select Multiply as Operation.
10
Select the Gaussian mask image as Image 2.
Deselect Create new window and 32-bit (float) result.
Click the OK button and answer Yes to Process all 2 images?
Perform the inverse Fourier transform using Process > FFT > Inverse FFT.
Since the command performs the complex Fourier transform, the resulting image is a
complex image (it has a real and an imaginary part). Separate the real and imaginary
parts using Image > Stacks > Stack to Images. Delete the imaginary part as save
the real part as Cell Colony Gauss Fourier.tif.
Question
Did the spatial-domain Gaussian filtering produce exactly the same result as the
Fourier-domain Gaussian filtering? If not, explain the differences.
Hint: Draw a rectangular selection in the subtraction image and measure the
minimum and the maximum value in different regions of the image to get a better
understanding of the spatial distribution of the differences.
Answer
To make a fair comparison, we need to make sure both are going to be executed on
a single core of your microprocessor. Run the command Edit > Options > Memory
& Threads… and set Parallel threads to 1.
11
Also, we need a sufficiently large image to make sure the measured execution times
are mostly due to the actual computations and not due to overhead. Create a new 8-
bit black test image of 4096 x 4096 pixels using File > New > Image… And create a
new Gaussian mask image of 4096 x 4096 pixels using the previous macro.
Question
Apply the spatial-domain Gaussian filtering to the test image, and at the end of the
process write down the total computation time reported in the ImageJ status bar.
Now apply the Fourier-domain Gaussian filtering to the test image, estimate the
execution times for the three steps (forward FFT of the image, multiplication with the
Gaussian mask image, inverse FFT of the result image), and add them up. Note that
the FFT command does not report the total computation time in the ImageJ status
bar, so you will need to use a stopwatch to make an estimate.
Answer
In the foregoing exercise the use of a hand stopwatch was a bit of a lame approach.
So here is a final challenge for today to make things more professional:
Challenge
Assuming you already have the test image and the Gaussian mask image open,
record a macro that performs the three steps of Fourier-domain Gaussian filtering
(forward FFT of the test image, multiplication with the Gaussian mask image, and
inverse FFT of the result image). Now add some code to your macro so that it will
automatically measure and print the total computation time in seconds.
Hint: Go to the Practicals > Manuals section of this course in Brightspace and study
the ImageJ Macro Guide, in particular Chapter 8 to learn how to work with variables
12
and print their values, and Chapter 15 for an overview of all macro functions of
ImageJ. Click ImageJ Macro Functions to read the overview online. The relevant
macro functions for this exercise are getTime() and print(string).
Solution
When you’re done, don’t forget to restore the number of parallel threads used by
ImageJ to its previous value in Edit > Options > Memory & Threads…, or otherwise
future exercises may take a lot more computation time.
13