ITK/Examples/Developer/ImageFilter
This example demonstrates the creation of a filter which produces the output image which is equal to the input image except its pixel on the main diagonal are turned to white.
ImageFilterExample.cxx
<source lang="cpp">
- include "itkImage.h"
- include "itkImageFileReader.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;
typedef itk::ImageFileReader<ImageType> ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName("Test.jpg"); reader->Update(); // Create and the filter FilterType::Pointer filter = FilterType::New(); filter->SetInput(reader->GetOutput()); filter->Update();
typedef itk::ImageFileWriter< ImageType > WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetFileName("TestOutput.jpg"); writer->SetInput(filter->GetOutput()); writer->Update();
return EXIT_SUCCESS;
} </source>
ImageFilter.h
<source lang="cpp">
- ifndef __itkImageFilter_h
- define __itkImageFilter_h
- include "itkImageToImageFilter.h"
namespace itk { template< class TImage> class ImageFilter:public ImageToImageFilter< TImage, TImage > { public:
/** Standard class typedefs. */ typedef ImageFilter Self; typedef ImageToImageFilter< TImage, TImage > Superclass; typedef SmartPointer< Self > Pointer;
/** Method for creation through the object factory. */ itkNewMacro(Self);
/** Run-time type information (and related methods). */ itkTypeMacro(ImageFilter, ImageToImageFilter);
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
</source>
ImageFilter.txx
<source lang="cpp">
- ifndef __itkImageFilter_txx
- define __itkImageFilter_txx
- include "ImageFilter.h"
- include "itkObjectFactory.h"
- include "itkImageRegionIterator.h"
- include "itkImageRegionConstIterator.h"
namespace itk {
template< class TImage> void ImageFilter< TImage>
- GenerateData()
{
typename TImage::ConstPointer input = this->GetInput(); typename TImage::Pointer output = this->GetOutput(); output->SetRegions(input->GetLargestPossibleRegion()); output->Allocate(); itk::ImageRegionIterator<TImage> outputIterator(output, output->GetLargestPossibleRegion()); itk::ImageRegionConstIterator<TImage> inputIterator(input, input->GetLargestPossibleRegion());
while(!outputIterator.IsAtEnd()) { if(inputIterator.GetIndex()[0] == inputIterator.GetIndex()[1]) { outputIterator.Set(255); } else { outputIterator.Set(inputIterator.Get()); }
++inputIterator; ++outputIterator; }
}
}// end namespace
- endif
</source>
CMakeLists.txt
<source lang="cmake"> cmake_minimum_required(VERSION 2.6)
PROJECT(ImageFilter)
FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})
ADD_EXECUTABLE(ImageFilter ImageFilterExample.cxx) TARGET_LINK_LIBRARIES(ImageFilter ITKBasicFilters ITKIO ITKCommon)
</source>