You are on page 1of 86

SimpleITK Tutorial

Image processing for mere mortals

Insight Software Consortium

Sept 23, 2011

January 10, 2012

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

1 / 142

This presentation is copyrighted by


The Insight Software Consortium
distributed under the
Creative Commons by Attribution License 3.0
http://creativecommons.org/licenses/by/3.0

What this Tutorial is about

Provide working knowledge of the SimpleITK platform

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

3 / 142

Program

Virtual Machines Preparation (10min)


Introduction (15min)
Basic Tutorials I (45min)
Short Break (10min)
Basic Tutorials II (45min)
Coee Break (30min)
Intermediate Tutorials (45min)
Short Break (10min)
Advanced Topics (40min)
Wrap-up (10min)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

4 / 142

Tutorial Goals

Gentle introduction to ITK


Introduce SimpleITK
Provide hands-on experience
Problem solving, not direction following
...but please follow directions!

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

13 / 142

ITK Overview

How many are familiar with ITK?

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

14 / 142

Ever seen code like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// Setup image types .


typedef
float InputPi xelType ;
typedef
float O ut pu t Pi xe lT y pe ;
typedef itk : : Image<InputPixelType , 2> Inpu tImageTy pe ;
typedef itk : : Image<OutputPixelType ,2> O ut pu tI m ag eT yp e ;
// Filter type
typedef itk : : DiscreteGaussianImageFilter<
InputImageType , O ut pu tI m ag eT yp e >
FilterType ;
// Create a filter
FilterType : : Pointer filter = FilterType : : New ( ) ;
// Create the p i p e l i n e
filter >SetInput ( reader >GetOutput ( ) ) ;
filter >SetVariance ( 1 . 0 ) ;
filter >S e t M a x i m u m K e r n e l W i d t h ( 5 ) ;
filter >Update ( ) ;
Ou t pu tI ma g eT yp e : : Pointer blurred = filter >GetOutput ( ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

15 / 142

What if you could write this?

1
2
3

import SimpleITK
input = SimpleITK . ReadImage ( filename )
output = SimpleITK . D i s c r e t e G a u s s i a n F i l t e r ( input , 1 . 0 , 5 )

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

16 / 142

What if you could write this?

1
2
3

import SimpleITK
input = SimpleITK . ReadImage ( filename )
output = SimpleITK . D i s c r e t e G a u s s i a n F i l t e r ( input , 1 . 0 , 5 )

We are here to tell you that you can...

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

16 / 142

Goals of SimpleITK

Be an on-ramp for ITK


Simplify the use of ITK by
Providing a templateless, typeless layer for ITK in C++
Providing wrappings in scripting languages
Providing access to most ITK algorithms

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

17 / 142

SimpleITK Architectural Overview

Conceptually, SimpleITK is an application library built on ITK


All functionality provided by ITK
Components:
Template expansion system
C++ library
Small SWIG definition (more details later)
Glue code for several scripting languages
Some language utilities

Open Source, Apache licensed project


(http://www.opensource.org/licenses/apache2.0.php)
Hosted by GitHub (https://github.com/SimpleITK/SimpleITK)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

18 / 142

Templates in ITK

1
2
3
4
5
6
7
8
9
10
11

typedef unsigned char PixelType ;


enum {Imag eDimensi on = 2 } ;
typedef itk : : Image<PixelType , ImageDimension> ImageType ;
typedef itk : : Vector<float , ImageDimension> VectorType ;
typedef itk : : Image<VectorType , ImageDimension> FieldType ;
typedef itk : : Image<VectorType : : ValueType , ImageDimension> Float ImageTyp e ;
typedef ImageType : : IndexType IndexType ;
typedef ImageType : : SizeType
SizeType ;
typedef ImageType : : RegionType RegionType ;
typedef itk : : M u l t i R e s o l u t i o n P D E D e f o r m a b l e R e g i s t r a t i o n
<ImageType , ImageType , FieldType> R e g i s t r a t i o n T y p e ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

19 / 142

Template Freedom

1
2
3
4
5
6
7
8
9
10
11

using itk : : simple ;


// Read the image file
I ma ge Fi l eR ea de r reader ;
reader . SetFileName ( " / my / fancy / file . nrrd " ) ;
Image image = reader . Execute ( ) ;
// This filters perform a gaussian bluring with sigma in
// ph y s i c a l space . The output image will be of real type .
S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian ;
gaussian . SetSigma ( 2 . 0 ) ;
Image blurredImage = gaussian . Execute ( image ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

20 / 142

Programming Models

Object oriented
Function oriented
More about this later

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

21 / 142

Wrapping

Transformed by SWIG
Parses header and interface files
Automatically creates scripting glue
Wrappings available for:
Python
Java
C#
Tcl, Lua, Ruby
Others as requested. . .

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

22 / 142

Image Anatomy

SimpleITK images contain itk::Image


Hides the templates
Adds useful utility functions
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

23 / 142

Filter Anatomy

SimpleITK filters contain itk::ImageToImageFilter


Hides the templates
Adds useful utility functions
(Mainly) output = Filter.Execute ( input )
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

24 / 142

Basic Tutorial

Basic Tutorial

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

25 / 142

Note on the Tutorial

Most examples will be Python


Obvious translation to other languages
C++ usage (generally) obvious

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

33 / 142

Image Class

Creation
Number of dimensions, size, origin, spacing
Pixel access
Back to iPython (Examples/BasicTutorial1/Image.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

34 / 142

What just happened?

1
2
3
4
5

import SimpleITK as sitk


# Create an image
image = sitk . Image ( 2 5 6 , 2 5 6 , 2 5 6 , sitk . sitkInt16 ) ;
# How about 2 d ?
twoD = sitk . Image ( 6 4 , 6 4 , sitk . sitkFloat32 )

sitk is the module


Image is the constructor for the Image class
Height, width, depth (omit depth for 2D images)
Datatype (more on this later)
Back to iPython (Examples/BasicTutorial1/Image.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

35 / 142

What just happened?

1
2
3
4

# Addressing pixels
image . GetPixel ( 0 , 0 , 0 )
image . SetPixel ( 0 , 0 , 0 , 1 )
image . GetPixel ( 0 , 0 , 0 )

Get the voxel value at [0,0,0]?


Hmm, I dont like it, so set to 1
What is the value at [0,0,0] now?
Back to iPython (Examples/BasicTutorial1/Image.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

36 / 142

What just happened?

1
2
3
4

# Addressing pixels
image [ 0 , 0 , 0 ]
image [ 0 , 0 , 0 ] = 10
image [ 0 , 0 , 0 ]

Without warning, we sprinkled syntatic sugar on you!


image[0,0,0] is shorthand for Image.GetPixel(0,0,0)
image[0,0,0] = 10 is shorthand for Image.SetPixel(0,0,0,10)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

37 / 142

Summary

Images are created using SimpleITK.Image ( w, h, d, Type )


Images can be 2- or 3-dimensional
Images can describe themselves
Images have simple pixel accessors
Questions before we move on?

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

38 / 142

Memory Management

Memory Management

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

39 / 142

Images

Images...
usually allocated on the stack
are copy-on-write
use internal smart-pointers
Back to iPython (Examples/BasicTutorial1/MemoryManagement.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

40 / 142

Image Memory Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

image = SimpleITK . Image ( 3 2 , 3 2 , 3 2 , SimpleITK . sitkInt16 )


print image
...
Image ( 0 x94f2d98 )
Reference Count : 1
...
# Clone image
b = SimpleITK . Image ( image )
print image
...
Image ( 0 x94f2d98 )
Reference Count : 2
...
print b
...
Image ( 0 x94f2d98 )

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

41 / 142

Image Memory Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

print b
...
Image ( 0 x94f2d98 )
...
b [0 ,0 ,0] = 1
print b
...
Image ( 0 x94f4cb0 )
Reference Count : 1
...
print image
...
Image ( 0 x94f2d98 )
Reference Count : 1
...

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

42 / 142

Filters

Filters...
usually allocated on the stack
tend to clean up after themselves
do not hold on to images
no access to the ITK pipeline
...more on this later...

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

43 / 142

Memory Management Strategies

C++. . .
No need for explicit management
Let images clean up after themselves
Let filters clean up after themselves
Wrapped. . .
Utilize language-specific memory management
Automatic in Python, Java, Ruby, C#, Lua
More manual in Tcl

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

44 / 142

Basic Tutorial 2

Basic Tutorial 2

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

50 / 142

Hands On

Hands On
Examples/BasicTutorial2/Filters.py

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

51 / 142

What just happened?

1
2
3
4
5
6
7

# Simple smoothing
smooth = sitk . S m o o t h i n g R e c u r s i v e G a u s s i a n ( image , 2 . 0 )
sitk . Show ( sitk . Subtract ( image , smooth ) )
...
RuntimeError : Exception thrown in SimpleITK Subtract : . . .
sitk : : ERROR : Both images for S u b t r a c t I m a g e F i l t e r don t match type or dimension !
...

The output of SmoothingRecursiveGaussian is of type float


The input image is signed short
Most SimpleITK filters with 2 inputs require the same type
Lets fix the problem

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

52 / 142

Introducing Cast

1
2
3
4
5

# Much better
print " Before : " , smooth . G e t P i x e l I D T y p e A s S t r i n g ( )
smooth = sitk . Cast ( smooth , image . G et Pi xe l ID Va lu e ( ) )
print " After : " , smooth . G e t P i x e l I D T y p e A s S t r i n g ( )
sitk . Show ( sitk . Subtract ( image , smooth ) , " D i f f W i t h G a u s s i a n " )

Back to iPython (Examples/BasicTutorial2/Filters.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

53 / 142

Sizes and Indices

1
2
3
4

# Extract
size = [ 6 4 , 6 4 , 1 ]
start = [ 6 4 , 0 , 0 ]
sitk . Show ( sitk . Extract ( image , size , start ) , " Extracted " )

in C++ / ITK code this would use


1
2
3
4
5
6
7

typedef unsigned char PixelType ;


enum {Imag eDimensi on = 2 } ;
typedef itk : : Image<PixelType , ImageDimension> ImageType ;
typedef ImageType : : IndexType IndexType ;
typedef ImageType : : SizeType
SizeType ;
typedef ImageType : : RegionType RegionType ;

SimpleITK uses STL vectors


Wrapping converts to language-specific constructs (tuples/arrays)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

54 / 142

Pixel-wise Operators
1
2

# Use pixel - wise operators


sitk . Show ( 127 image + 127 sitk . BinaryErode ( image ) , " ThinErosion " )

Table: SimpleITK Pixel-wise Operators

Operator
+

/
&
|

Equivalant
Add, AddConstantTo
Subtract, SubtractConstantFrom
Multiply, MultiplyByConstant
Divide, DivideByConstant
And and
Or or
Not not
Pow, PowToConstant

Usage
A + B, s + A, s + B
A B, s A, s B
A B, s A, s B
A/B, s/A, B/s
A&B
A|B
A
A B, A s

A and B are images (2D or 3D), s is a scalar


python only

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

58 / 142

Operator Example

1
2
3
4
5

sitk . Hash ( image + 2 )


sitk . Hash ( sitk . AddConstantTo ( image , 2 ) )
sitk . Hash ( image 2 )
sitk . Hash ( sitk . M u l t i p l y B y C o n s t a n t ( image , 2 ) )

Hash calculates the MD5 or SHA1 hash of the image data


Used to tell if two images are exactly identical
Back to iPython (Examples/BasicTutorial2/Morphology.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

59 / 142

SimpleITK with Numpy

Numpy is:
Pythons universal numerics foundation
Provides high performance basic linear algebra
numpy interface functions:
1
2
3
4
5

def G e t A r r a y F r o m I m a g e ( image ) :
""" Get a numpy array from a SimpleITK Image . """
def G e t I m a g e F r o m A r r a y ( arr ) :
""" Get a SimpleITK Image from a numpy array . """

Both of these methods do a deep copy of the image


Ensures safety, bypassing error-prone memory issues

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

68 / 142

Python Example

To iPython (Examples/InteractiveTutorial/05-02-Numpy.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

69 / 142

Image Measurements

Image Measurements

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

70 / 142

The Scenerio

You collected 1000 Diusion Weighted Imaging scan sessions


Now, compute the fractional anisotropy measures for a set of
anatomical regions

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

71 / 142

Python Example

To iPython (Examples/InteractiveTutorial/MeasureRegions.py)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

72 / 142

Feature Detection

Feature Detection

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

73 / 142

Edge Detection
CannyEdgeDetection
1
2
3
4
5

Image C a n n y E d g e D e t e c t i o n ( c o n s t Image& ,
double inLowerThreshold = 0.0 ,
double inUpperThreshold = 0.0 ,
s t d : : v e c t o r <d o u b l e> i n V a r i a n c e = s t d : : v e c t o r <d o u b l e >(3 , 0 . 0 ) ,
s t d : : v e c t o r <d o u b l e> inMaximumError = s t d : : v e c t o r <d o u b l e >(3 , 0 . 0 1 ) ) ;

SobelEdgeDetection
1

Image S o b e l E d g e D e t e c t i o n ( c o n s t Image&

);

ZeroCrossingBasedEdge
1
2
3
4
5

Image Z e r o C r o s s i n g B a s e d E d g e D e t e c t i o n ( c o n s t Image& ,
double i n V a r i a n c e = 1 ,
u i n t 8 t i n F o r e g r o u n d V a l u e = 1u ,
u i n t 8 t i n B a c k g r o u n d V a l u e = 0u ,
d o u b l e inMaximumError = 0 . 1 ) ;

GradientMagnitudeRecursiveGaussian
1
2
3

Image G r a d i e n t M a g n i t u d e R e c u r s i v e G a u s s i a n ( c o n s t Image& ,
double inSigma = 1.0 ,
bool inNormalizeAcrossScale = f a l s e ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

75 / 142

Image Derivatives

Derivative
1
2
3
4

Image D e r i v a t i v e ( c o n s t Image& ,
u n s i g n e d i n t i n D i r e c t i o n = 0u ,
u n s i g n e d i n t i n O r d e r = 1u ,
bool inUseImageSpacing = true ) ;

RecursiveGaussian
1
2
3
4
5

Image R e c u r s i v e G a u s s i a n ( c o n s t Image& ,
double inSigma = 1.0 ,
bool inNormalizeAcrossScale = fal se ,
i t k : : s i m p l e : : R e c u r s i v e G a u s s i a n I m a g e F i l t e r : : OrderEnumType i n O r d e r = i t k : : s i m p l
u n s i g n e d i n t i n D i r e c t i o n = 0u ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

76 / 142

Advanced Topics

Advanced Topics

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

81 / 142

Building SimpleITK in 5 Easy Commands

git clone --recursive https://github.com/SimpleITK/SimpleITK


mkdir SimpleITK/SimpleITK-build
cd SimpleITK/SimpleITK-build
cmake ../SuperBuild
make -j 5

Or as part of Slicer
ITK_VERSION_MAJOR=4
Slicer_USE_SimpleITK=ON

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

82 / 142

More complete version

Check out the code from GitHub


(https://github.com/SimpleITK/SimpleITK)
Run CMake (http://www.cmake.org/) using
SimpleITK/SuperBuild as the source directory
Build using your favorite compiler

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

83 / 142

Supported Platforms

Windows: Visual Studio 10


Windows: Visual Studio 9 (Requires TR1 service pack)
Mac OSX: gcc 4.x (Xcode 4.x)
Linux: gcc 4.x

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

84 / 142

SimpleITK Architecture

SimpleITK Architecture

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

85 / 142

Filter Anatomy

SimpleITK filters create ITK filters


Templated based on input type
Output type is usually the same as input type
Instantiated for many possible image types
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

86 / 142

Image and Filter Types


Dimensions

Vector Types

2 dimensional
3 dimensional

int8 t
uint8 t
int16 t
uint16 t
int32 t
uint32 t
float
double

Scalar types
int8 t
uint8 t
int16 t
uint16 t
int32 t
uint32 t
float
double
std :: complex < float >
std :: complex < double >

(Insight Software Consortium)

Label Types

SimpleITK - MICCAI 2011

uint8 t
uint16 t
uint32 t

Sept 2011

87 / 142

Filter Anatomy

Filter interrogates input


Instantiates proper ITK filter
Executes ITK filter
Constructs output from ITK image
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

88 / 142

Using Filters

Using Filters

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

89 / 142

Object Paradigm (C++)

1 #i n c l u d e <S i m p l e I T K . h>
2 namespace sitk = itk : : simple ;
3 ...
4 // Create a s m o o t h i n g filter
5 sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian ;
6
7 // Set a p a r a m e t e r
8 gaussian . SetSigma ( 2 . 0 ) ;
9
10 // " Execute " the Filter
11 sitk : : Image blurredImage = gaussian . Execute ( image ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

90 / 142

Object Paradigm (C++)

Flexibility
1 #i n c l u d e <S i m p l e I T K . h>
2 namespace sitk = itk : : simple ;
3 ...
4 // Create a s m o o t h i n g filter
5 sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian ;
6
7 // Set p a r a m e t e r ( s ) , then execute
8 sitk : : Image blurredImage = gaussian
9
. SetSigma ( 2 . 0 )
10
. Execute ( image ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

91 / 142

Object Paradigm (C++)

1 #i n c l u d e <S i m p l e I T K . h>
2 namespace sitk = itk : : simple ;
3 ...
4 blurredImage = sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r ( )
5
. SetSigma ( 2 . 0 )
6
. SetRadius ( 5 )
7
. Execute ( image ) ;

One line: create anonymous filter, set parameters, and execute

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

92 / 142

Function Paradigm (C++)

1 #i n c l u d e <S i m p l e I T K . h>
2 namespace sitk = itk : : simple ;
3 ...
4 // Call the f u n c t i o n version
5 // NB : Drop the " I m a g e F i l t e r "!
6 // S i g n a t u r e :
7 /*
8
sitk :: Image S m o o t h i n g R e c u r s i v e G a u s s i a n (
9
const Image & ,
10
double inSigma = 1.0 ,
11
bool i n N o r m a l i z e A c r o s s S c a l e = false );
12 */
13 sitk : : Image blurredImage = sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n (
14
image ,
15
2.0 ,
16
false ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

93 / 142

Mix & Match (C++)

1 #i n c l u d e <S i m p l e I T K . h>
2 namespace sitk = itk : : simple ;
3 ...
4 // Get our g a u s s i a n ready
5 sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian ;
6 gaussian . SetSigma ( 2 . 0 ) ;
7
8 // What is the effect on the image
9 sitk : : Image difference = sitk : : Subtract (
10
image ,
11
gaussian . Execute ( image )
12
);
13 sitk : : Image difference2 = sitk : : Subtract (
14
image ,
15
sitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n (
16
image , 2 . 0
17
)
18
);

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

94 / 142

Code Philosophy

Code Philosophy

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

95 / 142

Filter Class Overview (C++)

1 class S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r :
2
public ImageFilter {
3
typedef S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r Self ;
4
5
/* * Default C o n s t r u c t o r that takes no a r g u m e n t s
6
and i n i t i a l i z e s default p a r a m e t e r s */
7
SmoothingRecursiveGaussianImageFilter ( ) ;

In line 1, we declare a subclass of ImageFilter


Line 3 creates a special typedef for use later
The default constructor is line 7 (never any parameters)

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

96 / 142

Filter Class Overview (C++) Continued

1
2

/* * Define the pixels types s u p p o r t e d by this filter */


typedef B a s i c P i x e l I D T y p e L i s t P ix e lI DT yp e Li st ;

Notice PixelIDTypeList in line 2


Used to instantiate ITK filters
Determines valid input image types
BasicPixelIDTypeList expands to:
int8 t, uint8 t
int16 t, uint16 t
int32 t, uint32 t
float, double

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

97 / 142

Filter Class Overview (C++) Continued

1
2
3
4
5
6
7
8

Self& SetSigma ( double t ) { . . . return this ; }


double GetSigma ( ) { return this >m_Sigma ; }
Self& S e t N o r m a l i z e A c r o s s S c a l e ( bool t ) { . . . }
Self& N o r m a l i z e A c r o s s S c a l e O n ( ) { . . . }
Self& N o r m a l i z e A c r o s s S c a l e O f f ( ) { . . . }
bool G e t N o r m a l i z e A c r o s s S c a l e ( ) { . . . }

Get/Set parameters
Set methods always return Self & (more later)
Generally, a direct mapping to ITK
Boolean parameters generate On and O methods

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

98 / 142

Filter Class Overview (C++) Continued

1
2
3
4
5

/* * Name of this class */


std : : string GetName ( ) const { . . . }
/* * Print o u r s e l v e s out */
std : : string ToString ( ) const ;

Return the name and description of the filter

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

99 / 142

Filter Class Overview (C++) Continued

1
/* * Execute the filter on the input image */
2
Image Execute ( const Image & ) ;
3
4
/* * Execute the filter with p a r a m e t e r s */
5
Image Execute ( const Image &,
6
double inSigma ,
7
bool i n N o r m a l i z e A c r o s s S c a l e ) ;
8 } ; /* End of class S m o o t h i n g R e c u r s i v e G a u s s i a n */
9
10 Image S m o o t h i n g R e c u r s i v e G a u s s i a n ( const Image& ,
11
double inSigma = 1 . 0 ,
12
bool i n N o r m a l i z e A c r o s s S c a l e = false ) ;

Run the filter on an image and return the result


Notice extra function (line 10), adds flexibility
Drop ImageFilter from class name to get function name

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

100 / 142

Questions?

Questions?

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

101 / 142

Using ITK with SimpleITK

Problem: Use ITK from SimpleITK (or vice versa)


./ToITK input.nii output.nii
Steps:
Load image using SimpleITK
Filter using ITK
Save using OpenCV

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

102 / 142

To ITK

Starting code: ToITK/ToITK.cxx


Directory:
SimpleITK-MICCAI-2011-Tutorial/Examples/AdvancedTutorial
1
2
3
4
5
6
7
8
9
10
11
12

namespace s i t k = i t k : : s i m p l e ;
...
// Load t h e image v i a S i m p l e I T K
s i t k : : Image s i t k I m a g e = s i t k : : ReadImage ( i n p u t F i l e n a m e ) ;
// C o n s t r u c t t h e ITK P i p e l i n e
// L i n k p i p e l i n e t o S i m p l e I T K
// Update p i p e l i n e
// C r e a t e o u t p u t S i m p l e I T K image
// Save image v i a S i m p l e I T K
s i t k : : W r i t e I m a g e ( sOutput , o u t p u t F i l e n a m e ) ;
r e t u r n EXIT SUCCESS ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

103 / 142

ToITK Step 1: Construct the ITK Pipeline

1
2
3
4
5
6
7
8
9
10
11
12
13

// C o n s t r u c t t h e ITK P i p e l i n e
t y p e d e f i t k : : Image<f l o a t ,3> ImageType ;
t y p e d e f i t k : : M i r r o r P a d I m a g e F i l t e r <ImageType , ImageType> P a d F i l t e r T y p e ;
P a d F i l t e r T y p e : : S i z e T y p e upperBound , l o w e r B o u n d ;
P a d F i l t e r T y p e : : P o i n t e r pad = P a d F i l t e r T y p e : : New ( ) ;
f o r ( u n s i g n e d i n t i = 0 ; i < 3 ; i++ )
{
upperBound [ i ] = s i t k I m a g e . G e t S i z e ( ) [ i ] ;
lowerBound [ i ] = s i t k I m a g e . G e t S i z e ( ) [ i ] ;
}
pad >SetPadUpperBound ( upperBound ) ;
pad >SetPadLowerBound ( l o w e r B o u n d ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

104 / 142

ToITK Step 2: Link pipeline to SimpleITK

1
2
3

// L i n k p i p e l i n e t o S i m p l e I T K
ImageType : : P o i n t e r i n p u t I m a g e = ( ImageType ) s i t k I m a g e . G e t I m a g e B a s e ( ) ;
pad >S e t I n p u t ( i n p u t I m a g e ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

105 / 142

ToITK Step 3: Update ITK Pipeline

1
2

// Update p i p e l i n e
pad >Update ( ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

106 / 142

ToITK Step 4: Create the SimpleITK output image

1
2

// C r e a t e o u t p u t S i m p l e I T K image
s i t k : : Image s O u t p u t ( pad >GetOutput ( ) ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

107 / 142

ToITK (Optional) Step 5: Show

1
2

// ( O p t i o n a l ) Show t h e r e s u l t s
s i t k : : Show ( s O u t p u t ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

108 / 142

To ITK Solution
~/Source/AdvancedTutorial-build/ToITK/ToITKSolution \
~/Source/SimpleITK/Testing/Data/Input/RA-Float.nrrd \
/tmp/foo.nii

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

109 / 142

Quick Examples from Other Languages

Simple Gaussian in 8
languages
AdvancedTutorial/SimpleGaussian/SimpleGaussian.*

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

130 / 142

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# include < SimpleITK .h >


namespace sitk = itk :: simple ;
int main ( int argc , char * argv [] ) {
// Read the image file
sitk :: I ma g eF il eR e ad er reader ;
reader . SetFileName ( std :: string ( argv [1] ) );
sitk :: Image image = reader . Execute ();
// This filters perform a gauss ian bluring with sigma in physical
// space . The output image will be of real type .
sitk :: S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian ;
gaussian . SetSigma ( atof ( argv [2] ) );
sitk :: Image blurredImage = gaussian . Execute ( image );
// Covert the real output image back to the original pixel type , to
// make writing easier , as many file formats don t support real
// pixels .
sitk :: C as tI m ag eF il t er caster ;
caster . S e t O u t p u t P i x e l T y p e ( image . G et Pi x el ID Va l ue () );
sitk :: Image outputImage = caster . Execute ( blurredImage );
// write the image
sitk :: I ma ge F il eW ri t er writer ;
writer . SetFileName ( std :: string ( argv [3] ) );
writer . Execute ( outputImage );
return 0;
}

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

131 / 142

Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

require simpleitk
if ARGV . length != 3 then
puts " Usage : Simpl eGaussia n < input > < sigma > < output > " ;
exit ( 1 )
end
reader = Simpleitk : : I ma ge Fi l eR ea de r . new
reader . set_file_name ( ARGV [ 0 ] )
image = reader . execute
in putPixe lType = image . g e t _ p i x e l _ i d v a l u e
gaussian = Simpleitk : : S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r . new
gaussian . set_sigma ARGV [ 1 ] . to_f
image = gaussian . execute image ;
caster = Simpleitk : : C as tI ma g eF il te r . new
caster . s e t _ o u t p u t _ p i x e l _ t y p e inputP ixelType
image = caster . execute image
writer = Simpleitk : : I ma ge Fi l eW ri te r . new
writer . set_file_name ARGV [ 2 ]
writer . execute image

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

132 / 142

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# Run with :
#
# Rscript -- vanilla S i m p l e G a u s s i a n . R
#

input sigma output

library ( SimpleITK )
args <- commandArgs ( TRUE )
myreader <- I ma ge Fi l eR ea de r ( )
myreader <- I ma ge Fi l eR ea de r _ SetFileName ( myreader , args [ [ 1 ] ] )
myimage <- Im ag e Fi le Re a de r _ Execute ( myreader )
pixeltype <- Image _ G e tP ix el I DV al ue ( myimage )
myfilter <- S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r ( )
myfilter <- S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r _ SetSigma ( myfilter , as . real ( args [ 2 ] ) )
smoothedimage <- S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r _ Execute ( myfilter , myimage )
mycaster <- C as tI ma g eF il te r ( )
mycaster <- C as tI ma g eF il te r _ S e t O u t p u t P i x e l T y p e ( mycaster , pixeltype )
castedimage <- Ca st Im a ge Fi lt e r _ Execute ( mycaster , soothedimage )
mywriter <- I ma ge Fi l eW ri te r ( )
mywriter <- I ma ge Fi l eW ri te r _ SetFileName ( mywriter , args [ [ 3 ] ] )
mywriter <- I ma ge Fi l eW ri te r _ Execute ( mywriter , castedimage )

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

133 / 142

C#

1 u s i n g System ;
2 using itk . simple ;
3
4 namespace itk . simple . examples {
5
class Sim pleGauss ian {
6
static void Main ( string [ ] args ) {
7
try {
8
if ( args . Length < 3 ) {
9
Console . WriteLine ( " Usage : Simp leGauss ian < input > < sigma > < output > " ) ;
10
return ;
11
}
12
// Read input image
13
I ma ge Fi l eR ea de r reader = new I ma ge F il eR ea d er ( ) ;
14
reader . SetFileName ( args [ 0 ] ) ;
15
Image image = reader . Execute ( ) ;
16
17
// Execute Gaussian s m o o t h i n g filter
18
S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian = new S m o o t h i n g R e c u r s i v e G a
19
gaussian . SetSigma ( Double . Parse ( args [ 1 ] ) ) ;
20
image = gaussian . Execute ( image ) ;
21
22
// Write output image
23
I ma ge Fi l eW ri te r writer = new I ma ge F il eW ri t er ( ) ;
24
writer . SetFileName ( args [ 2 ] ) ;
25
writer . Execute ( image ) ;
26
27
} catch ( Exception ex ) {
28
Console . WriteLine ( ex ) ;
29
}
30
}
31
}
32 }
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

134 / 142

Java
1 import org . itk . simple . ;
2
3 class Simp leGaussi an {
4
5
public static void main ( String argv [ ] ) {
6
7
if ( argv . length < 3 ) {
8
System . out . println ( " Usage : java S impleGau ssian < input > < sigma > < output > " ) ;
9
return ;
10
}
11
12
org . itk . simple . I ma ge Fi l eR ea de r reader = new org . itk . simple . I ma ge Fi l eR ea de r ( ) ;
13
reader . setFileName ( argv [ 0 ] ) ;
14
Image img = reader . execute ( ) ;
15
16
S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r filter =
17
new S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r ( ) ;
18
filter . setSigma ( Double . valueOf ( argv [ 1 ] ) . doubleValue ( ) ) ;
19
Image blurredImg = filter . execute ( img ) ;
20
21
Ca s tI ma ge F il te r caster = new C as tI ma g eF il te r ( ) ;
22
caster . s e t O u t p u t P i x e l T y p e ( img . g et Pi xe l ID Va lu e ( ) ) ;
23
Image castImg = caster . execute ( blurredImg ) ;
24
25
Im a ge Fi le W ri te r writer = new I ma ge Fi l eW ri te r ( ) ;
26
writer . setFileName ( argv [ 2 ] ) ;
27
writer . execute ( castImg ) ;
28
29
}
30
31 }
(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

135 / 142

Lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

if #arg < 3 then


print ( Usage : Simpl eGaussia n <input> <sigma> <output> )
os . exit ( 1 )
end
reader = SimpleITK . I ma ge Fi l eR ea de r ( )
-- Re m e m b e r that Lua arrays are 1 - based ,
-- and that arg does not contain the a p p l i c a t i o n name !
reader : SetFileName ( arg [ 1 ] )
image = reader : Execute ( ) ;
in putPixe lType = image : G et Pi xe l ID Va lu e ( )
gaussian = SimpleITK . S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r ( )
gaussian : SetSigma ( arg [ 2 ] )
image = gaussian : Execute ( image ) ;
caster = SimpleITK . C as tI ma g eF il te r ( ) ;
caster : S e t O u t p u t P i x e l T y p e ( input PixelTy pe ) ;
image = caster : Execute ( image )
writer = SimpleITK . I ma ge Fi l eW ri te r ( )
writer : SetFileName ( arg [ 3 ] )
writer : Execute ( image ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

136 / 142

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

import SimpleITK as sitk


import sys
if len ( sys . argv ) < 4 :
print " Usage : Sim pleGauss ian < input > < sigma > < output > " ;
sys . exit ( 1 )
reader = sitk . I ma ge Fi l eR ea de r ( )
reader . SetFileName ( sys . argv [ 1 ] )
image = reader . Execute ( )
pixelID = image . G et Pi xe l ID Va lu e ( )
gaussian = sitk . S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r ( )
gaussian . SetSigma ( float ( sys . argv [ 2 ] ) )
image = gaussian . Execute ( image )
caster = sitk . C as tI ma g eF il te r ( )
caster . S e t O u t p u t P i x e l T y p e ( pixelID )
image = caster . Execute ( image )
writer = sitk . I ma ge Fi l eW ri te r ( )
writer . SetFileName ( sys . argv [ 3 ] )
writer . Execute ( image ) ;

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

137 / 142

Tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

if { $argc < 3 } {
puts " Usage: Simpl eGaussia n < input > < sigma > < output > "
exit 1
}
I ma ge Fi l eR ea de r reader
reader SetFileName [ lindex $argv 0 ]
set image [ reader Execute ]
set pixelID [ $image G et P ix el ID V al ue ]
S m o o t h i n g R e c u r s i v e G a u s s i a n I m a g e F i l t e r gaussian
gaussian SetSigma [ lindex $argv 1 ]
set image [ gaussian Execute $image ]
Ca s tI ma ge F il te r caster
caster S e t O u t p u t P i x e l T y p e $pixelID
set image [ caster Execute $image ]
Im a ge Fi le W ri te r writer
writer SetFileName [ lindex $argv 2 ]
writer Execute $image
# Tcl r e q u i r e s e x p l i c i t cleanup Cleanup
reader -delete
gaussian -delete
caster -delete
$image -delete
writer -delete

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

138 / 142

Conclusion

Conclusion

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

139 / 142

Wrap-up

Gentle introduction to ITK


Introduce SimpleITK
Provide hands-on experience
Problem solving, not direction following

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

140 / 142

Where to go next
Some resources for using and extending SimpleITK
Home Page
http://www.simpleitk.org
Documentation
http://www.itk.org/SimpleITKDoxygen/html/
Conventions
http:
//www.itk.org/SimpleITKDoxygen/html/Conventions.html
Contributions
http://www.itk.org/SimpleITKDoxygen/html/Developer.html

(Insight Software Consortium)

SimpleITK - MICCAI 2011

Sept 2011

141 / 142

You might also like