Title: ITK Workshop
1ITK Workshop
Writing a New ITK Filter
2ITK Workshop Extending the Toolkit
- Filters Anatomy
- Class Hierarchy
- Inputs
- Outputs
- UnaryFunctorFilter
- Casting Example
- Division by 2 Example
- Functor with parameters Example
- Regions and Iterators
- Defining properties of the Output Image
- Allocating the output
- Using Iterators
3Insight Toolkit - Advanced Course
4The Class Hierarchy
itkObject
itkProcessObject
itkDataObject
itkImageBase
itkImageSource
itkImage
itkImageToImageFilter
5The Class Hierarchy
itkImageToImageFilter
itkInPlaceImageFilter
itkUnaryFunctorImageFilter
itkBinaryFunctorImageFilter
itkTernaryFunctorImageFilter
6Filter Typical Elements
Filter
Input Image
Output Image
Parameters
7InPlace Filter Elements
Filter
Input Image
Output Image
Parameters
8Insight Toolkit - Advanced Course
- The Unary Functor Image Filter
9Image Filter Hierarchy
template ltclass TOutputImagegt class ImageSource
public ProcessObject
template ltclass TInputImage, class
TOutputImagegt class ImageToImageFilter public
ImageSourcelt TOutputImage gt
template ltclass TInputImage, class
TOutputImagegt class InPlaceToImageFilter
public ImageToImageFilterlt TOutputImage ,
TOutputImage gt
10Unary Functor Filter
itkUnaryFunctorImageFilter
Pixel-Wise Image Filter
Input Image
Output Image
11Unary Functor Filter
- It should be enough to specify the
- operation to be applied on each pixel
That is the role of the FUNCTOR
12Unary Functor Filter
Functor
Filter
template ltclass TInputImage, class TOutputImage,
class TFunctorgt class UnaryFunctorImageFilter
public InPlaceImageFilterlt TInputImage,
TOutputImage gt private TFunctor
m_Functor
13Insight Toolkit - Advanced Course
Create an Image Filter that performs Casting
14Unary Functor Filter Example Casting
namespace itk namespace Functor
templatelt class TInput, class TOutputgt class
Cast public Cast()
Cast() inline TOutput operator()(
const TInput A ) return
static_castltTOutputgt( A )
// end of Functor namespace
15Unary Functor Filter Example Casting
include itkUnaryFunctorImageFilter.h template
ltclass TInputImage, class TOutputImagegt class
MyFunctorImageFilter public
UnaryFunctorImageFilterlt TInputImage,TOutputImage,
FunctorCastlt typename
TInputImagePixelType,
typename TOutputImagePixelTypegt gt
16Unary Functor Filter Example Casting
public typedef MyFunctorImageFilter
Self typedef UnaryFunctorImageFilterlt
TInputImage, TOutputImage,
FunctorCastlt typename TInputImagePixelType,
typename
TOutputImagePixelType gt
gt Superclass
typedef SmartPointerlt Self gt Pointer
typedef SmartPointerlt const Self gt
ConstPointer itkNewMacro( Self )
itkTypeMacro( MyFunctorImageFilter,
UnaryFunctorImageFilter )
17Typical Declarations Traits and Macros
- Traits
- Self
- Superclass
- Pointer
- ConstPointer
- Macros
- NewMacro
- TypeMacro
18Unary Functor Filter Example Casting
protected MyFunctorImageFilter() virtual
MyFunctorImageFilter() private
MyFunctorImageFilter( const Self ) //
purposely not implemented void operator(
const Self ) // purposely not
implemented // end of class // end of
namespace itk
19Unary Functor Filter Example Casting
include MyFunctorImageFilter.h int main()
typedef itkImagelt unsigned char, 2 gt
InputImageType typedef itkImagelt unsigned
short, 2 gt OutputImageType typedef
itkMyFunctorImageFilterlt
InputImageType,
OutputImageType
gt FilterType
FilterTypePointer filter FilterTypeNew()
20Insight Toolkit - Advanced Course
Create an Image Filter that divides all the
intensity values by 2
21Insight Toolkit - Advanced Course
The use of itkNumericTraitsltgt
and RealType and typename
22Exercise
namespace itk namespace Functor
templatelt class TInput, class TOutputgt class
Divider public Divider()
Divider() inline TOutput operator()(
const TInput A ) typedef typename
NumericTraitsltTInputgtRealType InputRealType
return static_castltTOutputgt( InputRealType( A )
/ 2.0 ) // end of Functor
namespace
23Exercise
include itkUnaryFunctorImageFilter.h templatelt
class TInputImage, class TOutputImagegt class
DividerByTwoImageFilter public
UnaryFunctorImageFilterlt TInputImage,TOutputImage,
FunctorDividerlt typename
TInputImagePixelType,
typename TOutputImagePixelTypegt
gt public typedef MyFunctorImageFilter
Self typedef UnaryFunctorImageFilterlt
TInputImage, TOutputImage,
FunctorDividerlt typename TInputImagePixelType,
typename
TOutputImagePixelTypegt
gt Superclass
24Insight Toolkit - Advanced Course
Create an Image Filter that divides all the
intensity values by a given value
25Exercise Functors with parameters
namespace itk namespace Functor templatelt
class TInput, class TOutputgt class Divider
public Divider() Divider()
typedef typename NumericTraitsltTInputgtRealType
InputRealType inline TOutput operator()(
const TInput A ) return
static_castltTOutputgt( InputRealType( A ) /
m_Divisor ) void SetDivisor( const
InputRealType value )
m_Divisor value private
InputRealType m_Divisor
26Exercise Functors with parameters
templateltclass TInputImage, class
TOutputImagegt class DividerImageFilter
public UnaryFunctorImageFilterlt
TInputImage,TOutputImage,
Dividerlt typename TInputImagePixelType,
typename TOutputImagePixelT
ypegt gt public typedef typename
SuperclassFunctorType FunctorType
typedef typename FunctorTypeInputRealType
InputRealType void SetDivisor( const
InputRealType value )
this-gtGetFunctor().SetDivisor( value )
27Exercise Functors with parameters
include DividerImageFilter.h int main()
typedef itkImagelt unsigned char, 2 gt
InputImageType typedef itkImagelt unsigned
short, 2 gt OutputImageType typedef
itkDividerImageFilterlt
InputImageType,
OutputImageType
gt FilterType
FilterTypePointer filter FilterTypeNew()
filter-gtSetDivisor( 7.5 )
filter-gtUpdate()
28Insight Toolkit - Advanced Course
29Insight Toolkit Image Regions
30Filter Typical Elements
Filter
Input Image
Output Image
Region
Region
Parameters
31Insight Toolkit Image Regions
Input Image
Output Image
Input Region
Output Region
32Generate Output Information Method
Filter
virtual voidGenerateOutputInformation()
SuperclassGenerateOutputInformation()
OutputImagePointer outputPtr
this-gtGetOutput() outputPtr-gtSetLargest
PossibleRegion() outputPtr-gtSetSpacing()
outputPtr-gtSetOrigin()
33Generate Output Information Method
This method configures the Output Image
- Spacing
- Origin
- Orientation (Direction)
- LargestPossibleRegion
by default it copies meta data from the Input
34Insight Toolkit - Advanced Course
Create an Image Filter that extractsthe first
quadrant of an image
35Quadrant Extract Image Filter Example
include itkImageToImageFilter.h templateltclass
TInputImagegt class FirstQuadrantExtractImageFilte
r public ImageToImageFilterlt
TInputImage,TInputImage gt public typedef
FirstQuadrantExtractImageFilter Self
typedef ImageToImageFilterlt TInputImage,
TInputImage gt Superclass typedef
SmartPointerlt Self gt Pointer typedef
SmartPointerlt const Self gt ConstPointer
itkNewMacro( Self ) itkTypeMacro(
FirstQuadrantExtractImageFilter,
ImageToImageFilter )
36Quadrant Extract Image Filter Example
typedef typename TInputImageRegionType
RegionTypetypedef typename
TInputImageSizeType SizeTypetypedef
typename TInputImageIndexType
IndexType typedef typename
TInputImagePointer ImagePointertypedef
typename TInputImageConstPointer
ImageConstPointer protected
FirstQuadrantExtractImageFilter()
FirstQuadrantExtractImageFilter() void
PrintSelf( stdostream os, Indent indent)
const void GenerateOutputInformation()
void GenerateData()
37GenerateOutputInformation() Method
- Call SuperclassGenerateOutputInformation()
- Set Parameters of the Output Image
- Spacing
- Origin
- Direction
- LargestPossibleRegion
38GenerateOutputInformation() Method
templatelt class TInputImage gtvoid
GenerateOutputInformation()
SuperclassGenerateOutputInformation()
ImageConstPointer inputImage
this-gtGetInput() ImagePointer
outputImage this-gtGetOutput() RegionType
inputRegion inputImage-gtGetLargestPossibleRegion
() IndexType inputStart
inputRegion.GetIndex() SizeType inputSize
inputRegion.GetSize()
39GenerateOutputInformation() Method
IndexType outputStart SizeType
outputSize const unsigned int Dimension
TInputImageImageDimension for( unsigned
int i 0 i lt Dimension i )
outputSizei inputSizei / 2
outputStarti inputStarti
RegionType outputRegion outputRegion.SetIndex(
outputStart ) outputRegion.SetSize(
outputSize )
40GenerateOutputInformation() Method
outputImage-gtSetLargestPossibleRegion(
outputRegion ) outputImage-gtSetSpacing(
inputImage-gtGetSpacing() ) outputImage-gtSetOrig
in( inputImage-gtGetOrigin() ) outputImage-gtSetD
irection( inputImage-gtGetDirection() ) //
end of GenerateOutputInformation() method
41GenerateData() Method
- Get Input and Output Image pointers
- Invokes GetInput()
- Invokes GetOutput()
- Allocate memory for the Output Image (s)
- Invokes SetRegions()
- Invokes Allocate()
- Computes pixels of Output Image
- Usually requires Image Iterators
42GenerateData() Method
templatelt class TInputImage gtvoid
GenerateData() ImageConstPointer inputImage
this-gtGetInput() ImagePointer
outputImage this-gtGetOutput() RegionType
outputRegion outputImage-gtGetLargestPossibleRegi
on() outputImage-gtSetRegions( outputRegion
) outputImage-gtAllocate() typedef
ImageRegionIteratorlt TInputImage gt
ImageIterator typedef ImageRegionConstIteratorlt
TInputImage gt ImageConstIterator
ImageIterator outputIterator( outputImage,
outputRegion ) ImageConstIterator
inputIterator( inputImage, outputRegion )
43Insight Toolkit Image Regions
OutputImageRegion
InputImageRegion
First Quadrant
44GenerateData() Method
inputIterator.GoToBegin() outputIterator.GoToBegi
n() while( ! outputIterator.IsAtEnd() )
outputIterator.Set( inputIterator.Get() )
inputIterator outputIterator
45Insight Toolkit - Advanced Course
Modify the code for extractingthe central thirds
an image
46Insight Toolkit Image Regions
OutputImageRegion
Central Third
InputImageRegion
47Insight Toolkit - Advanced Course