In [1]:
import SimpleITK as sitk
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)
In [ ]:
colorimage = sitk.ReadImage(destination)
In [ ]:
image = sitk.VectorMagnitude(colorimage)
In [ ]:
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
In [ ]:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
imshow(sitk.GetArrayFromImage(image),cmap=cm.gray);
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.
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);
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
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);
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.
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);
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);
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);
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);
In [ ]:
rglaplacianfilter = sitk.LaplacianRecursiveGaussianImageFilter()
rglaplacianfilter.SetSigma(2.5)
rglaplacianfilter.SetNormalizeAcrossScale(1)
rglaplacianimage = rglaplacianfilter.Execute(image)
imshow(sitk.GetArrayFromImage(rglaplacianimage),cmap=cm.gray);
In [ ]:
otsu_filter = sitk.OtsuThresholdImageFilter()
otsu_image = otsu_filter.Execute(image)
imshow(sitk.GetArrayFromImage(otsu_image),cmap=cm.gray);
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);
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);
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)