ITK/Examples/Developer/ImageSource
From KitwarePublic
Contents |
Description
This example demonstrates how to make a filter which produces an image.
ITK Classes Demonstrated
ImageSourceExample.cxx
#include "itkImage.h" #include "itkImageFileWriter.h" #include "ImageFilter.h" int main(int, char*[]) { // Setup types typedef itk::Image<unsigned char, 2> ImageType; typedef itk::ImageFilter<ImageType> FilterType; // Create and the filter FilterType::Pointer filter = FilterType::New(); filter->Update(); typedef itk::ImageFileWriter< ImageType > WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetFileName("Test.jpg"); writer->SetInput(filter->GetOutput()); writer->Update(); return EXIT_SUCCESS; }
ImageSource.h
#ifndef __itkImageFilter_h #define __itkImageFilter_h #include "itkImageSource.h" namespace itk { template< class TOutputImage> class ImageFilter:public ImageSource< TOutputImage > { public: /** Standard class typedefs. */ typedef ImageFilter Self; typedef ImageSource< TOutputImage > Superclass; typedef SmartPointer< Self > Pointer; /** Method for creation through the object factory. */ itkNewMacro(Self); /** Run-time type information (and related methods). */ itkTypeMacro(ImageFilter, ImageSource); protected: ImageFilter(){} ~ImageFilter(){} /** Does the real work. */ virtual void GenerateData(); private: ImageFilter(const Self &); //purposely not implemented void operator=(const Self &); //purposely not implemented }; } //namespace ITK #ifndef ITK_MANUAL_INSTANTIATION #include "ImageFilter.txx" #endif #endif // __itkImageFilter_h
ImageSource.txx
#ifndef __itkImageFilter_txx #define __itkImageFilter_txx #include "ImageFilter.h" #include "itkObjectFactory.h" namespace itk { template< class TOutputImage> void ImageFilter< TOutputImage> ::GenerateData() { typename TOutputImage::Pointer output = this->GetOutput(); typename TOutputImage::RegionType region; typename TOutputImage::IndexType start; start[0] = 0; start[1] = 0; typename TOutputImage::SizeType size; size[0] = 200; size[1] = 300; region.SetSize(size); region.SetIndex(start); output->SetRegions(region); output->Allocate(); itk::ImageRegionIterator<TOutputImage> imageIterator(output,output->GetLargestPossibleRegion()); while(!imageIterator.IsAtEnd()) { if(imageIterator.GetIndex()[0] == imageIterator.GetIndex()[1]) { imageIterator.Set(255); } else { imageIterator.Set(0); } ++imageIterator; } } }// end namespace #endif
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) project(ImageSource) find_package(ITK REQUIRED) include(${ITK_USE_FILE}) add_executable(ImageSource ImageSource.cxx) if( "${ITK_VERSION_MAJOR}" LESS 4 ) target_link_libraries(ImageSource ITKReview ${ITK_LIBRARIES}) else( "${ITK_VERSION_MAJOR}" LESS 4 ) target_link_libraries(ImageSource ${ITK_LIBRARIES}) endif( "${ITK_VERSION_MAJOR}" LESS 4 )