template <typename TRegistration>
{
public:
typedef RegistrationInterfaceCommand
Self;
itkNewMacro( Self );
protected:
RegistrationInterfaceCommand() {};
public:
typedef TRegistration RegistrationType;
typedef RegistrationType * RegistrationPointer;
typedef OptimizerType * OptimizerPointer;
{
{
return;
}
RegistrationPointer registration =
dynamic_cast<RegistrationPointer>( object );
OptimizerPointer optimizer = dynamic_cast< OptimizerPointer >(
registration->GetModifiableOptimizer() );
std::cout << "-------------------------------------" << std::endl;
std::cout << "MultiResolution Level : "
<< registration->GetCurrentLevel() << std::endl;
std::cout << std::endl;
if ( registration->GetCurrentLevel() == 0 )
{
optimizer->SetMaximumStepLength( 16.00 );
optimizer->SetMinimumStepLength( 0.01 );
}
else
{
optimizer->SetMaximumStepLength(
optimizer->GetMaximumStepLength() * 0.25 );
optimizer->SetMinimumStepLength(
optimizer->GetMinimumStepLength() * 0.1 );
}
}
{ return; }
};
{
public:
typedef CommandIterationUpdate
Self;
itkNewMacro( Self );
protected:
CommandIterationUpdate() {};
public:
typedef const OptimizerType * OptimizerPointer;
{
}
{
OptimizerPointer optimizer =
dynamic_cast< OptimizerPointer >( object );
{
return;
}
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;
}
};
int main( int argc, const char *argv[] )
{
if( argc < 4 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile ";
std::cerr << " outputImagefile [backgroundGrayLevel]";
std::cerr << " [checkerBoardBefore] [checkerBoardAfter]";
std::cerr << " [useExplicitPDFderivatives ] " << std::endl;
std::cerr << " [numberOfBins] [numberOfSamples ] " << std::endl;
return EXIT_FAILURE;
}
const unsigned int Dimension = 2;
typedef unsigned short PixelType;
const std::string fixedImageFile = argv[1];
const std::string movingImageFile = argv[2];
const std::string outImagefile = argv[3];
const PixelType backgroundGrayLevel = (argc >4 )? atoi(argv[4]): 100;
const std::string checkerBoardBefore = (argc >5 )? argv[5]: "";
const std::string checkerBoardAfter = (argc >6 )? argv[6]: "";
const bool useExplicitPDFderivatives = (argc >7 )? static_cast<bool>(atoi(argv[7])): false;
const int numberOfBins = (argc >8 )? atoi(argv[8]): 0;
const int numberOfSamples = (argc >9 )? atoi(argv[9]): 0;
typedef float InternalPixelType;
InternalImageType,
double > InterpolatorType;
InternalImageType,
InternalImageType > MetricType;
InternalImageType,
InternalImageType > RegistrationType;
InternalImageType, InternalImageType > FixedImagePyramidType;
InternalImageType, InternalImageType > MovingImagePyramidType;
TransformType::Pointer transform = TransformType::New();
OptimizerType::Pointer optimizer = OptimizerType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
RegistrationType::Pointer registration = RegistrationType::New();
MetricType::Pointer metric = MetricType::New();
FixedImagePyramidType::Pointer fixedImagePyramid =
FixedImagePyramidType::New();
MovingImagePyramidType::Pointer movingImagePyramid =
MovingImagePyramidType::New();
registration->SetOptimizer( optimizer );
registration->SetTransform( transform );
registration->SetInterpolator( interpolator );
registration->SetMetric( metric );
registration->SetFixedImagePyramid( fixedImagePyramid );
registration->SetMovingImagePyramid( movingImagePyramid );
FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName( fixedImageFile );
movingImageReader->SetFileName( movingImageFile );
FixedImageType, InternalImageType > FixedCastFilterType;
MovingImageType, InternalImageType > MovingCastFilterType;
FixedCastFilterType::Pointer fixedCaster = FixedCastFilterType::New();
MovingCastFilterType::Pointer movingCaster = MovingCastFilterType::New();
fixedCaster->SetInput( fixedImageReader->GetOutput() );
movingCaster->SetInput( movingImageReader->GetOutput() );
registration->SetFixedImage( fixedCaster->GetOutput() );
registration->SetMovingImage( movingCaster->GetOutput() );
fixedCaster->Update();
registration->SetFixedImageRegion(
fixedCaster->GetOutput()->GetBufferedRegion() );
typedef RegistrationType::ParametersType ParametersType;
ParametersType initialParameters( transform->GetNumberOfParameters() );
initialParameters[0] = 0.0;
initialParameters[1] = 0.0;
registration->SetInitialTransformParameters( initialParameters );
metric->SetNumberOfHistogramBins( 128 );
metric->SetNumberOfSpatialSamples( 50000 );
if( argc > 8 )
{
metric->SetNumberOfHistogramBins( numberOfBins );
}
if( argc > 9 )
{
metric->SetNumberOfSpatialSamples( numberOfSamples );
}
metric->ReinitializeSeed( 76926294 );
if( argc > 7 )
{
metric->SetUseExplicitPDFDerivatives( useExplicitPDFderivatives );
}
optimizer->SetNumberOfIterations( 200 );
optimizer->SetRelaxationFactor( 0.9 );
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
typedef RegistrationInterfaceCommand<RegistrationType> CommandType;
CommandType::Pointer command = CommandType::New();
registration->SetNumberOfLevels( 3 );
try
{
registration->Update();
std::cout << "Optimizer stop condition: "
<< registration->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
return EXIT_FAILURE;
}
ParametersType finalParameters = registration->GetLastTransformParameters();
double TranslationAlongX = finalParameters[0];
double TranslationAlongY = finalParameters[1];
unsigned int numberOfIterations = optimizer->GetCurrentIteration();
double bestValue = optimizer->GetValue();
std::cout << "Result = " << std::endl;
std::cout << " Translation X = " << TranslationAlongX << std::endl;
std::cout << " Translation Y = " << TranslationAlongY << std::endl;
std::cout << " Iterations = " << numberOfIterations << std::endl;
std::cout << " Metric value = " << bestValue << std::endl;
MovingImageType,
FixedImageType > ResampleFilterType;
TransformType::Pointer finalTransform = TransformType::New();
finalTransform->SetParameters( finalParameters );
finalTransform->SetFixedParameters( transform->GetFixedParameters() );
ResampleFilterType::Pointer resample = ResampleFilterType::New();
resample->SetTransform( finalTransform );
resample->SetInput( movingImageReader->GetOutput() );
FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();
resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
resample->SetOutputOrigin( fixedImage->GetOrigin() );
resample->SetOutputSpacing( fixedImage->GetSpacing() );
resample->SetOutputDirection( fixedImage->GetDirection() );
resample->SetDefaultPixelValue( backgroundGrayLevel );
typedef unsigned char OutputPixelType;
FixedImageType,
OutputImageType > CastFilterType;
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName( outImagefile );
caster->SetInput( resample->GetOutput() );
writer->SetInput( caster->GetOutput() );
writer->Update();
CheckerBoardFilterType::Pointer checker = CheckerBoardFilterType::New();
checker->SetInput1( fixedImage );
checker->SetInput2( resample->GetOutput() );
caster->SetInput( checker->GetOutput() );
writer->SetInput( caster->GetOutput() );
resample->SetDefaultPixelValue( 0 );
TransformType::Pointer identityTransform = TransformType::New();
identityTransform->SetIdentity();
resample->SetTransform( identityTransform );
for (int q=0; q< argc; ++q)
{
std::cout << q << " " << argv[q] << std::endl;
}
if( checkerBoardBefore != std::string("") )
{
writer->SetFileName( checkerBoardBefore );
writer->Update();
}
resample->SetTransform( finalTransform );
if( checkerBoardAfter != std::string("") )
{
writer->SetFileName( checkerBoardAfter );
writer->Update();
}
return EXIT_SUCCESS;
}