Professional Documents
Culture Documents
Sept 2011
1 / 142
Sept 2011
3 / 142
Program
Sept 2011
4 / 142
Tutorial Goals
Sept 2011
13 / 142
ITK Overview
Sept 2011
14 / 142
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sept 2011
15 / 142
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 )
Sept 2011
16 / 142
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 )
Sept 2011
16 / 142
Goals of SimpleITK
Sept 2011
17 / 142
Sept 2011
18 / 142
Templates in ITK
1
2
3
4
5
6
7
8
9
10
11
Sept 2011
19 / 142
Template Freedom
1
2
3
4
5
6
7
8
9
10
11
Sept 2011
20 / 142
Programming Models
Object oriented
Function oriented
More about this later
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. . .
Sept 2011
22 / 142
Image Anatomy
Sept 2011
23 / 142
Filter Anatomy
Sept 2011
24 / 142
Basic Tutorial
Basic Tutorial
Sept 2011
25 / 142
Sept 2011
33 / 142
Image Class
Creation
Number of dimensions, size, origin, spacing
Pixel access
Back to iPython (Examples/BasicTutorial1/Image.py)
Sept 2011
34 / 142
1
2
3
4
5
Sept 2011
35 / 142
1
2
3
4
# Addressing pixels
image . GetPixel ( 0 , 0 , 0 )
image . SetPixel ( 0 , 0 , 0 , 1 )
image . GetPixel ( 0 , 0 , 0 )
Sept 2011
36 / 142
1
2
3
4
# Addressing pixels
image [ 0 , 0 , 0 ]
image [ 0 , 0 , 0 ] = 10
image [ 0 , 0 , 0 ]
Sept 2011
37 / 142
Summary
Sept 2011
38 / 142
Memory Management
Memory Management
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)
Sept 2011
40 / 142
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sept 2011
41 / 142
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
...
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...
Sept 2011
43 / 142
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
Sept 2011
44 / 142
Basic Tutorial 2
Basic Tutorial 2
Sept 2011
50 / 142
Hands On
Hands On
Examples/BasicTutorial2/Filters.py
Sept 2011
51 / 142
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 !
...
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 " )
Sept 2011
53 / 142
1
2
3
4
# Extract
size = [ 6 4 , 6 4 , 1 ]
start = [ 6 4 , 0 , 0 ]
sitk . Show ( sitk . Extract ( image , size , start ) , " Extracted " )
Sept 2011
54 / 142
Pixel-wise Operators
1
2
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
Sept 2011
58 / 142
Operator Example
1
2
3
4
5
Sept 2011
59 / 142
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 . """
Sept 2011
68 / 142
Python Example
To iPython (Examples/InteractiveTutorial/05-02-Numpy.py)
Sept 2011
69 / 142
Image Measurements
Image Measurements
Sept 2011
70 / 142
The Scenerio
Sept 2011
71 / 142
Python Example
To iPython (Examples/InteractiveTutorial/MeasureRegions.py)
Sept 2011
72 / 142
Feature Detection
Feature Detection
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 ) ;
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 ) ;
Sept 2011
76 / 142
Advanced Topics
Advanced Topics
Sept 2011
81 / 142
Or as part of Slicer
ITK_VERSION_MAJOR=4
Slicer_USE_SimpleITK=ON
Sept 2011
82 / 142
Sept 2011
83 / 142
Supported Platforms
Sept 2011
84 / 142
SimpleITK Architecture
SimpleITK Architecture
Sept 2011
85 / 142
Filter Anatomy
Sept 2011
86 / 142
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 >
Label Types
uint8 t
uint16 t
uint32 t
Sept 2011
87 / 142
Filter Anatomy
Sept 2011
88 / 142
Using Filters
Using Filters
Sept 2011
89 / 142
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 ) ;
Sept 2011
90 / 142
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 ) ;
Sept 2011
91 / 142
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 ) ;
Sept 2011
92 / 142
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 ) ;
Sept 2011
93 / 142
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
);
Sept 2011
94 / 142
Code Philosophy
Code Philosophy
Sept 2011
95 / 142
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 ( ) ;
Sept 2011
96 / 142
1
2
Sept 2011
97 / 142
1
2
3
4
5
6
7
8
Get/Set parameters
Set methods always return Self & (more later)
Generally, a direct mapping to ITK
Boolean parameters generate On and O methods
Sept 2011
98 / 142
1
2
3
4
5
Sept 2011
99 / 142
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 ) ;
Sept 2011
100 / 142
Questions?
Questions?
Sept 2011
101 / 142
Sept 2011
102 / 142
To ITK
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 ;
Sept 2011
103 / 142
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 ) ;
Sept 2011
104 / 142
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 ) ;
Sept 2011
105 / 142
1
2
// Update p i p e l i n e
pad >Update ( ) ;
Sept 2011
106 / 142
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 ( ) ) ;
Sept 2011
107 / 142
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 ) ;
Sept 2011
108 / 142
To ITK Solution
~/Source/AdvancedTutorial-build/ToITK/ToITKSolution \
~/Source/SimpleITK/Testing/Data/Input/RA-Float.nrrd \
/tmp/foo.nii
Sept 2011
109 / 142
Simple Gaussian in 8
languages
AdvancedTutorial/SimpleGaussian/SimpleGaussian.*
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
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
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
#
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 )
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)
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)
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
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
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
Sept 2011
138 / 142
Conclusion
Conclusion
Sept 2011
139 / 142
Wrap-up
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
Sept 2011
141 / 142