Assignment 3: SimpleITK Notebooks
Methods In Medical Image Analysis (BioE 2630 : 16-725) - Spring 2018

Creative Commons License SimpleITK Notebooks 1 assignment by John Galeotti, © 2018 Carnegie Mellon University, is licensed under a Creative Commons Attribution 3.0 Unported License. Permissions beyond the scope of this license may be available by sending email to itk (The SimpleITK Notebooks referenced are separately © by their own authors, etc..)

John Galeotti
Usually meets with students after class
Peiyun Hu
By email or appointment

25 points total

Due Date: Email your submission to the TA/Grader by 5pm on Thursday night, March 1st. (extended 2 days: The end of Problem 1 was supposed to have you modify the basic code to do edge preserving smoothing with Otsu thresholding. Otherwise, the comparison question doesn't make sense.)

Submit everything as a single email.

(Windows users can install the free and open source PDFCreator to print to pdf. Macs can natively print to pdf files. Linux can natively print to either ps or pdf, and ps can easily be converted to pdf using ps2pdf or pstopdf from the command line.)

Prep (0 points)

You should have previously downloaded a set of SimpleITK notebooks by using git as instructed in assignment 2.4. In this assignment, we will use an updated version of notebooks. You should now open a command prompt, and begin by renaming your notebooks directory to SimpleITK-Notebooks-old:

cd \mimia\
ren SimpleITK-Notebooks SimpleITK-Notebooks-old

Make a new directory to hold images from this homework:

mkdir hw3

Now checkout an updated notebooks repository, and then change to your new SimpleITK-Notebooks folder before launching iPython's notebook interface:

git clone
cd SimpleITK-Notebooks

Finally you can launch a web-based python interface called Jupyter. You should do so from the command line, making sure you are already in the notebooks directory (like we just did above):


Problem 1  (15 points)

After you launch the Jupyter notebook's web interface, in the Jupyter web page locate a directory named Python and then single click to open it. Inside the directory, find a notebook named notebook 300_Segmentation_Overview.ipynb. Open the notebook and work through the first part of it. Stop when you get to the fast-marching level set. You are now ready to proceed with the following changes.

If you are not familiar with Jupyter notebook, it is an interactive Python environment. It divides your Python code into different cells and runs each cell separately. Such structure offers great flexibility for editing code. For example, when you want to add a cell, you can first click Insert at top and choose to add above or below a selected cell. If you prefer keyboard shortcut, try press a or b and see what happens.

Now try inserting a new cell in the notebook after the segmentation result produced by Otsu Thresholding. Inside the new cell, write the following lines to save the both the scanned image and segmentation to disk, such as this:


Since the notebook is running under mimia/SimpleITK-Notebooks/Python, by running two lines above, we will be saving images in to mimia/hw3/.

To view the stored image and segmentation, we use ITK-Snap, which you should have installed as part of the previous homework. Open ITK-Snap and click Open Image … in the bottom right corner. (Note: The first time Mac users open ITK-Snap, they should right click on the application and then click open, so that OS X will allow them to open from an "unidentified developer.") In the pop-up, click Browse … to find mimia/hw3/img_T1.nii. The software should automatically identify the file format (i.e. NiFTI). Click Next >. If you see a warning regarding loss of precision, feel free to ignore it. Click Finish.

Now you should see img_T1 sliced along three different axes. Play with the blue crosshairs and the scroll bar and see how each view changes. Notice how the Cursor Inspector panel on the left shows the 3D coordinates of the blue crosshairs, as well as the pixel value at that location. You can scrool through slices under the cursor using scroll wheel, and you can zoom using either the Zoom to Fit button or by holding the right mouse button while dragging over an image. (Mac users can additionally use trackpad gestures to scroll through slices and pinch to zoom.) There are two useful buttons attached to each panel. One expands the view to the entire window and the other takes a snapshot. Finally, there is one very important warning about 3D coordinates in ITK-Snap:

ITK-Snap was made for "clinicians" and so starts numbering at 1. However, ITK and SimpleITK were made for programmers and so start numbering at 0. Accordingly, ITK pixel coordinate (3 25 40) would be the same as ITK-Snap coordinate (4 26 41).

To apply segmentation onto the scanned image, we now open the segmentation image we stored (i.e. mimia/hw3/img_T1_seg.nii). Click Segmentation > Open Segmentation … (in the menu bar) and load the segmentation. Once segmentation is loaded, we can click update in the bottom left corner to render a 3D mesh. Once it finishes, we should be able to see a 3D head rendered inside the bottom left panel. Now take a screenshot of ITK-SNAP when you finish rendering the 3D segmentation.

Now, let’s modify the notebook to try out edge-preserving smoothing and see how it may change the segmentation result. Let’s insert a cell below Otsu Thresholding and type “### (Edge-preserving) Smoothing + Thresholding”. Now we change the cell from Code to Markdown. Run this cell and you will see this cell becomes a header. Now insert yet another cell (for code) beneath this new header. Use this new code cell for the rest of Problem 1.

Simple ITK provides both a procedural interface (with multiple arguments to a single function call) and an object-oriented class-based interface. You can use either style, or mix and match between them. We will introduce both now as we demonstrate how to do Gaussian smoothing. Try this out in your new code cell:

img_T1_srg = sitk.SmoothingRecursiveGaussian(img_T1, sigma=1, normalizeAcrossScale=True)

Above is the procedure-style, one can also do the same thing with the class-based interface:

gf = sitk.SmoothingRecursiveGaussianImageFilter()
img_T1_gf = gf.Execute(img_T1)

Now we visualize the result of Gaussian Smoothing plus basic thresholding (basic thresholding can also be done directly in python as follows, but note how we then type-cast and rescale from boolean to 8-bit unsigned int with "true" changing to 255 "white"):

seg_gf = img_T1_gf>250
img_T1_gf_255 = sitk.Cast(sitk.RescaleIntensity(img_T1_gf), sitk.sitkUInt8)
myshow(sitk.LabelOverlay(img_T1_gf_255, seg_gf), "Smoothing Recursive Gaussian + Basic Thresholding")

Once the above seems to be working for you, you are ready for the main part of this assignment. You need to modify the above code to instead do Edge-preserving smoothing and Otsu thresholding. You will need to consult the lecture notes, the SimpleITK Doxygen, and possibly other documentation sources to do this. (Hint: Bilateral filtering may take too long to execute in 3D.) Then, you also need to save the resulting segmentation as a new image file named mimia/hw3/img_T1_eps_seg.nii and then load this segmentation into ITK-Snap. Take a new screenshot of ITK-SNAP when you finish rendering the new 3D segmentation. Now compare the new segmentation to the original Otsu Segmentation. Snap's file menu allows you to "add another image", which provides several nice ways to compare segmentations.

When finished, "print"/save the entire completed portion (up until level sets) of the notebook as a pdf file. Email the pdf and both screenshots of ITK-SNAP to your grader/instructor. Please make sure that the entire notebook, including all code, is visible in the pdf (long warning messages don't matter). You may need to tell your webbrowser to print the notebook at a reduced scale, e.g. 70%, to fit everything.

Problem 2  (4 points)

Open and work through all of 30_Segmentation_Region_Growing.ipynb. After completing this notebook, "print"/save the entire notebook as a pdf file. Email the pdf to your grader/instructor.

Problem 3  (6 points)

Finally, include in your email (not as an attachment) a short description (100 - 200 words) of the differences between all the threshold-based altorighms you just used, including your observations as to their strengths, weaknesses, and best applications for use. Finish with a paragraph about the result (big or small) of doing edge preserving smoothing vs Gaussian blurring vs no preprocessing at all on Otsu thresholding of this T1 brain MRI.