01-SimpleITK-Filtering


Welcome to the SimpleITK Image Filtering Tutorial

This tutorial illustrates how to use some of the basic image filtering capabilities of SimpleITK.

Importing SimpleITK

We start by importing the SimpleITK python module


In [1]:
import SimpleITK as sitk


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-3dd12c25dd88> in <module>()
----> 1 import SimpleITK as sitk

ImportError: No module named SimpleITK

Downloading an Image

We download a specific image from the Figshare data sharing and hosting site


In [ ]:
import urllib
import os.path

destination = '/tmp/TralitusSaltrator.jpg'
source = 'http://files.figshare.com/1546044/IMG_0974.jpg'

and we download the image file, only if it doesn't already exist in our local disk


In [ ]:
if not os.path.isfile(destination):
    urllib.urlretrieve(source,destination)

Reading an Image

Then we read in an image


In [ ]:
colorimage = sitk.ReadImage(destination)

Converting to Grayscale

Typical image fileformats such as PNG and JPG will be read as color images (three channels matching RGB components).


In [ ]:
image = sitk.VectorMagnitude(colorimage)

Displaying the Image

We extract the pixel data in the form of a numpy array, that then we visualize using the Matplotlib python module


In [ ]:
imshow(sitk.GetArrayFromImage(image));

Changing the Colormap

By default, imshow would use a rainbow colormap. We can change that by selecting from http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps


In [ ]:
import matplotlib.pyplot as plt
import matplotlib.cm as cm

imshow(sitk.GetArrayFromImage(image),cmap=cm.gray);

Your Work

Add cells here for experimenting with two other color maps from the SciPy wiki list above.

Note that you don't need to repeat the import of matplotlib modules.

Just call imshow() with different arguments in "cmap" color map.

Smoothing Filters

Smooting is often used to reduce noise.

Recursive Gaussian Smoothing

This is a relatively fast filter for smoothing an image along all directions.

Here we are showing off, how cool of equations we can add to the notebook

$$ SI(x') = \int^\infty_\infty{I(x'-x) e^{-x^2}}$$

We need to specify the Sigma value of the Gaussian function to use.

Your work

Experiment with the values of Sigma and see its effect on the image.


In [ ]:
rgsmootherfilter = sitk.SmoothingRecursiveGaussianImageFilter()
rgsmootherfilter.SetSigma(2.5)
rgsmootherfilter.SetNormalizeAcrossScale(1)
rgsmoothedimage  = rgsmootherfilter.Execute(image)

imshow(sitk.GetArrayFromImage(rgsmoothedimage),cmap=cm.gray);

The parameters of the filter can also be passed directly to the Execute() function as:


In [ ]:
rgsmoothedimage  = rgsmootherfilter.Execute(image,3.5,1)

imshow(sitk.GetArrayFromImage(rgsmoothedimage),cmap=cm.gray);

Median Image Filter

The Median filter is commonly used to provide robust smoothing of an image.

Its main parameter is the size of the neirborhood over which the median of pixels intensities is computed.

The neighborhood size is specified in terms of an integer number representing the radius of a Manhattan distance.

Therefore, a radius of 3 means that we will use a neighborhood of 7x7 pixels: 7 = 3 + 1 + 3

Your work

Experiment with the values of the Radius and see the effect on the smoothed image


In [ ]:
medianfilter = sitk.MedianImageFilter()
medianfilter.SetRadius(3)
medianimage  = medianfilter.Execute(image)

imshow(sitk.GetArrayFromImage(medianimage),cmap=cm.gray);

The parameters of the filter can also be passed directly to the Execute() function. Note that the Radius could be different in the X and Y directions, therefore we must pass an array with two values.


In [ ]:
medianimage  = medianfilter.Execute(image,[3,3])

imshow(sitk.GetArrayFromImage(medianimage),cmap=cm.gray);

Curvature Flow Filter

The Curvature flow filter applies a level set paradigm to the smoothin of a scalar image.

Its main parameters are the TimeStep and NumberOfIterations to use for performing the approximation of a solution to a differential equation.

  • Increasing the number of iterations will increase the smoothing
  • Reducing the time stop will improve the precision of the differential equation solution, but would require more iterations to produce the same amount of smoothing.

Your work

Experiment with the values of number of iterations and time step size.


In [ ]:
curvatureflowfilter = sitk.CurvatureFlowImageFilter()
curvatureflowfilter.SetTimeStep(0.01)
curvatureflowfilter.SetNumberOfIterations(30)
curvatureflowimage  = curvatureflowfilter.Execute(image)

imshow(sitk.GetArrayFromImage(curvatureflowimage),cmap=cm.gray);

The parameters of the filter can also be passed directly to the Execute() function as:


In [ ]:
rgsmootherfilter.SetSigma(2.5)

imshow(sitk.GetArrayFromImage(curvatureflowimage),cmap=cm.gray);

Grayscale Morphology

Mathematical Morphology filters can be applied in Grayscale

Grayscale Erosion

Based on a size of neighborhood type defined in the SetKernelType() method, and a neighborhood size defined in the SetKernelRadius() method.


In [ ]:
grayscale_erode_filter = sitk.GrayscaleErodeImageFilter()
grayscale_eroded_image = grayscale_erode_filter.Execute(image)
grayscale_erode_filter.SetKernelRadius(3)
grayscale_erode_filter.SetKernelType(sitk.sitkBall)
imshow(sitk.GetArrayFromImage(grayscale_eroded_image),cmap=cm.gray);

Grayscale Dilation

Based on a size of neighborhood type defined in the SetKernelType() method, and a neighborhood size defined in the SetKernelRadius() method.


In [ ]:
grayscale_dilate_filter = sitk.GrayscaleDilateImageFilter()
grayscale_dilate_image = grayscale_dilate_filter.Execute(image)
grayscale_dilate_filter.SetKernelRadius(3)
grayscale_dilate_filter.SetKernelType(sitk.sitkBall)
imshow(sitk.GetArrayFromImage(grayscale_dilate_image),cmap=cm.gray);

Edge Detection

Filters commonly used in Edge Detection

Canny Edge Detection

The classic Canny filter. We define the lower threshold for edge connections and the Variance of the smoothing component.


In [ ]:
canny_filter = sitk.CannyEdgeDetectionImageFilter()
float_image =  sitk.Cast(image, sitk.sitkFloat32)
canny_filter.SetLowerThreshold(50);
canny_filter.SetVariance(15);
canny_edges = canny_filter.Execute(float_image)
imshow(sitk.GetArrayFromImage(canny_edges),cmap=cm.gray);

Your Work

In the Canny filter above, experiment with other values of

  • The Variance
  • The Lower Threshold

Laplacian Recursive

The Laplacian Recursive Image filter is based on the RecursiveGaussianImageFilter, and its second derivatives. We define the Sigma value for the Gaussian.


In [ ]:
rglaplacianfilter = sitk.LaplacianRecursiveGaussianImageFilter()
rglaplacianfilter.SetSigma(2.5)
rglaplacianfilter.SetNormalizeAcrossScale(1)
rglaplacianimage  = rglaplacianfilter.Execute(image)

imshow(sitk.GetArrayFromImage(rglaplacianimage),cmap=cm.gray);

Your Work

In the Laplacian filter above, experiment with other values of

  • Sigma

Thresholding Filters

Filters commonly used to threshold images.

Otsu Threshold Image Filter

Computes a threshold value that maximizes the separation between classes in the histogram. Then applies this threshold the image.


In [ ]:
otsu_filter = sitk.OtsuThresholdImageFilter()
otsu_image = otsu_filter.Execute(image)
imshow(sitk.GetArrayFromImage(otsu_image),cmap=cm.gray);

Voting Filters

These filters perform operations on binary images based on the counts (votes) of neighboring pixels.

Hole Filling Filter

This filter is based on a Voting operation. It is useful to remove isolated pixels from the image


In [ ]:
hole_filling_filter = sitk.VotingBinaryHoleFillingImageFilter()
hole_filling_filter.SetRadius(2)
hole_filling_filter.SetMajorityThreshold(1)
hole_filling_filter.SetBackgroundValue(0)
hole_filling_filter.SetForegroundValue(1)
hole_filled_image = hole_filling_filter.Execute(otsu_image)
imshow(sitk.GetArrayFromImage(hole_filled_image),cmap=cm.gray);

Iterative Voting Hole Filling Filter

Performs the filling operation multiple times until no pixels change, or maximum number of iterations is reached.


In [ ]:
hole_filling_filter = sitk.VotingBinaryIterativeHoleFillingImageFilter()
hole_filling_filter.SetRadius(2)
hole_filling_filter.SetMajorityThreshold(1)
hole_filling_filter.SetBackgroundValue(0)
hole_filling_filter.SetForegroundValue(1)
hole_filled_image = hole_filling_filter.Execute(otsu_image)
imshow(sitk.GetArrayFromImage(hole_filled_image),cmap=cm.gray);

Your Work

In the Hole Filling filters above, experiment with the values of:

  • Radius
  • Majority Threshold

More Filters

Find more filters available in SimpleITK in the following documentation page

http://www.itk.org/SimpleITKDoxygen08/html/namespaceitk_1_1simple.html

Another way to see the full list of classes available in SimpleITK is to use the dir() function, as in:


In [ ]:
dir(sitk)