#include <iomanip>
int main(int argc, char *argv[])
{
if( argc < 4 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile ";
std::cerr << " resampledMovingImageFile ";
std::cerr << " [numberOfAffineIterations numberOfDisplacementIterations] ";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * fixedImageFile = argv[1];
const char * movingImageFile = argv[2];
const char * resampledMovingImageFile = argv[3];
unsigned int numberOfAffineIterations = 2;
unsigned int numberOfDisplacementIterations = 2;
if( argc >= 5 )
{
numberOfAffineIterations = atoi( argv[4] );
}
if( argc >= 6 )
{
numberOfDisplacementIterations = atoi( argv[5] );
}
const unsigned int NumberOfPixelComponents = 3;
typedef float PixelComponentType;
typedef double ParametersValueType;
ReaderType::Pointer fixedImageReader = ReaderType::New();
fixedImageReader->SetFileName( fixedImageFile );
ReaderType::Pointer movingImageReader = ReaderType::New();
movingImageReader->SetFileName( movingImageFile );
try
{
fixedImageReader->Update();
movingImageReader->Update();
}
{
std::cerr << "Error while reading images: " << error << std::endl;
return EXIT_FAILURE;
}
InputImageType::Pointer fixedImage = fixedImageReader->GetOutput();
InputImageType::Pointer movingImage = movingImageReader->GetOutput();
AffineTransformType;
AffineTransformType::Pointer affineTransform = AffineTransformType::New();
ParametersValueType, Dimension > DisplacementTransformType;
DisplacementTransformType::Pointer displacementTransform =
DisplacementTransformType::New();
typedef DisplacementTransformType::DisplacementFieldType
DisplacementFieldType;
DisplacementFieldType::Pointer displacementField =
DisplacementFieldType::New();
displacementField->SetRegions( fixedImage->GetLargestPossibleRegion() );
displacementField->CopyInformation( fixedImage );
std::cout << "fixedImage->GetLargestPossibleRegion(): "
<< fixedImage->GetLargestPossibleRegion() << std::endl;
displacementField->Allocate();
DisplacementTransformType::OutputVectorType zeroVector;
zeroVector.Fill( 0.0 );
displacementField->FillBuffer( zeroVector );
displacementTransform->SetDisplacementField( displacementField );
displacementTransform->SetGaussianSmoothingVarianceForTheUpdateField( 5.0 );
displacementTransform->SetGaussianSmoothingVarianceForTheTotalField( 6.0 );
IdentityTransformType;
IdentityTransformType::Pointer identityTransform =
IdentityTransformType::New();
VirtualImageType;
InputImageType, InputImageType, VirtualImageType, InputPixelType::Length >
MetricTraitsType;
InputImageType, InputImageType, VirtualImageType, double, MetricTraitsType >
MetricType;
typedef MetricType::FixedSampledPointSetType
PointSetType;
MetricType::Pointer metric = MetricType::New();
PointSetType::Pointer pointSet = PointSetType::New();
fixedImage->GetLargestPossibleRegion() );
for( fixedImageIt.GoToBegin(); !fixedImageIt.IsAtEnd(); ++fixedImageIt )
{
if ( count % 2 == 0 )
{
PointType point;
fixedImage->TransformIndexToPhysicalPoint( fixedImageIt.GetIndex(), point );
pointSet->SetPoint( index, point );
++index;
}
++count;
}
metric->SetFixedSampledPointSet( pointSet );
metric->SetUseFixedSampledPointSet( true );
metric->SetFixedImage( fixedImage );
metric->SetMovingImage( movingImage );
metric->SetFixedTransform( identityTransform );
metric->SetMovingTransform( affineTransform );
const bool gaussian = false;
metric->SetUseMovingImageGradientFilter( gaussian );
metric->SetUseFixedImageGradientFilter( gaussian );
metric->Initialize();
RegistrationParameterScalesFromShiftType;
RegistrationParameterScalesFromShiftType::Pointer shiftScaleEstimator =
RegistrationParameterScalesFromShiftType::New();
shiftScaleEstimator->SetMetric( metric );
std::cout << "First do an affine registration..." << std::endl;
OptimizerType::Pointer optimizer = OptimizerType::New();
optimizer->SetMetric( metric );
optimizer->SetNumberOfIterations( numberOfAffineIterations );
optimizer->SetScalesEstimator( shiftScaleEstimator );
optimizer->StartOptimization();
std::cout << "After optimization affine parameters are: "
<< affineTransform->GetParameters() << std::endl;
std::cout << "Now, add the displacement field to the composite transform..."
<< std::endl;
CompositeType::Pointer compositeTransform = CompositeType::New();
compositeTransform->AddTransform( affineTransform );
compositeTransform->AddTransform( displacementTransform );
compositeTransform->SetOnlyMostRecentTransformToOptimizeOn();
metric->SetMovingTransform( compositeTransform );
metric->SetUseFixedSampledPointSet( false );
metric->Initialize();
optimizer->SetScalesEstimator( shiftScaleEstimator );
optimizer->SetMetric( metric );
optimizer->SetNumberOfIterations( numberOfDisplacementIterations );
try
{
if( numberOfDisplacementIterations > 0 )
{
optimizer->StartOptimization();
}
else
{
std::cout << "*** Skipping displacement field optimization.\n";
}
}
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
ResampleFilterType;
ResampleFilterType::Pointer resampler = ResampleFilterType::New();
resampler->SetTransform( compositeTransform );
resampler->SetInput( movingImageReader->GetOutput() );
resampler->SetReferenceImage( fixedImage );
resampler->SetUseReferenceImage( true );
OutputPixelType;
OutputImageType;
CastFilterType;
CastFilterType::Pointer caster = CastFilterType::New();
caster->SetInput( resampler->GetOutput() );
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( resampledMovingImageFile );
writer->SetInput( caster->GetOutput() );
try
{
writer->UpdateLargestPossibleRegion();
}
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}