1 / 47

ITK Workshop

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

Download Presentation

ITK Workshop

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITK Workshop Writing a New ITK Filter October 5-8, 2005

  2. 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

  3. Insight Toolkit - Advanced Course Anatomy of an ITK Filter

  4. The Class Hierarchy itk::Object itk::ProcessObject itk::DataObject itk::ImageBase itk::ImageSource itk::Image itk::ImageToImageFilter

  5. The Class Hierarchy itk::ImageToImageFilter itk::InPlaceImageFilter itk::UnaryFunctorImageFilter itk::BinaryFunctorImageFilter itk::TernaryFunctorImageFilter

  6. Filter Typical Elements Filter Input Image Output Image Parameters

  7. InPlace Filter Elements Filter Input Image Output Image Parameters

  8. Insight Toolkit - Advanced Course The Unary Functor Image Filter

  9. 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 > {…};

  10. Unary Functor Filter itk::UnaryFunctorImageFilter Pixel-Wise Image Filter Input Image Output Image

  11. Unary Functor Filter It should be enough to specify the operation to be applied on each pixel That is the role of the FUNCTOR

  12. Unary Functor Filter Functor Filter template <class TInputImage, class TOutputImage, class TFunctor> class UnaryFunctorImageFilter : public InPlaceImageFilter< TInputImage, TOutputImage >{ private: TFunctor m_Functor; };

  13. Insight Toolkit - Advanced Course Exercise 23 Create an Image Filter that performs Casting

  14. 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

  15. 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> > { …

  16. 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 );

  17. Typical Declarations: Traits and Macros • Traits • Self • Superclass • Pointer • ConstPointer • Macros • NewMacro • TypeMacro

  18. 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

  19. 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(); }

  20. Insight Toolkit - Advanced Course Exercise 24 Create an Image Filter that divides all the intensity values by 2

  21. Insight Toolkit - Advanced Course NOTE The use of itk::NumericTraits<> and RealType and typename

  22. 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

  23. 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; …

  24. Insight Toolkit - Advanced Course Exercise 25 Create an Image Filter that divides all the intensity values by a given value

  25. 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; };

  26. 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 ); }

  27. 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 ); }

  28. Insight Toolkit - Advanced Course Image Regions

  29. LargestPossibleRegion BufferedRegion RequestedRegion Insight Toolkit – Image Regions

  30. Filter Typical Elements Filter Input Image Output Image Region Region Parameters

  31. Insight Toolkit – Image Regions Input Image Output Image Input Region Output Region

  32. Generate Output Information Method Filter virtual voidGenerateOutputInformation(){Superclass::GenerateOutputInformation(); OutputImagePointer outputPtr = this->GetOutput(); outputPtr->SetLargestPossibleRegion(…); outputPtr->SetSpacing(…); outputPtr->SetOrigin(…);}

  33. Generate Output Information Method This method configures the Output Image • Spacing • Origin • Orientation (Direction) • LargestPossibleRegion by default it copies meta data from the Input

  34. Insight Toolkit - Advanced Course Exercise 26 Create an Image Filter that extractsthe first quadrant of an image

  35. 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 ); …

  36. 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();

  37. GenerateOutputInformation() Method • Call Superclass::GenerateOutputInformation() • Set Parameters of the Output Image • Spacing • Origin • Direction • LargestPossibleRegion

  38. 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();

  39. 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 );

  40. GenerateOutputInformation() Method outputImage->SetLargestPossibleRegion( outputRegion); outputImage->SetSpacing( inputImage->GetSpacing() ); outputImage->SetOrigin( inputImage->GetOrigin() ); outputImage->SetDirection( inputImage->GetDirection() ); } // end of GenerateOutputInformation() method

  41. 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

  42. 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 );

  43. Insight Toolkit – Image Regions First Quadrant OutputImageRegion InputImageRegion

  44. GenerateData() Method inputIterator.GoToBegin(); outputIterator.GoToBegin(); while( ! outputIterator.IsAtEnd() ) { outputIterator.Set( inputIterator.Get() ); ++inputIterator; ++outputIterator; }

  45. Insight Toolkit - Advanced Course Exercise 26b Modify the code for extractingthe central thirds an image

  46. Insight Toolkit – Image Regions OutputImageRegion Central Third InputImageRegion

  47. Insight Toolkit - Advanced Course END

More Related