itk workshop

47
NA-MIC National Alliance for Medical Image Computing http://na-mic.org ITK Workshop October 5-8, 2005 Writing a New ITK Filter

Upload: gazit

Post on 09-Feb-2016

18 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ITK Workshop

NA-MICNational Alliance for Medical Image Computing http://na-mic.org

ITK Workshop

October 5-8, 2005

Writing a New ITK Filter

Page 2: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 3: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Anatomy of an ITK Filter

Insight Toolkit - Advanced Course

Page 4: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

The Class Hierarchy

itk::ProcessObjectitk::DataObject

itk::Object

itk::ImageBase

itk::Image

itk::ImageSource

itk::ImageToImageFilter

Page 5: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

The Class Hierarchy

itk::InPlaceImageFilter

itk::UnaryFunctorImageFilter itk::BinaryFunctorImageFilter

itk::TernaryFunctorImageFilter

itk::ImageToImageFilter

Page 6: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Filter Typical Elements

InputImage

OutputImage

Filter

Parameters

Page 7: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

InPlace Filter Elements

InputImage

OutputImage

Filter

Parameters

Page 8: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

The Unary Functor Image Filter

Insight Toolkit - Advanced Course

Page 9: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Image Filter Hierarchy

template <class TInputImage, class TOutputImage>

class ImageToImageFilter : public ImageSource< TOutputImage >

{…};

template <class TInputImage, class TOutputImage>

class InPlaceToImageFilter :

public ImageToImageFilter< TOutputImage , TOutputImage >

{…};

template <class TOutputImage>

class ImageSource : public ProcessObject

{…};

Page 10: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Unary Functor Filter

itk::UnaryFunctorImageFilter

Pixel-WiseImageFilter

Input Image Output Image

Page 11: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

It should be enough to specify theoperation to be applied on each pixel

Unary Functor Filter

That is the role of the

FUNCTOR

Page 12: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Unary Functor Filter

Filter

Functor

template <class TInputImage, class TOutputImage, class TFunctor>

class UnaryFunctorImageFilter :

public InPlaceImageFilter< TInputImage, TOutputImage >{

private:

TFunctor m_Functor;

};

Page 13: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise 23

Insight Toolkit - Advanced Course

Create an Image Filter thatperforms Casting

Page 14: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

namespace itk {

namespace Functor {

template< class TInput, class TOutput> class Cast { public: Cast() {}; ~Cast() {}; inline TOutput operator()( const TInput & A ) { return static_cast<TOutput>( A ); } };

} // end of Functor namespace

Unary Functor Filter Example : Casting

Page 15: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Unary Functor Filter Example : Casting

#include “itkUnaryFunctorImageFilter.h”

template<class TInputImage, class TOutputImage>class MyFunctorImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Cast< typename TInputImage::PixelType, typename TOutputImage::PixelType> > {

Page 16: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

public: typedef MyFunctorImageFilter Self; typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Cast< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass;

typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer;

itkNewMacro( Self );

itkTypeMacro( MyFunctorImageFilter, UnaryFunctorImageFilter );

Unary Functor Filter Example : Casting

Page 17: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Typical Declarations: Traits and Macros

• Traits

• Self

• Superclass

• Pointer

• ConstPointer

• Macros

• NewMacro

• TypeMacro

Page 18: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 19: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

}

Unary Functor Filter Example : Casting

Page 20: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise 24

Insight Toolkit - Advanced Course

Create an Image Filter that divides all the intensity values by 2

Page 21: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

NOTE

Insight Toolkit - Advanced Course

The use of itk::NumericTraits<> and RealType and typename

Page 22: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

namespace itk {

namespace Functor {

template< class TInput, class TOutput> class Divider { public: Divider() {}; ~Divider() {}; inline TOutput operator()( const TInput & A ) { typedef typename NumericTraits<TInput>::RealType InputRealType; return static_cast<TOutput>( InputRealType( A ) / 2.0 ); } };

} // end of Functor namespace

Exercise

Page 23: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise

#include “itkUnaryFunctorImageFilter.h”

template<class TInputImage, class TOutputImage>class DividerByTwoImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Functor::Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> >{public:

typedef MyFunctorImageFilter Self;

typedef UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> > Superclass;…

Page 24: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise 25

Insight Toolkit - Advanced Course

Create an Image Filter that divides all the intensity values by a given value

Page 25: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

namespace itk { namespace Functor { template< class TInput, class TOutput> class Divider { public: Divider() {}; ~Divider() {}; typedef typename NumericTraits<TInput>::RealType InputRealType; inline TOutput operator()( const TInput & A ) { return static_cast<TOutput>( InputRealType( A ) / m_Divisor ); } void SetDivisor( const InputRealType & value ) { m_Divisor = value; } private: InputRealType m_Divisor; };

Exercise : Functors with parameters

Page 26: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise : Functors with parameters

template<class TInputImage, class TOutputImage>class DividerImageFilter : public UnaryFunctorImageFilter< TInputImage,TOutputImage, Divider< typename TInputImage::PixelType, typename TOutputImage::PixelType> >{…public: typedef typename Superclass::FunctorType FunctorType; typedef typename FunctorType::InputRealType InputRealType;

void SetDivisor( const InputRealType & value ) { this->GetFunctor().SetDivisor( value ); }

Page 27: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Exercise : Functors with parameters

Page 28: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Image Regions

Insight Toolkit - Advanced Course

Page 29: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Insight Toolkit – Image Regions

LargestPossibleRegion

BufferedRegion

RequestedRegion

Page 30: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Filter Typical Elements

InputImage

OutputImage

Filter

Parameters

Region Region

Page 31: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Insight Toolkit – Image Regions

InputRegion

OutputRegion

Input Image Output Image

Page 32: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Generate Output Information Method

Filter

virtual voidGenerateOutputInformation(){ Superclass::GenerateOutputInformation();

OutputImagePointer outputPtr = this->GetOutput();

outputPtr->SetLargestPossibleRegion(…); outputPtr->SetSpacing(…); outputPtr->SetOrigin(…);}

Page 33: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Generate Output Information Method

• Spacing• Origin• Orientation (Direction)• LargestPossibleRegion

This method configures the Output Image

by default it copies meta data from the Input

Page 34: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise 26

Insight Toolkit - Advanced Course

Create an Image Filter that extractsthe first quadrant of an image

Page 35: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Quadrant Extract Image Filter Example

#include “itkImageToImageFilter.h”

template<class TInputImage>class FirstQuadrantExtractImageFilter : public ImageToImageFilter< TInputImage,TInputImage >{public: typedef FirstQuadrantExtractImageFilter Self; typedef ImageToImageFilter< TInputImage, TInputImage > Superclass;

typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer;

itkNewMacro( Self );

itkTypeMacro( FirstQuadrantExtractImageFilter, ImageToImageFilter ); …

Page 36: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 37: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

GenerateOutputInformation() Method

• Call Superclass::GenerateOutputInformation()

• Set Parameters of the Output Image

• Spacing

• Origin

• Direction

• LargestPossibleRegion

Page 38: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 39: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 40: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

GenerateOutputInformation() Method

outputImage->SetLargestPossibleRegion( outputRegion );

outputImage->SetSpacing( inputImage->GetSpacing() );

outputImage->SetOrigin( inputImage->GetOrigin() );

outputImage->SetDirection( inputImage->GetDirection() );

} // end of GenerateOutputInformation() method

Page 41: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 42: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

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

Page 43: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Insight Toolkit – Image Regions

First Quadrant

InputImageRegion

OutputImageRegion

Page 44: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

GenerateData() Method

inputIterator.GoToBegin();outputIterator.GoToBegin();

while( ! outputIterator.IsAtEnd() ) {

outputIterator.Set( inputIterator.Get() );

++inputIterator; ++outputIterator;

}

Page 45: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Exercise 26b

Insight Toolkit - Advanced Course

Modify the code for extractingthe central thirds an image

Page 46: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

Insight Toolkit – Image Regions

InputImageRegion

OutputImageRegion

Central Third

Page 47: ITK Workshop

National Alliance for Medical Image Computing http://na-mic.org

END

Insight Toolkit - Advanced Course