ITK/Examples/Developer/ImageFilterMultipleOutputsDifferentType: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Deprecated content that is moved to sphinx)
 
Line 1: Line 1:
==ImageFilterMultipleOutputsDifferentType.cxx==
{{warning|1=The media wiki content on this page is no longer maintainedThe examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
<source lang="cpp">
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
 
#include "ImageFilterMultipleOutputsDifferentType.h"
 
int main(int, char*[])
{
  // Setup types
  typedef itk::Image<unsigned char, 2>  InputImageType;
  typedef itk::Image<float, 2>  OutputImageType1;
  typedef itk::Image<int, 2>  OutputImageType2;
  typedef itk::ImageFilterMultipleOutputsDifferentType<InputImageType, OutputImageType1, OutputImageType2>  FilterType;
 
  // Create and the filter
  FilterType::Pointer filter = FilterType::New();
  filter->Update();
 
  {
  typedef  itk::ImageFileWriter< OutputImageType1  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName("TestOutput1.jpg");
  writer->SetInput(filter->GetOutput1());
  writer->Update();
  }
 
  {
  typedef  itk::ImageFileWriter< OutputImageType2 > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName("TestOutput2.jpg");
  writer->SetInput(filter->GetOutput2());
  writer->Update();
  }
 
  return EXIT_SUCCESS;
}
 
</source>
 
==ImageFilterMultipleOutputsDifferentType.h==
<source lang="cpp">
#ifndef __itkImageFilterMultipleOutputsDifferentType_h
#define __itkImageFilterMultipleOutputsDifferentType_h
 
#include "itkImageToImageFilter.h"
 
namespace itk
{
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
class ImageFilterMultipleOutputsDifferentType : public ImageToImageFilter< TInputImage, TOutputImage1 > // This doesn't actually matter, as we will be overriding the output image type in MakeOutput()
{
public:
  /** Standard class typedefs. */
  typedef ImageFilterMultipleOutputsDifferentType            Self;
  typedef ImageToImageFilter< TInputImage, TOutputImage1 > Superclass;
  typedef SmartPointer< Self >        Pointer;
 
  /** Method for creation through the object factory. */
  itkNewMacro(Self);
 
  /** Run-time type information (and related methods). */
  itkTypeMacro(ImageFilterMultipleOutputsDifferentType, ImageToImageFilter);
 
  TOutputImage1* GetOutput1();
  TOutputImage2* GetOutput2();
 
protected:
  ImageFilterMultipleOutputsDifferentType();
  ~ImageFilterMultipleOutputsDifferentType(){}
 
  /** Does the real work. */
  virtual void GenerateData();
 
  /**  Create the Output */
  DataObject::Pointer MakeOutput(unsigned int idx);
 
private:
  ImageFilterMultipleOutputsDifferentType(const Self &); //purposely not implemented
  void operator=(const Self &); //purposely not implemented
 
};
} //namespace ITK
 
 
#ifndef ITK_MANUAL_INSTANTIATION
#include "ImageFilterMultipleOutputsDifferentType.hxx"
#endif
 
 
#endif // __itkImageFilterMultipleOutputsDifferentType_h
 
</source>
 
==ImageFilterMultipleOutputsDifferentType.hxx==
<source lang="cpp">
#ifndef __itkImageFilterMultipleOutputsDifferentType_txx
#define __itkImageFilterMultipleOutputsDifferentType_txx
 
#include "ImageFilterMultipleOutputsDifferentType.h"
 
#include "itkObjectFactory.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
 
namespace itk
{
 
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
ImageFilterMultipleOutputsDifferentType<TInputImage, TOutputImage1, TOutputImage2>::ImageFilterMultipleOutputsDifferentType()
{
  this->SetNumberOfRequiredOutputs(2);
  this->SetNumberOfRequiredInputs(0);
 
   this->SetNthOutput( 0, this->MakeOutput(0) );
  this->SetNthOutput( 1, this->MakeOutput(1) );
}
 
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
void ImageFilterMultipleOutputsDifferentType<TInputImage, TOutputImage1, TOutputImage2>::GenerateData()
{
  itk::Index<2> start;
  start.Fill(0);
 
  itk::Size<2> size;
  size.Fill(20);
 
  itk::ImageRegion<2> region(start,size);
 
  // Setup output 1
  typename TOutputImage1::Pointer output1 = this->GetOutput1();
  output1->SetRegions(region);
  output1->Allocate();
 
  // Setup output 2
  typename TOutputImage2::Pointer output2 = this->GetOutput2();
  output2->SetRegions(region);
  output2->Allocate();
 
}
 
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
DataObject::Pointer ImageFilterMultipleOutputsDifferentType<TInputImage, TOutputImage1, TOutputImage2>::MakeOutput(unsigned int idx)
{
  DataObject::Pointer output;
 
  switch ( idx )
    {
    case 0:
      output = ( TOutputImage1::New() ).GetPointer();
      break;
    case 1:
      output = ( TOutputImage2::New() ).GetPointer();
      break;
    default:
      std::cerr << "No output " << idx << std::endl;
      output = NULL;
      break;
    }
  return output.GetPointer();
}
 
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
TOutputImage1* ImageFilterMultipleOutputsDifferentType<TInputImage, TOutputImage1, TOutputImage2>::GetOutput1()
{
  return dynamic_cast< TOutputImage1 * >(this->ProcessObject::GetOutput(0) );
}
 
template< typename TInputImage, typename TOutputImage1, typename TOutputImage2>
TOutputImage2* ImageFilterMultipleOutputsDifferentType<TInputImage, TOutputImage1, TOutputImage2>::GetOutput2()
{
  return dynamic_cast< TOutputImage2 * >(this->ProcessObject::GetOutput(1) );
}
 
}// end namespace
 
 
#endif
 
</source>
 
{{ITKCMakeLists|{{SUBPAGENAME}}}}

Latest revision as of 19:36, 7 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.