Extending the Insight Toolkit: Creating New ITK Filters for Image Processing
This workshop focuses on enhancing the Insight Toolkit (ITK) by teaching participants how to write new image filters. Attendees will learn about the anatomy of filters, class hierarchies, and the usage of iterators in ITK. Key topics include implementing unary and binary functor filters, handling inputs/outputs, allocating output images, and applying operations on image pixels. Practical exercises will involve creating filters that perform pixel casting and division to reinforce the concepts learned.
Extending the Insight Toolkit: Creating New ITK Filters for Image Processing
E N D
Presentation Transcript
ITK Workshop Writing a New ITK Filter October 5-8, 2005
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
Insight Toolkit - Advanced Course Anatomy of an ITK Filter
The Class Hierarchy itk::Object itk::ProcessObject itk::DataObject itk::ImageBase itk::ImageSource itk::Image itk::ImageToImageFilter
The Class Hierarchy itk::ImageToImageFilter itk::InPlaceImageFilter itk::UnaryFunctorImageFilter itk::BinaryFunctorImageFilter itk::TernaryFunctorImageFilter
Filter Typical Elements Filter Input Image Output Image Parameters
InPlace Filter Elements Filter Input Image Output Image Parameters
Insight Toolkit - Advanced Course The Unary Functor Image Filter
Image Filter Hierarchy template <class TOutputImage> class ImageSource : public ProcessObject {…}; template <class TInputImage, class TOutputImage> class ImageToImageFilter : public ImageSource< TOutputImage > {…}; template <class TInputImage, class TOutputImage> class InPlaceToImageFilter : public ImageToImageFilter< TOutputImage , TOutputImage > {…};
Unary Functor Filter itk::UnaryFunctorImageFilter Pixel-Wise Image Filter Input Image Output Image
Unary Functor Filter It should be enough to specify the operation to be applied on each pixel That is the role of the FUNCTOR
Unary Functor Filter Functor Filter template <class TInputImage, class TOutputImage, class TFunctor> class UnaryFunctorImageFilter : public InPlaceImageFilter< TInputImage, TOutputImage >{ private: TFunctor m_Functor; };
Insight Toolkit - Advanced Course Exercise 23 Create an Image Filter that performs Casting
Unary Functor Filter Example : Casting namespace itk { namespace Functor { template< class TInput, class TOutput> class Cast{ public:Cast() {};~Cast() {}; inlineTOutput operator()( const TInput & A ) { returnstatic_cast<TOutput>( A ); } }; } // end of Functor namespace
Unary Functor Filter Example : Casting #include “itkUnaryFunctorImageFilter.h” template<class TInputImage, class TOutputImage> class MyFunctorImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Cast< typenameTInputImage::PixelType,typenameTOutputImage::PixelType> > { …
Unary Functor Filter Example : Casting public: typedefMyFunctorImageFilterSelf; typedefUnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Cast< typenameTInputImage::PixelType,typenameTOutputImage::PixelType > > Superclass; typedef SmartPointer< Self > Pointer;typedef SmartPointer< const Self > ConstPointer; itkNewMacro( Self ); itkTypeMacro( MyFunctorImageFilter, UnaryFunctorImageFilter );
Typical Declarations: Traits and Macros • Traits • Self • Superclass • Pointer • ConstPointer • Macros • NewMacro • TypeMacro
Unary 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
Unary Functor Filter Example : Casting #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(); }
Insight Toolkit - Advanced Course Exercise 24 Create an Image Filter that divides all the intensity values by 2
Insight Toolkit - Advanced Course NOTE The use of itk::NumericTraits<> and RealType and typename
Exercise namespace itk { namespace Functor { template< class TInput, class TOutput> class Divider { public: Divider() {}; ~Divider() {}; inlineTOutput operator()( const TInput & A ) { typedef typename NumericTraits<TInput>::RealType InputRealType; returnstatic_cast<TOutput>( InputRealType( A ) / 2.0 ); } }; } // end of Functor namespace
Exercise #include “itkUnaryFunctorImageFilter.h” template<class TInputImage, class TOutputImage> class DividerByTwoImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Divider< typenameTInputImage::PixelType, typenameTOutputImage::PixelType> > { public: typedefMyFunctorImageFilterSelf; typedefUnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Divider< typenameTInputImage::PixelType, typenameTOutputImage::PixelType> > Superclass; …
Insight Toolkit - Advanced Course Exercise 25 Create an Image Filter that divides all the intensity values by a given value
Exercise : Functors with parameters namespace itk { namespace Functor { template< class TInput, class TOutput> class Divider { public: Divider() {};~Divider() {}; typedef typename NumericTraits<TInput>::RealType InputRealType; inlineTOutput operator()( const TInput & A ) {returnstatic_cast<TOutput>( InputRealType( A ) / m_Divisor ); } void SetDivisor( const InputRealType & value ) { m_Divisor = value; } private: InputRealTypem_Divisor; };
Exercise : Functors with parameters template<class TInputImage, class TOutputImage> class DividerImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Divider< typenameTInputImage::PixelType,typenameTOutputImage::PixelType> > {… public: typedeftypenameSuperclass::FunctorTypeFunctorType; typedeftypenameFunctorType::InputRealTypeInputRealType; void SetDivisor( const InputRealType & value ) {this->GetFunctor().SetDivisor( value ); }
Exercise : Functors with parameters #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 ); filter->Update(); }
Insight Toolkit - Advanced Course Image Regions
LargestPossibleRegion BufferedRegion RequestedRegion Insight Toolkit – Image Regions
Filter Typical Elements Filter Input Image Output Image Region Region Parameters
Insight Toolkit – Image Regions Input Image Output Image Input Region Output Region
Generate Output Information Method Filter virtual voidGenerateOutputInformation(){Superclass::GenerateOutputInformation(); OutputImagePointer outputPtr = this->GetOutput(); outputPtr->SetLargestPossibleRegion(…); outputPtr->SetSpacing(…); outputPtr->SetOrigin(…);}
Generate Output Information Method This method configures the Output Image • Spacing • Origin • Orientation (Direction) • LargestPossibleRegion by default it copies meta data from the Input
Insight Toolkit - Advanced Course Exercise 26 Create an Image Filter that extractsthe first quadrant of an image
Quadrant Extract Image Filter Example #include “itkImageToImageFilter.h” template<class TInputImage> class FirstQuadrantExtractImageFilter : public ImageToImageFilter< TInputImage,TInputImage > { public: typedefFirstQuadrantExtractImageFilterSelf; typedefImageToImageFilter< TInputImage, TInputImage > Superclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; itkNewMacro( Self ); itkTypeMacro( FirstQuadrantExtractImageFilter, ImageToImageFilter ); …
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();
GenerateOutputInformation() Method • Call Superclass::GenerateOutputInformation() • Set Parameters of the Output Image • Spacing • Origin • Direction • LargestPossibleRegion
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();
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 );
GenerateOutputInformation() Method outputImage->SetLargestPossibleRegion( outputRegion); outputImage->SetSpacing( inputImage->GetSpacing() ); outputImage->SetOrigin( inputImage->GetOrigin() ); outputImage->SetDirection( inputImage->GetDirection() ); } // end of GenerateOutputInformation() method
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
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 );
Insight Toolkit – Image Regions OutputImageRegion InputImageRegion First Quadrant
GenerateData() Method inputIterator.GoToBegin(); outputIterator.GoToBegin(); while( ! outputIterator.IsAtEnd() ) { outputIterator.Set( inputIterator.Get() ); ++inputIterator; ++outputIterator; }
Insight Toolkit - Advanced Course Exercise 26b Modify the code for extractingthe central thirds an image
Insight Toolkit – Image Regions OutputImageRegion Central Third InputImageRegion