ITK/Examples/Developer/ImageFilterMultipleOutputs

From KitwarePublic
< ITK‎ | Examples
Revision as of 18:49, 7 December 2010 by Daviddoria (talk | contribs) (Created page with "==ImageFilterMultipleOutputsExample.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "ImageFilterMultiple...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

ImageFilterMultipleOutputsExample.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkImageFileWriter.h"
  1. include "ImageFilterMultipleOutputs.h"

int main(int, char*[]) {

 // Setup types
 typedef itk::Image<unsigned char, 2>   ImageType;
 typedef itk::ImageFilterMultipleOutputs<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("TestOutput1.jpg");
 writer->SetInput(filter->GetOutput1());
 writer->Update();
 }
 
 {
 typedef  itk::ImageFileWriter< ImageType  > WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetFileName("TestOutput2.jpg");
 writer->SetInput(filter->GetOutput2());
 writer->Update();
 }
 
 return EXIT_SUCCESS;

} </source>

ImageFilterMultipleOutputs.h

<source lang="cpp">

  1. ifndef __itkImageFilterMultipleOutputs_h
  2. define __itkImageFilterMultipleOutputs_h
  1. include "itkImageToImageFilter.h"

namespace itk { template< class TImage> class ImageFilterMultipleOutputs : public ImageToImageFilter< TImage, TImage > { public:

 /** Standard class typedefs. */
 typedef ImageFilterMultipleOutputs             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(ImageFilterMultipleOutputs, ImageToImageFilter);
 TImage* GetOutput1();
 TImage* GetOutput2();
 

protected:

 ImageFilterMultipleOutputs();
 ~ImageFilterMultipleOutputs(){}
 /** Does the real work. */
 virtual void GenerateData();
 /**  Create the Output */
 DataObject::Pointer MakeOutput(unsigned int idx);

private:

 ImageFilterMultipleOutputs(const Self &); //purposely not implemented
 void operator=(const Self &);  //purposely not implemented

}; } //namespace ITK


  1. ifndef ITK_MANUAL_INSTANTIATION
  2. include "ImageFilterMultipleOutputs.txx"
  3. endif


  1. endif // __itkImageFilterMultipleOutputs_h

</source>

ImageFilterMultipleOutputs.txx

<source lang="cpp">

  1. ifndef __itkImageFilterMultipleOutputs_txx
  2. define __itkImageFilterMultipleOutputs_txx
  1. include "ImageFilterMultipleOutputs.h"
  1. include "itkObjectFactory.h"
  2. include "itkImageRegionIterator.h"
  3. include "itkImageRegionConstIterator.h"

namespace itk {

template< class TImage> ImageFilterMultipleOutputs<TImage>::ImageFilterMultipleOutputs() {

 this->SetNumberOfRequiredOutputs(2);
 this->SetNumberOfRequiredInputs(0);
 this->SetNthOutput( 0, this->MakeOutput(0) );
 this->SetNthOutput( 1, this->MakeOutput(1) );

}

template< class TImage> void ImageFilterMultipleOutputs<TImage>::GenerateData() {

 typename TImage::IndexType start;
 start[0] = 0;
 start[1] = 0;
 typename TImage::SizeType size;
 size[0] = 20;
 size[1] = 20;
 typename TImage::RegionType region;
 region.SetSize(size);
 region.SetIndex(start);
 
 // Setup output 1
 typename TImage::Pointer output1 = this->GetOutput1();
 output1->SetRegions(region);
 output1->Allocate();  
 
 itk::ImageRegionIterator<TImage> outputIterator1(output1, output1->GetLargestPossibleRegion());
 outputIterator1.GoToBegin();
 
 while(!outputIterator1.IsAtEnd())
   {
   if(outputIterator1.GetIndex()[0] == outputIterator1.GetIndex()[1])
     {
     outputIterator1.Set(255);
     }
   else
     {
     outputIterator1.Set(0);
     }
   ++outputIterator1;
   }
 // Setup output 2
 typename TImage::Pointer output2 = this->GetOutput2();
 output2->SetRegions(region);
 output2->Allocate();
 itk::ImageRegionIterator<TImage> outputIterator2(output2, output2->GetLargestPossibleRegion());
 outputIterator2.GoToBegin();
 while(!outputIterator2.IsAtEnd())
   {
   if(outputIterator2.GetIndex()[0] > 10)
     {
     outputIterator2.Set(255);
     }
   else
     {
     outputIterator2.Set(0);
     }
   ++outputIterator2;
   }

}

template< class TImage> DataObject::Pointer ImageFilterMultipleOutputs<TImage>::MakeOutput(unsigned int idx) {

 DataObject::Pointer output;
 switch ( idx )
   {
   case 0:
     output = ( TImage::New() ).GetPointer();
     break;
   case 1:
     output = ( TImage::New() ).GetPointer();
     break;
   default:
     std::cerr << "No output " << idx << std::endl;
     output = NULL;
     break;
   }
 return output.GetPointer();

}

template< class TImage> TImage* ImageFilterMultipleOutputs<TImage>::GetOutput1() {

 return dynamic_cast< TImage * >(
          this->ProcessObject::GetOutput(0) );

}

template< class TImage> TImage* ImageFilterMultipleOutputs<TImage>::GetOutput2() {

 return dynamic_cast< TImage * >(
          this->ProcessObject::GetOutput(1) );

}

}// end namespace


  1. endif

</source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

PROJECT(ImageFilterMultipleOutputs)

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(ImageFilterMultipleOutputs ImageFilterMultipleOutputsExample.cxx) TARGET_LINK_LIBRARIES(ImageFilterMultipleOutputs ITKBasicFilters ITKIO ITKCommon)

</source>