NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter
National Alliance for Medical Image Computing ITK Workshop – Extending the Toolkit Filters Anatomy –Class HierarchyClass Hierarchy –InputsInputs –OutputsOutputs UnaryFunctorFilter –Casting ExampleCasting Example –Division by 2 ExampleDivision by 2 Example –Functor with parameters ExampleFunctor with parameters Example Regions and Iterators –Defining properties of the Output ImageDefining properties of the Output Image –Allocating the outputAllocating the output –Using IteratorsUsing Iterators
National Alliance for Medical Image Computing Anatomy of an ITK Filter Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing The Class Hierarchy itk::ProcessObject itk::DataObject itk::Object itk::ImageBase itk::Image itk::ImageSource itk::ImageToImageFilter
National Alliance for Medical Image Computing The Class Hierarchy itk::InPlaceImageFilter itk::UnaryFunctorImageFilteritk::BinaryFunctorImageFilter itk::TernaryFunctorImageFilter itk::ImageToImageFilter
National Alliance for Medical Image Computing Filter Typical Elements Input Image Output Image Filter Parameters
National Alliance for Medical Image Computing InPlace Filter Elements Input Image Output Image Filter Parameters
National Alliance for Medical Image Computing The Unary Functor Image Filter Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing Image Filter Hierarchy template class ImageToImageFilter : public ImageSource {…}; template class InPlaceToImageFilter : public ImageToImageFilter {…}; template class ImageSource : public ProcessObject {…};
National Alliance for Medical Image Computing Unary Functor Filter itk::UnaryFunctorImageFilter Pixel-Wise Image Filter Input ImageOutput Image
National Alliance for Medical Image Computing 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 Unary Functor Filter Filter Functor template class UnaryFunctorImageFilter : public InPlaceImageFilter { private: TFunctor m_Functor; };
National Alliance for Medical Image Computing Exercise 23 Insight Toolkit - Advanced Course Create an Image Filter that performs Casting
National Alliance for Medical Image Computing namespace itk { namespace Functor { template class Cast { public: Cast() {}; ~Cast() {}; inline TOutput operator()( const TInput & A ) { return static_cast ( A ); } }; } // end of Functor namespace Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing Unary Functor Filter Example : Casting #include itkUnaryFunctorImageFilter.h template class MyFunctorImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Cast > { …
National Alliance for Medical Image Computing public: typedef MyFunctorImageFilter Self; typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Cast > Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; itkNewMacro( Self ); itkTypeMacro( MyFunctorImageFilter, UnaryFunctorImageFilter ); Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing Typical Declarations: Traits and Macros Traits Self Superclass Pointer ConstPointer Macros NewMacro TypeMacro
National Alliance for Medical Image Computing 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 #include MyFunctorImageFilter.h int main() { typedef itk::Image InputImageType; typedef itk::Image OutputImageType; typedef itk::MyFunctorImageFilter FilterType; FilterType::Pointer filter = FilterType::New(); } Unary Functor Filter Example : Casting
National Alliance for Medical Image Computing Exercise 24 Insight Toolkit - Advanced Course Create an Image Filter that divides all the intensity values by 2
National Alliance for Medical Image Computing NOTE Insight Toolkit - Advanced Course The use of itk::NumericTraits<> and RealType and typename
National Alliance for Medical Image Computing namespace itk { namespace Functor { template class Divider { public: Divider() {}; ~Divider() {}; inline TOutput operator()( const TInput & A ) { typedef typename NumericTraits ::RealType InputRealType; return static_cast ( InputRealType( A ) / 2.0 ); } }; } // end of Functor namespace Exercise
National Alliance for Medical Image Computing Exercise #include itkUnaryFunctorImageFilter.h template class DividerByTwoImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Divider > { public: typedef MyFunctorImageFilter Self; typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Divider > Superclass; …
National Alliance for Medical Image Computing 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 namespace itk { namespace Functor { template class Divider { public: Divider() {}; ~Divider() {}; typedef typename NumericTraits ::RealType InputRealType; inline TOutput operator()( const TInput & A ) { return static_cast ( 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 Exercise : Functors with parameters template class DividerImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Divider > {…{… 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 #include DividerImageFilter.h int main() { typedef itk::Image InputImageType; typedef itk::Image OutputImageType; typedef itk::DividerImageFilter FilterType; FilterType::Pointer filter = FilterType::New(); filter->SetDivisor( 7.5 ); filter->Update(); } Exercise : Functors with parameters
National Alliance for Medical Image Computing Image Regions Insight Toolkit - Advanced Course
National Alliance for Medical Image Computing Insight Toolkit – Image Regions LargestPossibleRegion BufferedRegion RequestedRegion
National Alliance for Medical Image Computing Filter Typical Elements Input Image Output Image Filter Parameters Region
National Alliance for Medical Image Computing Insight Toolkit – Image Regions Input Region Output Region Input ImageOutput Image
National Alliance for Medical Image Computing Generate Output Information Method Filter virtual void GenerateOutputInformation() { Superclass::GenerateOutputInformation(); OutputImagePointer outputPtr = this->GetOutput(); outputPtr->SetLargestPossibleRegion(…); outputPtr->SetSpacing(…); outputPtr->SetOrigin(…); }
National Alliance for Medical Image Computing 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 Exercise 26 Insight Toolkit - Advanced Course Create an Image Filter that extracts the first quadrant of an image
National Alliance for Medical Image Computing Quadrant Extract Image Filter Example #include itkImageToImageFilter.h template class FirstQuadrantExtractImageFilter : public ImageToImageFilter { public: typedef FirstQuadrantExtractImageFilter Self; typedef ImageToImageFilter Superclass; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; itkNewMacro( Self ); itkTypeMacro( FirstQuadrantExtractImageFilter, ImageToImageFilter ); …
National Alliance for Medical Image Computing 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 GenerateOutputInformation() Method Call Superclass::GenerateOutputInformation() Set Parameters of the Output Image Spacing Origin Direction LargestPossibleRegion
National Alliance for Medical Image Computing GenerateOutputInformation() Method template 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 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 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 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 GenerateData() Method template void GenerateData() { ImageConstPointer inputImage = this->GetInput(); ImagePointer outputImage = this->GetOutput(); RegionType outputRegion = outputImage->GetLargestPossibleRegion(); outputImage->SetRegions( outputRegion ); outputImage->Allocate(); typedef ImageRegionIterator ImageIterator; typedef ImageRegionConstIterator ImageConstIterator; ImageIterator outputIterator( outputImage, outputRegion ); ImageConstIterator inputIterator( inputImage, outputRegion );
National Alliance for Medical Image Computing Insight Toolkit – Image Regions First Quadrant Input Image Region Output Image Region
National Alliance for Medical Image Computing GenerateData() Method inputIterator.GoToBegin(); outputIterator.GoToBegin(); while( ! outputIterator.IsAtEnd() ) { outputIterator.Set( inputIterator.Get() ); ++inputIterator; ++outputIterator; }
National Alliance for Medical Image Computing Exercise 26b Insight Toolkit - Advanced Course Modify the code for extracting the central thirds an image
National Alliance for Medical Image Computing Insight Toolkit – Image Regions Input Image Region Output Image Region Central Third
National Alliance for Medical Image Computing END Insight Toolkit - Advanced Course