namespace itk
{
template < typename TImage >
class CompositeExampleImageFilter :
public ImageToImageFilter< TImage, TImage >
{
public:
typedef CompositeExampleImageFilter Self;
itkNewMacro(Self);
itkTypeMacro(CompositeExampleImageFilter, ImageToImageFilter);
typedef TImage ImageType;
typedef typename ImageType::PixelType PixelType;
itkGetMacro( Threshold, PixelType );
itkSetMacro( Threshold, PixelType );
protected:
CompositeExampleImageFilter();
protected:
typedef ThresholdImageFilter< ImageType > ThresholdType;
typedef GradientMagnitudeImageFilter< ImageType, ImageType > GradientType;
typedef RescaleIntensityImageFilter< ImageType, ImageType > RescalerType;
virtual void GenerateData() ITK_OVERRIDE;
void PrintSelf( std::ostream& os, Indent indent ) const ITK_OVERRIDE;
private:
ITK_DISALLOW_COPY_AND_ASSIGN(CompositeExampleImageFilter);
typename GradientType::Pointer m_GradientFilter;
typename ThresholdType::Pointer m_ThresholdFilter;
typename RescalerType::Pointer m_RescaleFilter;
PixelType m_Threshold;
};
}
namespace itk
{
template< typename TImage >
CompositeExampleImageFilter< TImage >
::CompositeExampleImageFilter()
{
m_Threshold = 1;
m_GradientFilter = GradientType::New();
m_ThresholdFilter = ThresholdType::New();
m_ThresholdFilter->SetInput( m_GradientFilter->GetOutput() );
m_RescaleFilter = RescalerType::New();
m_RescaleFilter->SetInput( m_ThresholdFilter->GetOutput() );
m_RescaleFilter->SetOutputMinimum(
}
template< typename TImage >
void
CompositeExampleImageFilter< TImage >
::GenerateData()
{
typename ImageType::Pointer input = ImageType::New();
input->Graft( const_cast< ImageType * >( this->GetInput() ));
m_GradientFilter->SetInput( input );
m_ThresholdFilter->ThresholdBelow( this->m_Threshold );
m_RescaleFilter->GraftOutput( this->GetOutput() );
m_RescaleFilter->Update();
this->GraftOutput( m_RescaleFilter->GetOutput() );
}
template< typename TImage >
void
CompositeExampleImageFilter< TImage >
::PrintSelf( std::ostream& os, Indent indent ) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Threshold:" << this->m_Threshold
<< std::endl;
}
}
int main( int argc, char* argv[] )
{
if( argc < 3 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputImageFile outputImageFile" << std::endl;
return EXIT_FAILURE;
}
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
typedef itk::CompositeExampleImageFilter<ImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
filter->SetInput( reader->GetOutput() );
filter->SetThreshold( 20 );
WriterType::Pointer writer = WriterType::New();
writer->SetInput( filter->GetOutput() );
writer->SetFileName( argv[2] );
try
{
writer->Update();
}
{
std::cerr << "Error: " << e << std::endl;
}
return EXIT_SUCCESS;
}