You are on page 1of 7

Number of marks available for each task are giving in [] after the task number

Task 1 [5]

The CT images used for this coursework are stored as nifti images. Using the functions you
wrote for exercises 2, or the example solutions provided after the exercises, read in the
baseline and follow up CT scans stored in the files baseline_CT.nii and followup_CT.nii, and
display coronal slice number 256 from each scan in a separate figure. The axes should be
numbered with slice/voxel numbers (i.e. not in mm), and you should add a title to each image
as shown below:

Note, you may notice that the slices at the bottom of the scans appear different to those
higher up this is not due to a problem with the code, but is due to an artefact in the data,
or rather that the scans have actually been made by stitching together two separate scans.

Save all the commands for this task (together with comments) in a single script and submit it
as your solution to the task. Please also submit the functions you used for reading and
displaying the nifti images, whether these were your own functions or the example solutions
provided. Note you will not be marked on the functions, they are just required so that the
script you write for this task can be run correctly.

Task 2 [10]

The data file lung_masks.mat contains four binary volumes (saved as MATLAB logical arrays)
representing the left and right lung in each image. Load in these volumes and then use all the
techniques you learnt in the lectures and exercises to produce two nice 3D visualisations of
the lungs, one from each scan. You should display the left lung in green and the right lung in
red, set the viewing angle to (-90,0), use lighting effects, and scale the axis correctly to
account for the voxel dimensions. You should use the same axis limits for both visualisations.
You should also add a title to each visualisation, so that your results should look like the first
set of results shown on the next page.
As you will see from these results, the visualisations still appear a bit blocky, even after the
volumes have been smoothed using the standard 3x3x3 box filter that was used in the
exercises. You may also notice that the right lung appears on the left in the visualisation, and
the left lung on the right. This is because the function used to generate the visualisation
assumes the image data is in the MATLAB coordinate system where the first array dimension
maps to the y-dimension. Now produce two more improved visualisations by smoothing the
original volumes with a larger 9x9x9 box filter (note this can take a few minutes to run), and
swapping dimensions 1 and 2 of the volumes before creating the visualisations. Note, you will
need to adjust the viewing angle to account for swapping dimensions 1 and 2 so that the
lungs are viewed from the same direction as before you need to figure out which viewing
angle to use, and then add a command to your code to set it appropriately. The results should
appear as shown below:

Save all the commands used to load the data and produce both sets of visualisations
(together with comments) in a single script and submit it as your solution to the task

Task 3 [5]

You will have seen from the previous tasks that the volume of the lungs has changed between
the two scans. Confirm this by calculating the volume of each lung in each scan in litres, and
displaying the results using formatted text so that they appear like this on screen:

Volume of lungs (in litres);

Baseline: left=*.**, right=*.**

Follow up: left: *.**, right=*.**


where *.** is the volume of the lung in litres to 2 decimal places, e.g. 2.31 (i.e. your code
should display the correct value calculated from the data instead of *.**). Hint there are
10^6 mm3 in a litre.

Part of the volume change will be due to natural differences in the inspiratory volume of the
patient when the scans were taken. Therefore, what we are really interested in is how the
relative volume of each lung (compared to the other lung) has changed after radiotherapy. We
would expect the lung containing the tumour (in this case the right lung) to have gotten
smaller relative to the other lung, due to both the tumour disappearing and the healthy lung
stiffening and shrinking from radiation induced fibrosis. Calculate the relative volume of each
lung (as a percentage of the total lung volume) from the baseline and follow up scans, and
display the results using formatted text so that they appear like this on screen (note, you
should use a percentage sign as shown, and not the word percent):

Volume of lungs as % of total lung volume

Baseline: left=**%, right=**%

Follow up: left: **% , right=**%

Where ** is the volume of the lung as a percentage of the total lung volume.

Save all the commands for this task (together with comments) in a single script and submit it
as your solution to the task

Task 4 [5]

Over the next few tasks you are going to develop your own code for automatically segmenting
the lungs from the CT images. One of the steps requires you to perform a morphological
closing operation, and for this you require a structuring element (see the last set of lecture
slides and MATLAB help for more details). We are going to use a structuring element that has
the shape of a 3D ball. For this task you must write a function that produces the 3D ball
structuring element. The function should take the radius of the ball, R, as input and return the
structuring element as output. The structuring element should be a 3D logical array, where
each voxel is set to 1 if it is within R voxels of the central voxel (note, the structuring element
should always have an odd number of voxels in each dimension), and 0 otherwise. The
structuring element should only be as large as required to contain all the 1s, i.e. there should
be no rows/columns/slices of just 0s at the edge. E.g. the structuring element, SE, produced
when using a radius of 2 should be:
Note the data file ball_SE.mat contains a variable, ball_SE, which should be the result when
running the function with a radius of 10 this can be used to test your code is giving the
correct answer, and, if you cannot get your code to give the correct answer then it can be
used to complete the following tasks.

For this task you should submit a single .m file containing the function you have written. There
is no need to submit any of the code you used to test your function is working (but I do advise
that you run some tests to convince yourself that your function is working.
Task 5 [10]

You are now going to write a script that segments the lungs in the baseline CT scan.

The first step is to form a binary image which contains all the voxels that have an intensity
larger than a lower threshold and also smaller than an upper threshold. In this case you
should use values of -900 and -500 for the lower and upper thresholds respectively. This will
mainly keep the voxels that belong to the lung tissue, as air voxels (as found outside the
patient or in the windpipe) will have generally have intensities lower than -900, and other
types of tissue will have values larger than -500. However, there will be some voxels
incorrectly included that are not part of the lungs (e.g. due to noise), as well as some lung
voxels that are not included (e.g. the vessels and airways in

the lungs, which we would like to include with the lungs), e.g.:

In order to remove the voxels that are not part of the lungs you should find the connected
components in the binary image (hint there are MATLAB functions for finding the connected
components that you can use). This should be done using a 3D connectivity of 6. The
connected component containing the largest number of voxels should correspond to the right
lung, and the second largest to the left lung. These should be used to create a binary image
for each lung, e.g.:
Finally, to include the voxels that are missing from the binary images of the lungs you should
perform a morpholigcal closing on each lung image seperately. To do this you should use a 3D
ball structuring element with a radius of 10 this should be created using the function you
wrote for the previous task, or if you could not get your function to give the correct result use
the structuring element in the data file mentioned in the previous task (but add a comments
to indicate where and how the function would have been called if it worked). Note
performing the 3D morpholical closing on these images can take > 10 mins, depending on
your computer this could be a good opportunity for a coffee break! If you have implemented
this task correctly your results should match those loaded from the data file in task 2. Add a
test to your script to check that this is the case.

Save all the commands for this task (together with comments) in a single script and submit it
as your solution to the task

Task 6 [10]

For this task you are going to write a function that will perform a lung segmentation using the
method from the previous task. The function should take the image to be segmented, the
thresholds, and the radius of the ball structuring element as inputs, and return the two binary
images of the lungs as outputs.

Note if you did not get your function for task 4 working correctly then your function from this
task should load the ball_SE structuring element saved in the data file and use that instead,
but you should include comments indicating where and how your function would have been
called if it worked.

Write a script that uses your function to segment the lungs from the follow up CT scan and
checks the result is the same as the binary lung images loaded from the data file in task 2.
Run your function again, but using a radius of 5 for the structuring element instead, and store
the results in new variables. Time how long it takes to run the function using a radius of 10
and a radius of 5. Using the smaller radius will give a result much faster but will cause some
regions of the lungs such as the larger airways and some of the fibrotic regions to be left out
of the segmentation. Add some commands to your script to visually compare the two
segmentations with the original images. To do this you should display each coronal slice from
the follow up scan, one at a time, with the two segmentation results overlaid as outlines, with
the radius 10 results displayed in cyan and the radius 5 results displayed in magenta, as
shown on the next page. You should pause for a short time, e.g. 0.1 seconds, before moving
on to the next slice, so that the results can be seen.

You should submit two .m files for this task, one containing the function you wrote, and the
other containing the script.
Task 7 m-level only [10]

You will have discovered in the previous tasks that performing a 3D closing operation can take
a lot of computation time. Alternatively, similar (but not identical results) can be obtained by
performing a series of three closing operations using a 2D disk shape structuring elements
(i.e. the 2D equivalent of the 3D ball structuring element). The disk structuring element
should have a different orientation for each closing operation, corresponding to each of the 3
dimensions.

Write a new version of your lung segmentation function, which performs the same steps as
before but does 3 x 2D closings instead of a 3D closing. Now write a script that uses your new
function to perform a segmentation of the follow up CT scan using a radius of 10 (and the
same thresholds as before) and time how long it takes to run. Also perform a visual
comparison as in the previous task, but comparing the results of the function written for this
task with the results from the previous task when a radius of 10 was used, i.e. display the
outlines of the lungs segmented using the two different functions, overlaid on the slices from
the follow up CT.

A common method for numerically comparing two segmentations is to calculate the Dice
Similarity Coefficient (DSC) between them. This is defined as:

2|| || + ||

Where || is the number of foreground voxels in binary image A, and is the intersection of
A and B, i.e. the voxels which are in both A and B.

Add commands to your script to calculate the DSC between the segmentations produced for
this task and those produced in task 6 (using a radius of 10). You should calculate the DSC
separately for the right lung and the left lung.

You should submit two .m files for this task, one containing the function you wrote, and the
other containing the script

You might also like