Medical Imaging ITK Dr. Engr. Sami ur Rahman Assistant Professor Department of Computer Science University of Malakand Credit Some of the slides are taken from the presenation of Jean-Loïc Rose Other contributers are cited at the end. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 2 Installing ITK Download InsightTookit-2.0.1.zip from http://www.itk.org/HTML/Download.htm Download CMake installer from http://www.cmake.org/HTML/Download.html Install CMake Unzip InsightToolkit to folder Run CMake set PATH\InsightToolkit-2.0.1 as source code folder Set PATH\InsightToolkit-2.0.1-bin as where to build binaries Disable BUILD_EXAMPLES and BUILD_TESTING Click Configure button University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 3 Installing ITK (part 2) Open ITK.dsw in binary build directory Select Active Configuration to ALL_BUILD – Win32 RelWithDebInfo Click Build Wait 15-20 minutes University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 4 Build Sample Program Unzip itkdemo.zip to some folder Run CMake same as for ITK For ITK_DIR set binary directory Open itkdemp.dsw in Visual Studio Itkdemo.exe <infile> <outfile> <scale> University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 5 Contents What is ITK Background Concepts Data representations Images and regions Pixel access in ITK Developer’s guide Access pixel data How to write a filter University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 6 ITK Basics Image Processing and Analysis Toolkit No visualization (VTK recommended) Does not include GUI framework Designed for Medical Imaging Applications In general algorithms work in N-dimensions University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 7 Catalog of ITK Features Image IO Image processing Canny Edge Hough Transform (lines/ellipsoids) Variable Conductance Diffusion Geometry IO/representation/processing (Spatial Objects) Statistics Registration/Segmentation Numerics Optimizers Finite Element Simulation University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 8 How code using ITK is written Mostly in C++ Heavily templated/generic programming like STL Multi-threading capable Pipeline architecture 1. Build Pipeline 2. Execute pipeline CMake used as build system Reader File Image Gaussian Image Writer File University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 9 How to find what you need ? http://www.itk.org/ItkSoftwareGuide.pdf http://www.itk.org/Doxygen/html/index.html Follow the link Alphabetical List Follow the link Groups Post to the insight-users mailing list University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 10 How to integrate ITK C++ Glue code ITK GUI Image Processing MFC, QT, BBTK, wxWin Visualization OpenGL, VTK University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 11 Concepts Data Pipeline Smart Pointers C++ Generic Programming (Templates) University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 12 Concepts (I) Pipeline architecture Data pipeline Image Filter Image Filter Image Filter Image Image University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 13 Concepts (I) Pipeline architecture Pipeline architecture 1.Build Pipeline 2.Execute pipeline Reader File Image Gaussian Image Writer File University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 14 Concepts (II) An aside: Smart pointer In C++ you typically allocate memory with new and desallocate it with delete Cat* pCat = new Cat; pCat->Meaow(); delete pCat; memory leak Smart pointers get around this problem by allocating and deallocating memory for you You do not explicitly delete objects in ITK, this occurs automatically when they go out of scope Since you can’t forget to delete objects, you can’t leak memory University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 15 Concepts (III) C++ generic programming Generic programming is a method of organizing libraries consisting of generic—or reusable— Abstraction of Types and Behaviors Example: STL Standard Template Library std::vector< T > .02 … 1 2.5 … 0 .23 … 5 .47 … 4 5.6 … 3 std::vector< int > std::vector< double > std::vector< char* > std::vector< point > std::vector< image > University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 16 Concepts (III) C++ generic programming Template ? Pixel type (char, int, …) Dimension (2D, 3D, …) University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 17 Contents What is ITK Background Concepts Data representations Filtering Data representations Images and regions Pixel access in ITK ITK Features Developer’s guide Access pixel data How to write a filter Applications University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 18 Data storage in ITK ITK separates storage of data from the actions you can perform on data The DataObject class is the base class for the major “containers” into which you can place data Images: N-d rectilinear grids of regularly sampled data Meshes: N-d collections of points linked together into cells (e.g. triangles) University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 19 Class hierarchy itk::Object itk::DataObject itk::ImageBase itk::PointSet itk::Image itk::Mesh University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 20 Images and regions ITK was designed to allow analysis of very large images, even images that far exceed the available RAM of a computer For this reason, ITK distinguishes between an entire image and the part which is actually resident in memory or requested by an algorithm University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 21 Images and regions Algorithms only process a region of an image that sits inside the current buffer LargestPossibleRegion::Size BufferedRegion::Size The BufferedRegion is the portion of image in physical memory The RequestedRegion is the portion of image to be processed The LargestPossibleRegion describes the entire dataset RequestedRegion::Size RequestedRegion::Index BufferedRegion::Index LargestPossibleRegion::Index University Of Malakand | Department of Computer Science | Visual Computing Images are templated Image ITK itk::Image< PixelType, Dimension > Pixel type Dimensionality (value) Example itk::Image< int, 2 > itk::Image< char, 4 > itk::Image< RGB, 3 > itk::Image< std::vector< double >, 2 > Unsigned char, 2 University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 23 Images and regions The favorite keyword typedef Declaring an image type typedef itk::Image< char, 2 > ImageType; We can now use ImageType in place of the full class name, a nice convenience Remember that names ending in “Type” are types, not variables or class names University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 24 Images and regions Creating an image pointer typedef itk::Image< char, 2 > ImageType; ImageType::Pointer image = ImageType::New(); Pointer is typedef in itk::Image Macro “big New” An image is created by invoking the New() operator from the corresponding image type and assigning the result to a SmartPointer. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 25 Images and regions Creating an image typedef itk::Image< char, 2 > ImageType; ImageType::Pointer image = ImageType::New(); ImageType::SizeType size; size[0] = 512; // x direction size[1] = 512; // y direction ImageType::IndexType start; start[0] = 0; // x direction start[1] = 0; // y direction Classes can have typedefs as members. In this case, SizeType is a public member of itk::Image. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 26 Images and regions Creating the region of an image typedef itk::Image< char, 2 > ImageType; ImageType::Pointer image = ImageType::New(); ImageType::SizeType size = {{512,512}}; ImageType::IndexType start = {{0,0}}; // Initialize region parameter ImageType::RegionType region; region.SetSize( size ); region.SetIndex( start ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 27 Images and regions Allocate typedef itk::Image< char, 2 > ImageType; ImageType::Pointer image = ImageType::New(); ImageType::SizeType size = {{512,512}}; ImageType::IndexType start = {{0,0}}; // Initialize region parameter ImageType::RegionType region; region.SetSize( size ); region.SetIndex( start ); // Allocate image image->SetRegions( region ); image->Allocate( ); image->FillBuffer( 0 ); The SetRegions function sets all 3 regions to the same region and Allocate sets aside memory for the image. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 28 Image features Data space vs. “physical” space Image IO University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 29 Data space vs. “physical” space Data space is an N-d array with integer indices, indexed from 0 to (Li - 1) e.g. pixel (3,0,5) in 3D space Physical space relates to data space by defining the origin and spacing of the image University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 30 Data space vs. “physical” space Spacing: We can specify spacing by calling the SetSpacing() function in Image. ImageType::SpacingType spacing; spacing[0] = 0.83; // x direction spacing[1] = 2.15; // y direction Image->SetSpacing( spacing ); Origin: Similarly, we can set the image origin ImageType::IndexType origin; origin[0] = 0.83; // x direction origin[1] = 2.15; // y direction Image->SetIndex( origin ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 31 Data space vs. “physical” space The image class contains a number of convenience methods to convert between pixel indices and physical positions (as stored in the Point class) typedef itk::Image< char, 2 > ImageType; ImageType::PointType point; // Physical space ImageType::PixelType pixelIndex; // Data space image->TransformPhysicalPointToIndex( point, pixelIndex ); image->TransformIndexToPhysicalPoint( pixelIndex, point ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 32 ITK Image IO Image File PNGImageIO GIPLImageIO CustomImageIO ImageFileReader MetaImageIO Image AnalyzeImageIO VTKImageIO DICOMImageIO Loadable Factories University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 33 ITK Image IO Image File ImageFileReader Image Filter Image File ImageFileWriter Image University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 34 ITK Image IO #include “itkImageFileReader.h” #include “itkImageFileWriter.h” int main() { typedef itk::Image< unsigned char, 2 > ImageType; typedef itk::ImageFileReader< ImageType > ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFilename( inputFilename ); reader->Update( ); // reader->Update( ); typedef itk::ImageFileWiter< ImageType > WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetInput( reader->GetOutput( ) ); writer->SetFilename( outputFilename ); writer->Update( ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 35 } Image typedef Classes can have typedefs as members. typedef itk::Image< char, 2 > ImageType; ImageType::Pointer // Image pointer ImageType::SizeType // Size of image ImageType::IndexType // Index of pixels ImageType::PixelType // Type of pixels ImageType::RegionType // region of pixels ImageType::… University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 36 Contents What is ITK Background Concepts Data representations Filtering Data representations Images and regions Pixel access in ITK ITK Features Developer’s guide Access pixel data How to write a filter Applications University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 37 Pixel access in ITK There are many ways to access pixels in ITK Direct pixel access Iterators - Index in data space - Physical position, in physical space University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 38 Direct pixel access in ITK The simplest way is to directly address a pixel by knowing either its: The Index object is used to access pixels in an image, in data space ImageType::IndexType pixelIndex; pixelIndex[0] = 27; // x direction pixelIndex[1] = 29; // y direction // To set a pixel ImageType::PixelType pixelValue = 149; Image->SetPixel( pixelIndex, pixelValue ); // To get a pixel ImageType::PixelType pixelValue; pixelValue = Image->GetPixel( pixelIndex ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 39 Pixel access in ITK with iterators An iterator is described as walking its iteration region. There is no restriction on the dimensionality of the image or on the pixel type of the image. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 40 Pixel access in ITK with iterators Moving iterators - GoToBegin() - GoToEnd() - operator++() - operator--() - bool IsAtEnd() - bool IsAtBegin() - IndexType GetIndex() - … Accessing data - PixelType Get() - void Set( PixelType ) University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 41 Pixel access in ITK with iterators Iteration loops example (1D, 2D, 3D, 4D…) typedef itk::ImageRegionIterator< ImageType > IteratorType; IteratorType it( image, image->GetRequestedRegion() ); for( it.GoToBegin(); !it.IsAtEnd(); it++ ) { std::cout << “Index : ” << it.GetIndex() << std::endl; std::cout << “Value : ” << it.Get() << std::endl; } Example on a volume (500x500x500) Direct pixel access: Pixel access with iterators: 43.39 s. 4.48 s. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 42 Pixel access in ITK with iterators Classic image iterators ImageRegionIterator (basic ITK image iterator) ImageRegionIteratorWithIndex Line-by-line iterators ImageLinearIteratorWithIndex (NextLine(), …) ImageSliceIteratorWithIndex (NextSlice(), …) Random iterators ImageRandomConstIteratorWithIndex Neighborhood iterator NeighborhoodIterator University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 43 Pixel access in ITK with iterators Neighborhood iterator University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 44 Class hierarchy itk::Object itk::DataObject itk::ProcessObject itk::ImageBase itk::ImageSource itk::Image itk::ImageToImageFilter University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 45 Filter typical element typedef itk::ImageToImageFilter < ImageType, ImageType > FilterType; Filter Input Image Output Image Parameters University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 46 Inplace filter element typedef itk::InPlaceImageFilter < ImageType, ImageType > FilterType; Filter Input Image Output Image Parameters The output bulk data is the same block of memory as the input bulk data. University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 47 Create an Image Filter namespace itk { template< class TInput, class TOutput> class MyImageFilter : public ImageToImageFilter< class TInput, class TOutput> { public: typedef MyImageFilter Self; typedef ImageToImageFilter< TInput, TOutput > Superclass; typedef SmartPointer< Self > Pointer; typedef SmartPointer< const Self > ConstPointer; itkNewMacro( Self ); itkTypeMacro( MyImageFilter, ImageToImageFilter ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 48 Create an Image Filter protected: MyImageFilter() {} virtual ~MyImageFilter() {} virtual void GenerateData() {}; // The real work private: MyImageFilter( const Self & ); // purposely not implemented void operator=( const Self & ); // purposely not implemented }; // end of class } // end of namespace itk University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 49 Typical Declarations: Traits and Macros Traits Self Superclass Pointer ConstPointer Macros NewMacro TypeMacro University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 50 Create an Image Filter #include “MyImageFilter.h” int main() { typedef itk::Image< unsigned char, 2 > InputImageType; typedef itk::Image< unsigned short, 2 > OutputImageType; typedef itk::MyImageFilter< InputImageType, OutputImageType > FilterType; FilterType::Pointer filter = FilterType::New(); Filter->SetInput( reader->GetOuput() ); Filter->Update( ); OutputImageType::Pointer image = Filter->GetOutput( ); University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 51 } References http://www.itk.org/ItkSoftwareGuide.pdf http://www.itk.org/Doxygen/html/index.html http://www.itk.org/HTML/Tutorials.htm University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 52 References http://caddlab.rad.unc.edu/Public/InsightIn90Minutes.ppt ITK CVS Online documentation http://www.itk.org/Doxygen/html/index.html ITK 2.0 (preliminary) Software Guide http://prdownloads.sourceforge.net/itk/ItkSoftwareGuide2.0.0.pdf?download http://www.itk.org/CourseWare/Training/GettingStarted-I.pdf Neurolib tutorial (excellent for research use of ITK) http://www.ia.unc.edu/dev/tutorials/InstallLib/index.htm University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 53 The End University Of Malakand | Department of Computer Science | Visual Computing Research Group | Dr. Engr.Sami ur Rahman | 54