namespace itk
{
template < typename TImage >
class CompositeExampleImageFilter :
public ImageToImageFilter< TImage, TImage >
{
public:
ITK_DISALLOW_COPY_AND_ASSIGN(CompositeExampleImageFilter);
using Self = CompositeExampleImageFilter;
itkNewMacro(Self);
itkTypeMacro(CompositeExampleImageFilter, ImageToImageFilter);
using ImageType = TImage;
using PixelType = typename ImageType::PixelType;
itkGetMacro( Threshold, PixelType );
itkSetMacro( Threshold, PixelType );
protected:
CompositeExampleImageFilter();
protected:
using ThresholdType = ThresholdImageFilter< ImageType >;
using GradientType = GradientMagnitudeImageFilter< ImageType, ImageType >;
using RescalerType = RescaleIntensityImageFilter< ImageType, ImageType >;
void GenerateData() override;
void PrintSelf( std::ostream& os, Indent indent ) const override;
private:
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] );
using FilterType = itk::CompositeExampleImageFilter<ImageType>;
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;
}