int main( int argc, char *argv[] )
{
if( argc < 4 )
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile outputImagefile ";
std::cerr << " [differenceOutputfile] [differenceBeforeRegistration] ";
std::cerr << " [deformationField] ";
return EXIT_FAILURE;
}
constexpr unsigned int ImageDimension = 2;
using PixelType = float;
const unsigned int SpaceDimension = ImageDimension;
constexpr unsigned int SplineOrder = 3;
using CoordinateRepType = double;
CoordinateRepType,
SpaceDimension,
SplineOrder >;
FixedImageType,
MovingImageType >;
MovingImageType,
double >;
FixedImageType,
MovingImageType >;
MetricType::Pointer metric = MetricType::New();
OptimizerType::Pointer optimizer = OptimizerType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
RegistrationType::Pointer registration = RegistrationType::New();
registration->SetMetric( metric );
registration->SetOptimizer( optimizer );
registration->SetInterpolator( interpolator );
TransformType::Pointer transformLow = TransformType::New();
registration->SetTransform( transformLow );
FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();
MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName( argv[1] );
movingImageReader->SetFileName( argv[2] );
FixedImageType::ConstPointer fixedImage = fixedImageReader->GetOutput();
registration->SetFixedImage( fixedImage );
registration->SetMovingImage( movingImageReader->GetOutput() );
fixedImageReader->Update();
registration->SetFixedImageRegion( fixedRegion );
unsigned int numberOfGridNodes = 8;
TransformType::PhysicalDimensionsType fixedPhysicalDimensions;
TransformType::MeshSizeType meshSize;
TransformType::OriginType fixedOrigin;
for( unsigned int i=0; i< SpaceDimension; i++ )
{
fixedOrigin[i] = fixedImage->GetOrigin()[i];
fixedPhysicalDimensions[i] = fixedImage->GetSpacing()[i] *
static_cast<double>(
fixedImage->GetLargestPossibleRegion().GetSize()[i] - 1 );
}
meshSize.Fill( numberOfGridNodes - SplineOrder );
transformLow->SetTransformDomainOrigin( fixedOrigin );
transformLow->SetTransformDomainPhysicalDimensions(
fixedPhysicalDimensions );
transformLow->SetTransformDomainMeshSize( meshSize );
transformLow->SetTransformDomainDirection( fixedImage->GetDirection() );
using ParametersType = TransformType::ParametersType;
const unsigned int numberOfParameters =
transformLow->GetNumberOfParameters();
ParametersType parametersLow( numberOfParameters );
parametersLow.Fill( 0.0 );
transformLow->SetParameters( parametersLow );
registration->SetInitialTransformParameters( transformLow->GetParameters() );
optimizer->SetGradientConvergenceTolerance( 0.05 );
optimizer->SetLineSearchAccuracy( 0.9 );
optimizer->SetDefaultStepLength( 1.5 );
optimizer->TraceOn();
optimizer->SetMaximumNumberOfFunctionEvaluations( 1000 );
std::cout << "Starting Registration with low resolution transform"
<< std::endl;
try
{
registration->Update();
std::cout << "Optimizer stop condition = "
<< registration->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
TransformType::Pointer transformHigh = TransformType::New();
numberOfGridNodes = 12;
for( unsigned int i=0; i< SpaceDimension; i++ )
{
fixedOrigin[i] = fixedImage->GetOrigin()[i];
fixedPhysicalDimensions[i] = fixedImage->GetSpacing()[i] *
static_cast<double>(
fixedImage->GetLargestPossibleRegion().GetSize()[i] - 1 );
}
meshSize.Fill( numberOfGridNodes - SplineOrder );
transformHigh->SetTransformDomainOrigin( fixedOrigin );
transformHigh->SetTransformDomainPhysicalDimensions(
fixedPhysicalDimensions );
transformHigh->SetTransformDomainMeshSize( meshSize );
transformHigh->SetTransformDomainDirection( fixedImage->GetDirection() );
ParametersType parametersHigh( transformHigh->GetNumberOfParameters() );
parametersHigh.Fill( 0.0 );
unsigned int counter = 0;
for ( unsigned int k = 0; k < SpaceDimension; k++ )
{
using ParametersImageType = TransformType::ImageType;
ResamplerType::Pointer upsampler = ResamplerType::New();
FunctionType::Pointer function = FunctionType::New();
IdentityTransformType::Pointer identity = IdentityTransformType::New();
upsampler->SetInput( transformLow->GetCoefficientImages()[k] );
upsampler->SetInterpolator( function );
upsampler->SetTransform( identity );
upsampler->SetSize( transformHigh->GetCoefficientImages()[k]->
GetLargestPossibleRegion().GetSize() );
upsampler->SetOutputSpacing(
transformHigh->GetCoefficientImages()[k]->GetSpacing() );
upsampler->SetOutputOrigin(
transformHigh->GetCoefficientImages()[k]->GetOrigin() );
upsampler->SetOutputDirection( fixedImage->GetDirection() );
using DecompositionType =
DecompositionType::Pointer decomposition = DecompositionType::New();
decomposition->SetSplineOrder( SplineOrder );
decomposition->SetInput( upsampler->GetOutput() );
decomposition->Update();
ParametersImageType::Pointer newCoefficients = decomposition->GetOutput();
Iterator it( newCoefficients,
transformHigh->GetCoefficientImages()[k]->GetLargestPossibleRegion() );
while ( !it.IsAtEnd() )
{
parametersHigh[ counter++ ] = it.
Get();
++it;
}
}
transformHigh->SetParameters( parametersHigh );
std::cout << "Starting Registration with high resolution transform" << std::endl;
registration->SetInitialTransformParameters(transformHigh->GetParameters());
registration->SetTransform( transformHigh );
optimizer->SetGradientConvergenceTolerance( 0.01 );
optimizer->SetDefaultStepLength( 0.25 );
try
{
registration->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
transformHigh->SetParameters( registration->GetLastTransformParameters() );
MovingImageType,
FixedImageType >;
ResampleFilterType::Pointer resample = ResampleFilterType::New();
resample->SetTransform( transformHigh );
resample->SetInput( movingImageReader->GetOutput() );
resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
resample->SetOutputOrigin( fixedImage->GetOrigin() );
resample->SetOutputSpacing( fixedImage->GetSpacing() );
resample->SetOutputDirection( fixedImage->GetDirection() );
resample->SetDefaultPixelValue( 100 );
using OutputPixelType = unsigned char;
FixedImageType,
OutputImageType >;
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName( argv[3] );
caster->SetInput( resample->GetOutput() );
writer->SetInput( caster->GetOutput() );
try
{
writer->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
FixedImageType,
FixedImageType,
OutputImageType >;
DifferenceFilterType::Pointer difference = DifferenceFilterType::New();
WriterType::Pointer writer2 = WriterType::New();
writer2->SetInput( difference->GetOutput() );
if( argc >= 5 )
{
difference->SetInput1( fixedImageReader->GetOutput() );
difference->SetInput2( resample->GetOutput() );
writer2->SetFileName( argv[4] );
try
{
writer2->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
}
if( argc >= 6 )
{
writer2->SetFileName( argv[5] );
difference->SetInput1( fixedImageReader->GetOutput() );
difference->SetInput2( movingImageReader->GetOutput() );
try
{
writer2->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
}
DisplacementFieldType::Pointer field = DisplacementFieldType::New();
field->SetRegions( fixedRegion );
field->SetOrigin( fixedImage->GetOrigin() );
field->SetSpacing( fixedImage->GetSpacing() );
field->SetDirection( fixedImage->GetDirection() );
field->Allocate();
FieldIterator fi( field, fixedRegion );
fi.GoToBegin();
TransformType::InputPointType fixedPoint;
TransformType::OutputPointType movingPoint;
while( ! fi.IsAtEnd() )
{
index = fi.GetIndex();
field->TransformIndexToPhysicalPoint( index, fixedPoint );
movingPoint = transformHigh->TransformPoint( fixedPoint );
displacement = movingPoint - fixedPoint;
fi.Set( displacement );
++fi;
}
FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
fieldWriter->SetInput( field );
if( argc >= 7 )
{
fieldWriter->SetFileName( argv[6] );
try
{
fieldWriter->Update();
}
{
std::cerr << "Exception thrown " << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}