This tutorial illustrates how to use some of the basic image filtering capabilities of SimpleITK.
We start by importing the SimpleITK python module
import SimpleITK as sitk
We download a specific image from the Figshare data sharing and hosting site
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
if not os.path.isfile(destination):
urllib.urlretrieve(source,destination)
Then we read in an image
colorimage = sitk.ReadImage(destination)
Typical image fileformats such as PNG and JPG will be read as color images (three channels matching RGB components).
image = sitk.VectorMagnitude(colorimage)
We extract the pixel data in the form of a numpy array, that then we visualize using the Matplotlib python module
imshow(sitk.GetArrayFromImage(image));
By default, imshow would use a rainbow colormap. We can change that by selecting from http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
import matplotlib.pyplot as plt
import matplotlib.cm as cm
imshow(sitk.GetArrayFromImage(image),cmap=cm.gray);
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.
Smooting is often used to reduce noise.
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.
Experiment with the values of Sigma and see its effect on the image.
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:
rgsmoothedimage = rgsmootherfilter.Execute(image,3.5,1)
imshow(sitk.GetArrayFromImage(rgsmoothedimage),cmap=cm.gray);
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
Experiment with the values of the Radius and see the effect on the smoothed image
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.
medianimage = medianfilter.Execute(image,[3,3])
imshow(sitk.GetArrayFromImage(medianimage),cmap=cm.gray);
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.
Experiment with the values of number of iterations and time step size.
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:
rgsmootherfilter.SetSigma(2.5)
imshow(sitk.GetArrayFromImage(curvatureflowimage),cmap=cm.gray);
Mathematical Morphology filters can be applied in Grayscale
Based on a size of neighborhood type defined in the SetKernelType() method, and a neighborhood size defined in the SetKernelRadius() method.
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);
Based on a size of neighborhood type defined in the SetKernelType() method, and a neighborhood size defined in the SetKernelRadius() method.
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);
Filters commonly used in Edge Detection
The classic Canny filter. We define the lower threshold for edge connections and the Variance of the smoothing component.
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);
In the Canny filter above, experiment with other values of
The Laplacian Recursive Image filter is based on the RecursiveGaussianImageFilter, and its second derivatives. We define the Sigma value for the Gaussian.
rglaplacianfilter = sitk.LaplacianRecursiveGaussianImageFilter()
rglaplacianfilter.SetSigma(2.5)
rglaplacianfilter.SetNormalizeAcrossScale(1)
rglaplacianimage = rglaplacianfilter.Execute(image)
imshow(sitk.GetArrayFromImage(rglaplacianimage),cmap=cm.gray);
Filters commonly used to threshold images.
Computes a threshold value that maximizes the separation between classes in the histogram. Then applies this threshold the image.
otsu_filter = sitk.OtsuThresholdImageFilter()
otsu_image = otsu_filter.Execute(image)
imshow(sitk.GetArrayFromImage(otsu_image),cmap=cm.gray);
These filters perform operations on binary images based on the counts (votes) of neighboring pixels.
This filter is based on a Voting operation. It is useful to remove isolated pixels from the image
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);
Performs the filling operation multiple times until no pixels change, or maximum number of iterations is reached.
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);
In the Hole Filling filters above, experiment with the values of:
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:
dir(sitk)