Presentation is loading. Please wait.

Presentation is loading. Please wait.

NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter.

Similar presentations


Presentation on theme: "NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter."— Presentation transcript:

1 NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter

2 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

3 National Alliance for Medical Image Computing Anatomy of an ITK Filter Insight Toolkit - Advanced Course

4 National Alliance for Medical Image Computing The Class Hierarchy itk::ProcessObject itk::DataObject itk::Object itk::ImageBase itk::Image itk::ImageSource itk::ImageToImageFilter

5 National Alliance for Medical Image Computing The Class Hierarchy itk::InPlaceImageFilter itk::UnaryFunctorImageFilteritk::BinaryFunctorImageFilter itk::TernaryFunctorImageFilter itk::ImageToImageFilter

6 National Alliance for Medical Image Computing Filter Typical Elements Input Image Output Image Filter Parameters

7 National Alliance for Medical Image Computing InPlace Filter Elements Input Image Output Image Filter Parameters

8 National Alliance for Medical Image Computing The Unary Functor Image Filter Insight Toolkit - Advanced Course

9 National Alliance for Medical Image Computing Image Filter Hierarchy template class ImageToImageFilter : public ImageSource {…}; template class InPlaceToImageFilter : public ImageToImageFilter {…}; template class ImageSource : public ProcessObject {…};

10 National Alliance for Medical Image Computing Unary Functor Filter itk::UnaryFunctorImageFilter Pixel-Wise Image Filter Input ImageOutput Image

11 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

12 National Alliance for Medical Image Computing Unary Functor Filter Filter Functor template class UnaryFunctorImageFilter : public InPlaceImageFilter { private: TFunctor m_Functor; };

13 National Alliance for Medical Image Computing Exercise 23 Insight Toolkit - Advanced Course Create an Image Filter that performs Casting

14 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

15 National Alliance for Medical Image Computing Unary Functor Filter Example : Casting #include itkUnaryFunctorImageFilter.h template class MyFunctorImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Cast > { …

16 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

17 National Alliance for Medical Image Computing Typical Declarations: Traits and Macros Traits Self Superclass Pointer ConstPointer Macros NewMacro TypeMacro

18 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

19 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

20 National Alliance for Medical Image Computing Exercise 24 Insight Toolkit - Advanced Course Create an Image Filter that divides all the intensity values by 2

21 National Alliance for Medical Image Computing NOTE Insight Toolkit - Advanced Course The use of itk::NumericTraits<> and RealType and typename

22 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

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

24 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

25 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

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

27 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

28 National Alliance for Medical Image Computing Image Regions Insight Toolkit - Advanced Course

29 National Alliance for Medical Image Computing Insight Toolkit – Image Regions LargestPossibleRegion BufferedRegion RequestedRegion

30 National Alliance for Medical Image Computing Filter Typical Elements Input Image Output Image Filter Parameters Region

31 National Alliance for Medical Image Computing Insight Toolkit – Image Regions Input Region Output Region Input ImageOutput Image

32 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(…); }

33 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

34 National Alliance for Medical Image Computing Exercise 26 Insight Toolkit - Advanced Course Create an Image Filter that extracts the first quadrant of an image

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

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

37 National Alliance for Medical Image Computing GenerateOutputInformation() Method Call Superclass::GenerateOutputInformation() Set Parameters of the Output Image Spacing Origin Direction LargestPossibleRegion

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

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

40 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

41 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

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

43 National Alliance for Medical Image Computing Insight Toolkit – Image Regions First Quadrant Input Image Region Output Image Region

44 National Alliance for Medical Image Computing GenerateData() Method inputIterator.GoToBegin(); outputIterator.GoToBegin(); while( ! outputIterator.IsAtEnd() ) { outputIterator.Set( inputIterator.Get() ); ++inputIterator; ++outputIterator; }

45 National Alliance for Medical Image Computing Exercise 26b Insight Toolkit - Advanced Course Modify the code for extracting the central thirds an image

46 National Alliance for Medical Image Computing Insight Toolkit – Image Regions Input Image Region Output Image Region Central Third

47 National Alliance for Medical Image Computing END Insight Toolkit - Advanced Course


Download ppt "NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter."

Similar presentations


Ads by Google