const unsigned int Dimension = 2;
typedef unsigned char PixelType;
int main(int, char *[] )
{
ImageType,
ImageType > MetricType;
ImageType,
double > InterpolatorType;
ImageType,
ImageType > RegistrationType;
registration->SetMetric( metric );
registration->SetOptimizer( optimizer );
registration->SetTransform( transform );
registration->SetInterpolator( interpolator );
CreateSphereImage(fixedImage);
CreateEllipseImage(movingImage);
fixedWriter->SetFileName("fixed.png");
fixedWriter->SetInput( fixedImage);
fixedWriter->Update();
movingWriter->SetFileName("moving.png");
movingWriter->SetInput( movingImage);
movingWriter->Update();
registration->SetFixedImage(fixedImage);
registration->SetMovingImage(movingImage);
registration->SetFixedImageRegion(
fixedImage->GetLargestPossibleRegion() );
typedef RegistrationType::ParametersType ParametersType;
ParametersType initialParameters( transform->GetNumberOfParameters() );
initialParameters[0] = 0.0;
initialParameters[1] = 0.0;
registration->SetInitialTransformParameters( initialParameters );
optimizer->SetMaximumStepLength( 4.00 );
optimizer->SetMinimumStepLength( 0.01 );
optimizer->SetNumberOfIterations( 200 );
try
{
registration->Update();
}
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
ParametersType finalParameters = registration->GetLastTransformParameters();
const double TranslationAlongX = finalParameters[0];
const double TranslationAlongY = finalParameters[1];
const unsigned int numberOfIterations = optimizer->GetCurrentIteration();
const 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;
ImageType,
ImageType > ResampleFilterType;
resampler->SetInput( movingImage);
resampler->SetTransform( registration->GetOutput()->Get() );
resampler->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );
resampler->SetOutputOrigin( fixedImage->GetOrigin() );
resampler->SetOutputSpacing( fixedImage->GetSpacing() );
resampler->SetOutputDirection( fixedImage->GetDirection() );
resampler->SetDefaultPixelValue( 100 );
typedef unsigned char OutputPixelType;
ImageType,
ImageType > CastFilterType;
writer->SetFileName("output.png");
caster->SetInput( resampler->GetOutput() );
writer->SetInput( caster->GetOutput() );
writer->Update();
return EXIT_SUCCESS;
}
{
EllipseType, ImageType > SpatialObjectToImageFilterType;
ImageType::SizeType size;
size[ 0 ] = 100;
size[ 1 ] = 100;
imageFilter->SetSize( size );
ImageType::SpacingType spacing;
spacing.Fill(1);
imageFilter->SetSpacing(spacing);
EllipseType::ArrayType radiusArray;
radiusArray[0] = 10;
radiusArray[1] = 20;
ellipse->SetRadius(radiusArray);
typedef EllipseType::TransformType TransformType;
transform->SetIdentity();
TransformType::OutputVectorType translation;
TransformType::CenterType center;
translation[ 0 ] = 65;
translation[ 1 ] = 45;
transform->Translate( translation, false );
ellipse->SetObjectToParentTransform( transform );
imageFilter->SetInput(ellipse);
ellipse->SetDefaultInsideValue(255);
ellipse->SetDefaultOutsideValue(0);
imageFilter->SetUseObjectValue( true );
imageFilter->SetOutsideValue( 0 );
imageFilter->Update();
image->Graft(imageFilter->GetOutput());
}
{
EllipseType, ImageType > SpatialObjectToImageFilterType;
ImageType::SizeType size;
size[ 0 ] = 100;
size[ 1 ] = 100;
imageFilter->SetSize( size );
ImageType::SpacingType spacing;
spacing.Fill(1);
imageFilter->SetSpacing(spacing);
EllipseType::ArrayType radiusArray;
radiusArray[0] = 10;
radiusArray[1] = 10;
ellipse->SetRadius(radiusArray);
typedef EllipseType::TransformType TransformType;
transform->SetIdentity();
TransformType::OutputVectorType translation;
TransformType::CenterType center;
translation[ 0 ] = 50;
translation[ 1 ] = 50;
transform->Translate( translation, false );
ellipse->SetObjectToParentTransform( transform );
imageFilter->SetInput(ellipse);
ellipse->SetDefaultInsideValue(255);
ellipse->SetDefaultOutsideValue(0);
imageFilter->SetUseObjectValue( true );
imageFilter->SetOutsideValue( 0 );
imageFilter->Update();
image->Graft(imageFilter->GetOutput());
}