#include <iostream>
#include <cstdlib>
unsigned int RmsCounter = 0;
double MaxRmsE[4] = {0.8, 0.75, 0.4, 0.2};
{
public:
typedef CommandIterationUpdate
Self;
itkNewMacro( Self );
protected:
CommandIterationUpdate() {};
typedef short PixelType;
typedef float InternalPixelType;
InternalImageType, DisplacementFieldType> RegistrationFilterType;
public:
{
std::cout << "Warning: The const Execute method shouldn't be called" << std::endl;
}
{
RegistrationFilterType * 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:
typedef CommandResolutionLevelUpdate
Self;
itkNewMacro( Self );
protected:
CommandResolutionLevelUpdate() {};
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;
}
typedef short PixelType;
typedef float InternalPixelType;
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;
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;
RegistrationFilterType::Pointer filter = RegistrationFilterType::New();
filter->SetStandardDeviations( 1.0 );
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
filter->AddObserver( itk::IterationEvent(), observer );
InternalImageType,
InternalImageType,
DisplacementFieldType > MultiResRegistrationFilterType;
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;
}