1 / 68

ITK Workshop

ITK Workshop. Software Design. October 5-8, 2005. ITK Workshop – Open Source Viewers. Design Patterns Pipeline Decorators Iterators Adaptors Accessors Functors Factories Traits. Insight Toolkit - Advanced Course. The Data Pipeline. The Data Pipeline. Process Object. Process

spatricia
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 Software Design October 5-8, 2005

  2. ITK Workshop – Open Source Viewers • Design Patterns • Pipeline • Decorators • Iterators • Adaptors • Accessors • Functors • Factories • Traits

  3. Insight Toolkit - Advanced Course The Data Pipeline

  4. The Data Pipeline Process Object Process Object Data Object Data Object Data Object

  5. Who owns the Output ? Data Object Data Object Data Object ProcessObject ProcessObject The ProcessObject allocates and owns its output Therefore the output is const

  6. How to use the Pipeline ? • Instantiate the filters types • Construct the filters with New() • Connect SetInput()GetOutput() • Set filter Parameters • Invoke Update() in the last filter

  7. The Pipeline is intended to be Re-Executed • Modify the filter parameters • Invoke Update() in the last filter Only the filters that need to re-compute their output will be re-executed

  8. Updating the Pipeline ImageReader FilterA FilterB FilterC FilterD ImageWriter

  9. Updating the Pipeline ImageReader FilterA FilterB FilterC Execute Execute Update() FilterD ImageWriter Execute Execute

  10. Updating the Pipeline Update() ImageReader FilterA FilterB FilterC Execute Execute Execute Execute FilterD ImageWriter

  11. Updating the Pipeline Update() SetParameter ImageReader FilterA FilterB FilterC I’m OK I’m OK Execute Execute FilterD ImageWriter

  12. How it works internally • itk::Object has a TimeStamp • Invoking Modified() updates the stamp • SetParameter() methods calls Modified() • Most Set() methods use itkSetMacro() • If you write your own SetParameter() you must remember to invoke Modified()

  13. How it works internally Quiz ! What will go wrong with your filter if you create a SetParameter() method and forget to invoke Modified() from it ? It will run fine the first time but if you call SetParameter() and then call Update() the filter will not re-execute.

  14. How it works internally Exercise Open the itkMacro.h file in Insight/Code/Common Find the itkSetMacro() and identify the line where Modified() is invoked

  15. Insight Toolkit - Advanced Course Pervasive Pipelining (or the Ode to Intelligent Design)

  16. Insight Toolkit - Advanced Course In the Beginning… there was the itk::DataObject The itk::DataObject originated the itk::Image and the itk::Mesh

  17. Insight Toolkit - Advanced Course Only the the itk::DataObject and his sons were admited in the Pipeline garden The float, ints, Transforms and Pointsgrew jealous of the DataObject Since they could not promenadein the Pipeline Garden

  18. Insight Toolkit - Advanced Course So in chorus they asked to change the ways of the Pipeline garden And from the darkness of the abyss the itk::DataObjectDecoratorwas born.

  19. Insight Toolkit - Advanced Course With it,the float, ints, Transforms and Points became as DataObjects and walked at their will in the Pipeline Garden.

  20. The SimpleDataObjectDecorator #include “itkDataObject.h” template<class T> class SimpleDataObjectDecorator : public DataObject { public: typedefSimpleDataObjectDecoratorSelf; typedefDataObjectSuperclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; itkNewMacro( Self ); itkTypeMacro( SimpleDataObjectDecorator, DataObject ); private: Tm_Component;

  21. The SimpleDataObjectDecorator public: virtual void Set( const T & value ) { if( m_Component != value ) {m_Component = value; this->Modified(); } } virtual const T & Get() const { return m_Component }

  22. The DataObjectDecorator #include “itkDataObject.h” template<class T> class DataObjectDecorator : public DataObject { public: typedefDataObjectDecoratorSelf; typedefDataObjectSuperclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; itkNewMacro( Self ); itkTypeMacro(DataObjectDecorator, DataObject ); private: typename T::Pointer m_Component;

  23. The DataObjectDecorator public: virtual void Set( const T * value ) { if( m_Component != value ) {m_Component = value; this->Modified(); } } virtual const T * Get() const { return m_Component }

  24. Insight Toolkit - Advanced Course Iterators

  25. Iterators • STL Iterators • Image Iterators • Region • Linear • Slice • Mesh Iterators • VectorContainer • MapContainer

  26. STL Iterators #include <vector> typedefstd::vector< float > VectorType; VectorType myVector; myVector.push_back( 4 );myVector.push_back( 7 );myVector.push_back( 19 ); VectorType::const_iterator itr = myVector.begin(); while( itr != myVector.end() ) { std::cout << *itr << std::endl; ++itr; }

  27. Image Iterators The Evil nested loop and the Dimensional Trap for(unsigned int z = 0; z < Nz; z++ ) { for(unsigned int y = 0; y < Ny; y++ ) { for(unsigned int x = 0; x < Nx; x++ ) { image[z][y][x] = … // pixel access } } } How to generalize this code to 2D, 3D, 4D …?

  28. Image Iterators (const) #include “itkImage.h” #include “itkImageRegionIterator.h” typedefitk::Image< float, 3 > ImageType; typedefitk::ImageRegionConstIterator< ImageType > IteratorType; ImageType::ConstPointer image = GetConstImageSomeHow(); ImageType::RegionType region = image->GetLargestPossibleRegion(); IteratorType itr( image, region ); itr.GoToBegin(); while( ! itr.IsAtEnd() ) { std::cout << itr.Get() << std::endl; ++itr; }

  29. Image Iterators (non-const) #include “itkImage.h” #include “itkImageRegionIterator.h” typedefitk::Image< float, 3 > ImageType; typedefitk::ImageRegionIterator< ImageType > IteratorType; ImageType::Pointer image = GetNonConstImageSomeHow(); ImageType::RegionType region = image->GetLargestPossibleRegion(); IteratorType itr( image, region ); itr.GoToBegin(); while( ! itr.IsAtEnd() ) { itr.Set( 0 ); ++itr; }

  30. Image Linear Iterator (const) #include “itkImage.h” #include “itkImageLinearIteratorWithIndex.h” typedefitk::Image< float, 3 > ImageType; typedefitk::ImageLinearConstIteratorWithIndex< ImageType > IteratorType; ImageType::ConstPointer image = GetConstImageSomeHow(); ImageType::RegionType region = GetImageRegionSomeHow(); IteratorType itr( image, region ); for( itr.GoToBegin(); !itr.IsAtEnd(); itr.NextLine()) { while( ! itr.IsAtEndOfLine() ) { std::cout << itr.Get() << “:“ << itr.GetIndex() << std::endl; ++itr; } }

  31. Image Slice Iterator (const) #include “itkImageSliceIteratorWithIndex.h” typedefitk::ImageSliceConstIteratorWithIndex<ImageType> IteratorType; ImageType::ConstPointer image = GetConstImageSomeHow(); ImageType::RegionType region = GetImageRegionSomeHow(); IteratorType itr( image, region ); for( itr.GoToBegin(); !itr.IsAtEnd(); itr.NextSlice()) { for( ; ! itr.IsAtEndOfSlice(); itr.NextLine()) { for( ; ! itr.IsAtEndOfLine(); ++itr) { std::cout << itr.Get() << “:“ << itr.GetIndex() << std::endl; } } }

  32. Using the ImageLinearIterator Exercise 28 Use two ImageLinearIterators in order to flip and image across its diagonal

  33. Other Image Iterators • Reflective Image Iterator • Neighborhood Iterator • Shaped Neighborhood Iterator • Image Random Iterator • Image Random Non Repeating Iterator • Flood Filled Function Conditional Iterator • Path Iterator

  34. Importance of Image Iterators • Iterators absorb a large portion of the complexity in an Image Filter • Iterators made possible to generalize ITK to N-Dimensional images • Iterators allows to have fast access to pixel data • Iterators made ImageAdaptors possible (see later)

  35. Mesh Iterators (const) #include “itkMesh.h” typedefitk::Mesh< float, 3 > MeshType; MeshType::ConstPointer mesh = GetConstMeshSomeHow(); typedefMeshType::PointsContainer PointsContainer; typedefPointsContainer::ConstIterator PointsIterator; PointsContainer points = mesh->GetPoints(); PointsIterator pointItr = points->Begin(); while( pointItr != points->End() ) { MeshType::PointsType point = pointItr.Value(); std::cout << “point “ << point << std::endl; ++pointItr; }

  36. Insight Toolkit - Advanced Course Image Adaptors (Things are not always what they seem)

  37. Some operations are too simple for a filter Image Filter Image Image ProcessObject Image Adaptor Image FakeImage ImageAdaptor

  38. Pixel Accessor Image Adaptors = Image Iterators + Pixel Accessors Image FakeImage ImageAdaptor Pixel Accessor Pixel Accessor Where the transformation is done

  39. Pixel Accessor and Image Iterators Image Image Iterator

  40. Pixel Accessor and Image Iterators Image Image Iterator m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset )); itr.Get()

  41. Pixel Accessor : Red Channel from RGB Image namespace Accessor { class RedChannel{ public: typedef itk::RGBPixel<float> InternalType; typedef float ExternalType; static ExternalType Get()( const InternalType & A ) { return static_cast< ExternalType >( A.GetRed() ); } }; } // end of Accessor namespace

  42. Using the Pixel Accessor in the Image Adaptor int main(){ typedefAccessor::RedChannel::InternalType InternalPixelType; typedef itk::Image< InternalPixelType, 2 > AdaptedImageType; typedef itk::ImageAdaptor< AdaptedImageType, Accessor::RedChannel>ImageAdaptorType; ImageAdaptorType::Pointer adaptor =ImageAdaptorType::New(); typedef itk::ImageFilterReader< AdaptedImageType > ReaderType; ReaderType::Pointer reader =ReaderType::New(); adaptor->SetImage( reader->GetOutput());

  43. Image Adaptors • Image Adaptors are like Images • Image Adaptors are not Filters • Image Adaptors do not have Buffers • Not all Iterators support Image Adaptors • Not all Filters support Image Adaptors

  44. Using the Pixel Accessor in the Image Adaptor typedef itk::Image< unsigned char, 2 > OutputImageType; typedef itk::RescaleIntensityImageFilter< ImageAdaptorType,OutputImageType > FilterType; FilterType::Pointer filter =FilterType::New(); filter->SetInput( adaptor ); // Adaptors are like Images !! writer->SetInput( filter->GetOutput()); writer->Update(); }

  45. Image Adaptors Pierce the Illusion…to see the Void.

  46. Image Adaptors • Writers access Image’s buffer directly,They are not fooled by Image Adaptors. • Neigborhood Iterators do not call theGet() method…They are not fooled by Image Adaptors.

  47. Image Adaptors Exercise 27 Write a Pixel Accessor that will invert the intensities in an image. Instantiate its corresponding Image Adaptor

  48. Pixel Accessor : Invert 8-bits intensities. namespace Accessor { class Inversor{ public: typedef unsigned charInternalType; typedef unsigned char ExternalType; static ExternalType Get()( const InternalType & A ) { return static_cast< ExternalType >( 255 - A ); } }; } // end of Accessor namespace

  49. Insight Toolkit - Advanced Course Functors The Way takes no action, but leaves nothing undone

  50. Functors A Functor is a class that looks like a Function namespace Functor { template< class TInput, class TOutput> class Doubler{ public:Doubler() {};~Doubler() {}; inlineTOutput operator()( const TInput & A ) { returnstatic_cast<TOutput>( 2.0 * A ); } }; } // end of Functor namespace

More Related