Download - ITK Workshop
NA-MICNational Alliance for Medical Image Computing http://na-mic.org
ITK Workshop
October 5-8, 2005
Writing a New ITK Filter
National Alliance for Medical Image Computing http://na-mic.org
ITK 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
National Alliance for Medical Image Computing http://na-mic.org
Anatomy of an ITK Filter
Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing http://na-mic.org
The Class Hierarchy
itk::ProcessObjectitk::DataObject
itk::Object
itk::ImageBase
itk::Image
itk::ImageSource
itk::ImageToImageFilter
National Alliance for Medical Image Computing http://na-mic.org
The Class Hierarchy
itk::InPlaceImageFilter
itk::UnaryFunctorImageFilter itk::BinaryFunctorImageFilter
itk::TernaryFunctorImageFilter
itk::ImageToImageFilter
National Alliance for Medical Image Computing http://na-mic.org
Filter Typical Elements
InputImage
OutputImage
Filter
Parameters
National Alliance for Medical Image Computing http://na-mic.org
InPlace Filter Elements
InputImage
OutputImage
Filter
Parameters
National Alliance for Medical Image Computing http://na-mic.org
The Unary Functor Image Filter
Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing http://na-mic.org
Image Filter Hierarchy
template <class TInputImage, class TOutputImage>
class ImageToImageFilter : public ImageSource< TOutputImage >
{…};
template <class TInputImage, class TOutputImage>
class InPlaceToImageFilter :
public ImageToImageFilter< TOutputImage , TOutputImage >
{…};
template <class TOutputImage>
class ImageSource : public ProcessObject
{…};
National Alliance for Medical Image Computing http://na-mic.org
Unary Functor Filter
itk::UnaryFunctorImageFilter
Pixel-WiseImageFilter
Input Image Output Image
National Alliance for Medical Image Computing http://na-mic.org
It should be enough to specify the
operation to be applied on each pixel
Unary Functor Filter
That is the role of the
FUNCTOR
National Alliance for Medical Image Computing http://na-mic.org
Unary Functor Filter
Filter
Functor
template <class TInputImage, class TOutputImage, class TFunctor>
class UnaryFunctorImageFilter :
public InPlaceImageFilter< TInputImage, TOutputImage >{
private:
TFunctor m_Functor;
};
National Alliance for Medical Image Computing http://na-mic.org
Exercise 23
Insight Toolkit - Advanced Course
Create an Image Filter that
performs Casting
National Alliance for Medical Image Computing http://na-mic.org
namespace itk {
namespace Functor {
template< class TInput, class TOutput> class Cast { public: Cast() {}; ~Cast() {}; inline TOutput operator()( const TInput & A ) { return static_cast<TOutput>( A ); } };
} // end of Functor namespace
Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing http://na-mic.org
Unary Functor Filter Example : Casting
#include “itkUnaryFunctorImageFilter.h”
template<class TInputImage, class TOutputImage>class MyFunctorImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Cast< typename TInputImage::PixelType, typename TOutputImage::PixelType> > {
…
National Alliance for Medical Image Computing http://na-mic.org
public: typedef MyFunctorImageFilter Self; typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Cast< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass;
typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer;
itkNewMacro( Self );
itkTypeMacro( MyFunctorImageFilter, UnaryFunctorImageFilter );
Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing http://na-mic.org
Typical Declarations: Traits and Macros
• Traits
• Self
• Superclass
• Pointer
• ConstPointer
• Macros
• NewMacro
• TypeMacro
National Alliance for Medical Image Computing http://na-mic.org
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
Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing http://na-mic.org
#include “MyFunctorImageFilter.h”
int main() { typedef itk::Image< unsigned char, 2 > InputImageType; typedef itk::Image< unsigned short, 2 > OutputImageType;
typedef itk::MyFunctorImageFilter< InputImageType, OutputImageType > FilterType; FilterType::Pointer filter = FilterType::New();
}
Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing http://na-mic.org
Exercise 24
Insight Toolkit - Advanced Course
Create an Image Filter that divides all the intensity values by 2
National Alliance for Medical Image Computing http://na-mic.org
NOTE
Insight Toolkit - Advanced Course
The use of itk::NumericTraits<>
and RealType
and typename
National Alliance for Medical Image Computing http://na-mic.org
namespace itk {
namespace Functor {
template< class TInput, class TOutput> class Divider { public: Divider() {}; ~Divider() {}; inline TOutput operator()( const TInput & A ) { typedef typename NumericTraits<TInput>::RealType InputRealType; return static_cast<TOutput>( InputRealType( A ) / 2.0 ); } };
} // end of Functor namespace
Exercise
National Alliance for Medical Image Computing http://na-mic.org
Exercise
#include “itkUnaryFunctorImageFilter.h”
template<class TInputImage, class TOutputImage>class DividerByTwoImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> >{public:
typedef MyFunctorImageFilter Self;
typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> > Superclass;…
National Alliance for Medical Image Computing http://na-mic.org
Exercise 25
Insight Toolkit - Advanced Course
Create an Image Filter that divides all the intensity values by a given value
National Alliance for Medical Image Computing http://na-mic.org
namespace itk { namespace Functor { template< class TInput, class TOutput> class Divider { public: Divider() {}; ~Divider() {}; typedef typename NumericTraits<TInput>::RealType InputRealType; inline TOutput operator()( const TInput & A ) { return static_cast<TOutput>( InputRealType( A ) / m_Divisor ); } void SetDivisor( const InputRealType & value ) { m_Divisor = value; } private: InputRealType m_Divisor; };
Exercise : Functors with parameters
National Alliance for Medical Image Computing http://na-mic.org
Exercise : Functors with parameters
template<class TInputImage, class TOutputImage>class DividerImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> >{…public: typedef typename Superclass::FunctorType FunctorType; typedef typename FunctorType::InputRealType InputRealType;
void SetDivisor( const InputRealType & value ) { this->GetFunctor().SetDivisor( value ); }
National Alliance for Medical Image Computing http://na-mic.org
#include “DividerImageFilter.h”
int main() { typedef itk::Image< unsigned char, 2 > InputImageType; typedef itk::Image< unsigned short, 2 > OutputImageType;
typedef itk::DividerImageFilter< InputImageType, OutputImageType > FilterType; FilterType::Pointer filter = FilterType::New();
filter->SetDivisor( 7.5 );
}
Exercise : Functors with parameters
National Alliance for Medical Image Computing http://na-mic.org
Image Regions
Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing http://na-mic.org
Insight Toolkit – Image Regions
LargestPossibleRegion
BufferedRegion
RequestedRegion
National Alliance for Medical Image Computing http://na-mic.org
Filter Typical Elements
InputImage
OutputImage
Filter
Parameters
Region Region
National Alliance for Medical Image Computing http://na-mic.org
Insight Toolkit – Image Regions
InputRegion
OutputRegion
Input Image Output Image
National Alliance for Medical Image Computing http://na-mic.org
Generate Output Information Method
Filter
virtual voidGenerateOutputInformation(){ Superclass::GenerateOutputInformation();
OutputImagePointer outputPtr = this->GetOutput();
outputPtr->SetLargestPossibleRegion(…); outputPtr->SetSpacing(…); outputPtr->SetOrigin(…);}
National Alliance for Medical Image Computing http://na-mic.org
Generate Output Information Method
• Spacing
• Origin
• Orientation (Direction)
• LargestPossibleRegion
This method configures the Output Image
by default it copies meta data from the Input
National Alliance for Medical Image Computing http://na-mic.org
Exercise 26
Insight Toolkit - Advanced Course
Create an Image Filter that extractsthe first quadrant of an image
National Alliance for Medical Image Computing http://na-mic.org
Quadrant Extract Image Filter Example
#include “itkImageToImageFilter.h”
template<class TInputImage>class FirstQuadrantExtractImageFilter : public ImageToImageFilter< TInputImage,TInputImage >{public: typedef FirstQuadrantExtractImageFilter Self; typedef ImageToImageFilter< TInputImage, TInputImage > Superclass;
typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer;
itkNewMacro( Self );
itkTypeMacro( FirstQuadrantExtractImageFilter, ImageToImageFilter ); …
National Alliance for Medical Image Computing http://na-mic.org
Quadrant Extract Image Filter Example
typedef typename TInputImage::RegionType RegionType;typedef typename TInputImage::SizeType SizeType;typedef typename TInputImage::IndexType IndexType;
typedef typename TInputImage::Pointer ImagePointer;typedef typename TInputImage::ConstPointer ImageConstPointer;
protected:
FirstQuadrantExtractImageFilter() {}; ~FirstQuadrantExtractImageFilter() {};
void PrintSelf( std::ostream &os, Indent indent) const;
void GenerateOutputInformation();
void GenerateData();
National Alliance for Medical Image Computing http://na-mic.org
GenerateOutputInformation() Method
• Call Superclass::GenerateOutputInformation()
• Set Parameters of the Output Image
• Spacing
• Origin
• Direction
• LargestPossibleRegion
National Alliance for Medical Image Computing http://na-mic.org
GenerateOutputInformation() Method
template< class TInputImage >void GenerateOutputInformation(){ Superclass::GenerateOutputInformation();
ImageConstPointer inputImage = this->GetInput(); ImagePointer outputImage = this->GetOutput();
RegionType inputRegion = inputImage->GetLargestPossibleRegion ();
IndexType inputStart = inputRegion.GetIndex(); SizeType inputSize = inputRegion.GetSize();
National Alliance for Medical Image Computing http://na-mic.org
GenerateOutputInformation() Method
IndexType outputStart; SizeType outputSize;
const unsigned int Dimension = TInputImage::ImageDimension;
for( unsigned int i = 0; i < Dimension; i++ ) { outputSize[i] = inputSize[i] / 2; outputStart[i] = inputStart[i]; }
RegionType outputRegion;
outputRegion.SetIndex( outputStart ); outputRegion.SetSize( outputSize );
National Alliance for Medical Image Computing http://na-mic.org
GenerateOutputInformation() Method
outputImage->SetLargestPossibleRegion( outputRegion );
outputImage->SetSpacing( inputImage->GetSpacing() );
outputImage->SetOrigin( inputImage->GetOrigin() );
outputImage->SetDirection( inputImage->GetDirection() );
} // end of GenerateOutputInformation() method
National Alliance for Medical Image Computing http://na-mic.org
GenerateData() 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
National Alliance for Medical Image Computing http://na-mic.org
GenerateData() Method
template< class TInputImage >void GenerateData(){
ImageConstPointer inputImage = this->GetInput(); ImagePointer outputImage = this->GetOutput();
RegionType outputRegion = outputImage->GetLargestPossibleRegion();
outputImage->SetRegions( outputRegion ); outputImage->Allocate();
typedef ImageRegionIterator< TInputImage > ImageIterator; typedef ImageRegionConstIterator< TInputImage > ImageConstIterator;
ImageIterator outputIterator( outputImage, outputRegion ); ImageConstIterator inputIterator( inputImage, outputRegion );
National Alliance for Medical Image Computing http://na-mic.org
Insight Toolkit – Image Regions
First Quadrant
InputImageRegion
OutputImageRegion
National Alliance for Medical Image Computing http://na-mic.org
GenerateData() Method
inputIterator.GoToBegin();outputIterator.GoToBegin();
while( ! outputIterator.IsAtEnd() ) {
outputIterator.Set( inputIterator.Get() );
++inputIterator; ++outputIterator;
}
National Alliance for Medical Image Computing http://na-mic.org
Exercise 26b
Insight Toolkit - Advanced Course
Modify the code for extractingthe central thirds an image
National Alliance for Medical Image Computing http://na-mic.org
Insight Toolkit – Image Regions
InputImageRegion
OutputImageRegion
Central Third