ITK/Examples/Developer/ImageFilterMultipleInputsDifferentType: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "==ImageFilterMultipleInputsDifferentTypeExample.cxx== <source lang="cpp"> #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkCovar...")
 
No edit summary
Line 69: Line 69:
   ~ImageFilterMultipleInputsDifferentType(){}
   ~ImageFilterMultipleInputsDifferentType(){}


  typename TImage::ConstPointer GetInputImage();
  typename TMask::ConstPointer GetInputMask();
 
   /** Does the real work. */
   /** Does the real work. */
   virtual void GenerateData();
   virtual void GenerateData();
Line 118: Line 121:
{
{
   SetNthInput(1, const_cast<TMask*>(mask));
   SetNthInput(1, const_cast<TMask*>(mask));
}
template< typename TImage, typename TMask>
typename TImage::ConstPointer ImageFilterMultipleInputsDifferentType<TImage, TMask>::GetInputImage()
{
  return static_cast< const TImage * >
        ( this->ProcessObject::GetInput(0) );
}
template< typename TImage, typename TMask>
typename TMask::ConstPointer ImageFilterMultipleInputsDifferentType<TImage, TMask>::GetInputMask()
{
  return static_cast< const TMask * >
        ( this->ProcessObject::GetInput(1) );
}
}


Line 123: Line 140:
void ImageFilterMultipleInputsDifferentType<TImage, TMask>::GenerateData()
void ImageFilterMultipleInputsDifferentType<TImage, TMask>::GenerateData()
{
{
   typename TImage::ConstPointer input = this->GetInput(0);
   typename TImage::ConstPointer input = this->GetInputImage();
   //typename TMask::ConstPointer mask = const_cast<typename TMask::Pointer> (this->GetInput(1));
   typename TMask::ConstPointer mask = this->GetInputMask();
  typename TMask::ConstPointer mask = const_cast<TMask*> (this->GetInput(1)); // invalid cast
  //error: invalid const_cast from type ‘const itk::Image<itk::CovariantVector<unsigned char, 3u>, 2u>*’ to type ‘itk::Image<unsigned char, 2u>*’
 


   typename TImage::Pointer output = this->GetOutput();
   typename TImage::Pointer output = this->GetOutput();

Revision as of 21:53, 4 December 2010

ImageFilterMultipleInputsDifferentTypeExample.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkImageFileWriter.h"
  4. include "itkCovariantVector.h"
  1. include "ImageFilterMultipleInputsDifferentType.h"

int main(int, char*[]) {

 // Setup types
 typedef itk::Image<itk::CovariantVector<unsigned char, 3>, 2>   VectorImageType;
 typedef itk::Image<unsigned char, 2> ScalarImageType;
 typedef itk::ImageFilterMultipleInputsDifferentType<VectorImageType, ScalarImageType>  FilterType;
 typedef itk::ImageFileReader<VectorImageType> 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< VectorImageType  > WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetFileName("TestOutput.jpg");
 writer->SetInput(filter->GetOutput());
 writer->Update();
 return EXIT_SUCCESS;

} </source>

ImageFilterMultipleInputsDifferentType.h

<source lang="cpp">

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

namespace itk { template< typename TImage, typename TMask> class ImageFilterMultipleInputsDifferentType : public ImageToImageFilter< TImage, TImage > { public:

 /** Standard class typedefs. */
 typedef ImageFilterMultipleInputsDifferentType             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(ImageFilterMultipleInputsDifferentType, ImageToImageFilter);
 /** The image to be inpainted in regions where the mask is white.*/
 void SetInputImage(const TImage* image);
 /** The mask to be inpainted. White pixels will be inpainted, black pixels will be passed through to the output.*/
 void SetInputMask(const TMask* mask);

protected:

 ImageFilterMultipleInputsDifferentType();
 ~ImageFilterMultipleInputsDifferentType(){}
 typename TImage::ConstPointer GetInputImage();
 typename TMask::ConstPointer GetInputMask();
 
 /** Does the real work. */
 virtual void GenerateData();

private:

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

}; } //namespace ITK


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


  1. endif // __itkImageFilterMultipleInputsDifferentType_h

</source>

ImageFilterMultipleInputsDifferentType.txx

<source lang="cpp">

  1. ifndef __itkImageFilterMultipleInputs_txx
  2. define __itkImageFilterMultipleInputs_txx
  1. include "ImageFilterMultipleInputsDifferentType.h"
  1. include "itkObjectFactory.h"
  2. include "itkImageRegionIterator.h"
  3. include "itkImageRegionConstIterator.h"

namespace itk {

template< typename TImage, typename TMask> ImageFilterMultipleInputsDifferentType<TImage, TMask>::ImageFilterMultipleInputsDifferentType() {

 this->SetNumberOfRequiredInputs(2);

}

template< typename TImage, typename TMask> void ImageFilterMultipleInputsDifferentType<TImage, TMask>::SetInputImage(const TImage* image) {

 SetNthInput(0, const_cast<TImage*>(image));

}

template< typename TImage, typename TMask> void ImageFilterMultipleInputsDifferentType<TImage, TMask>::SetInputMask(const TMask* mask) {

 SetNthInput(1, const_cast<TMask*>(mask));

}

template< typename TImage, typename TMask> typename TImage::ConstPointer ImageFilterMultipleInputsDifferentType<TImage, TMask>::GetInputImage() {

 return static_cast< const TImage * >
        ( this->ProcessObject::GetInput(0) );

}

template< typename TImage, typename TMask> typename TMask::ConstPointer ImageFilterMultipleInputsDifferentType<TImage, TMask>::GetInputMask() {

 return static_cast< const TMask * >
        ( this->ProcessObject::GetInput(1) );

}

template< typename TImage, typename TMask> void ImageFilterMultipleInputsDifferentType<TImage, TMask>::GenerateData() {

 typename TImage::ConstPointer input = this->GetInputImage();
 typename TMask::ConstPointer mask = this->GetInputMask();
 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


  1. endif

</source>

CMakeLists.txt

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

PROJECT(ImageFilterMultipleInputsDifferentType)

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

ADD_EXECUTABLE(ImageFilterMultipleInputsDifferentType ImageFilterMultipleInputsDifferentTypeExample.cxx) TARGET_LINK_LIBRARIES(ImageFilterMultipleInputsDifferentType ITKBasicFilters ITKIO ITKCommon)

</source>