#include <iostream>
#include <cstdlib>
unsigned int RmsCounter = 0;
double MaxRmsE[4] = {0.8, 0.75, 0.4, 0.2};
{
public:
using Self = CommandIterationUpdate;
itkNewMacro( Self );
protected:
CommandIterationUpdate() = default;
using PixelType = short;
using InternalPixelType = float;
InternalImageType, DisplacementFieldType>;
public:
{
std::cout << "Warning: The const Execute method shouldn't be called" << std::endl;
}
{
auto * filter = static_cast< RegistrationFilterType * >( caller );
if( !(itk::IterationEvent().CheckEvent( &event )) )
{
return;
}
if(filter)
{
filter->SetMaximumRMSError(MaxRmsE[RmsCounter]);
std::cout << filter->GetMetric() << " RMS Change: " << filter->GetRMSChange() << std::endl;
std::cout << "Level Tolerance= "<<filter->GetMaximumRMSError ()<<std::endl;
}
}
};
{
public:
using Self = CommandResolutionLevelUpdate;
itkNewMacro( Self );
protected:
CommandResolutionLevelUpdate() = default;
public:
{
}
{
std::cout << "----------------------------------" << std::endl;
RmsCounter = RmsCounter + 1;
std::cout << "----------------------------------" << std::endl;
}
};
int main( int argc, char * argv [] )
{
if( argc != 5 )
{
std::cerr << "usage: " << std::endl;
std::cerr << argv[0] << " fixedImage movingImage registeredImage deformationField" << std::endl;
return EXIT_FAILURE;
}
using PixelType = short;
using InternalPixelType = float;
ReaderType::Pointer targetReader = ReaderType::New();
targetReader->SetFileName( argv[1] );
targetReader->Update();
ReaderType::Pointer sourceReader = ReaderType::New();
sourceReader->SetFileName( argv[2] );
sourceReader->Update();
ImageCasterType::Pointer targetImageCaster = ImageCasterType::New();
ImageCasterType::Pointer sourceImageCaster = ImageCasterType::New();
targetImageCaster->SetInput( targetReader->GetOutput() );
sourceImageCaster->SetInput( sourceReader->GetOutput() );
InternalImageType, InternalImageType >;
MatchingFilterType::Pointer matcher = MatchingFilterType::New();
matcher->SetInput( sourceImageCaster->GetOutput() );
matcher->SetReferenceImage( targetImageCaster->GetOutput() );
matcher->SetNumberOfHistogramLevels( 1024 );
matcher->SetNumberOfMatchPoints( 7 );
matcher->ThresholdAtMeanIntensityOn();
InternalImageType,
InternalImageType,
DisplacementFieldType>;
RegistrationFilterType::Pointer filter = RegistrationFilterType::New();
filter->SetStandardDeviations( 1.0 );
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
filter->AddObserver( itk::IterationEvent(), observer );
InternalImageType,
InternalImageType,
DisplacementFieldType >;
MultiResRegistrationFilterType::Pointer multires =
MultiResRegistrationFilterType::New();
multires->SetRegistrationFilter( filter );
multires->SetNumberOfLevels( 4 );
multires->SetFixedImage( targetImageCaster->GetOutput() );
multires->SetMovingImage( matcher->GetOutput() );
unsigned int nIterations[4] = {40, 40, 32, 32 };
multires->SetNumberOfIterations( nIterations );
CommandResolutionLevelUpdate::Pointer levelobserver = CommandResolutionLevelUpdate::New();
multires->AddObserver( itk::IterationEvent(), levelobserver );
try
{
multires->Update();
}
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
WarperType::Pointer warper = WarperType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
ImageType::Pointer targetImage = targetReader->GetOutput();
warper->SetInput( sourceReader->GetOutput() );
warper->SetInterpolator( interpolator );
warper->SetOutputSpacing( targetImage->GetSpacing() );
warper->SetOutputOrigin( targetImage->GetOrigin() );
warper->SetOutputDirection( targetImage->GetDirection() );
warper->SetDisplacementField( multires->GetOutput() );
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[3] );
writer->SetInput( warper->GetOutput() );
try
{
writer->Update();
}
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
DeformationWriterType::Pointer defwriter = DeformationWriterType::New();
defwriter->SetFileName( argv[4] );
defwriter->SetInput( multires->GetOutput() );
try
{
defwriter->Update();
}
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}