{
public:
using Self = CommandIterationUpdate;
protected:
CommandIterationUpdate() = default;
public:
using OptimizerPointer = const OptimizerType *;
void
{
}
void
{
auto optimizer = static_cast<OptimizerPointer>(object);
if (!itk::IterationEvent().CheckEvent(&event))
{
return;
}
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << optimizer->GetCurrentPosition();
vnl_matrix<double> p(2, 2);
p[0][0] = static_cast<double>(optimizer->GetCurrentPosition()[0]);
p[0][1] = static_cast<double>(optimizer->GetCurrentPosition()[1]);
p[1][0] = static_cast<double>(optimizer->GetCurrentPosition()[2]);
p[1][1] = static_cast<double>(optimizer->GetCurrentPosition()[3]);
vnl_svd<double> svd(p);
vnl_matrix<double> r(2, 2);
r = svd.U() * vnl_transpose(svd.V());
const double angle = std::asin(r[1][0]);
<< std::endl;
}
};
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::endl;
std::cerr << " outputImagefile [differenceBeforeRegistration] "
<< std::endl;
std::cerr << " [differenceAfterRegistration] " << std::endl;
std::cerr << " [stepLength] [maxNumberOfIterations] " << std::endl;
return EXIT_FAILURE;
}
using PixelType = float;
using MetricType =
using RegistrationType = itk::
ImageRegistrationMethodv4<FixedImageType, MovingImageType, TransformType>;
auto metric = MetricType::New();
auto optimizer = OptimizerType::New();
auto registration = RegistrationType::New();
registration->SetMetric(metric);
registration->SetOptimizer(optimizer);
auto transform = TransformType::New();
auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName(argv[1]);
movingImageReader->SetFileName(argv[2]);
registration->SetFixedImage(fixedImageReader->GetOutput());
registration->SetMovingImage(movingImageReader->GetOutput());
using TransformInitializerType =
FixedImageType,
MovingImageType>;
auto initializer = TransformInitializerType::New();
initializer->SetTransform(transform);
initializer->SetFixedImage(fixedImageReader->GetOutput());
initializer->SetMovingImage(movingImageReader->GetOutput());
initializer->MomentsOn();
initializer->InitializeTransform();
registration->SetInitialTransform(transform);
registration->InPlaceOn();
double translationScale = 1.0 / 1000.0;
if (argc > 8)
{
translationScale = std::stod(argv[8]);
}
using OptimizerScalesType = OptimizerType::ScalesType;
OptimizerScalesType optimizerScales(transform->GetNumberOfParameters());
optimizerScales[0] = 1.0;
optimizerScales[1] = 1.0;
optimizerScales[2] = 1.0;
optimizerScales[3] = 1.0;
optimizerScales[4] = translationScale;
optimizerScales[5] = translationScale;
optimizer->SetScales(optimizerScales);
double steplength = 1.0;
if (argc > 6)
{
steplength = std::stod(argv[6]);
}
unsigned int maxNumberOfIterations = 300;
if (argc > 7)
{
maxNumberOfIterations = std::stoi(argv[7]);
}
optimizer->SetLearningRate(steplength);
optimizer->SetMinimumStepLength(0.0001);
optimizer->SetNumberOfIterations(maxNumberOfIterations);
auto observer = CommandIterationUpdate::New();
optimizer->AddObserver(itk::IterationEvent(), observer);
constexpr unsigned int numberOfLevels = 1;
RegistrationType::ShrinkFactorsArrayType shrinkFactorsPerLevel;
shrinkFactorsPerLevel.SetSize(1);
shrinkFactorsPerLevel[0] = 1;
RegistrationType::SmoothingSigmasArrayType smoothingSigmasPerLevel;
smoothingSigmasPerLevel.SetSize(1);
smoothingSigmasPerLevel[0] = 0;
registration->SetNumberOfLevels(numberOfLevels);
registration->SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel);
registration->SetShrinkFactorsPerLevel(shrinkFactorsPerLevel);
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;
}
const TransformType::ParametersType finalParameters =
registration->GetOutput()->Get()->GetParameters();
const double finalRotationCenterX = transform->GetCenter()[0];
const double finalRotationCenterY = transform->GetCenter()[1];
const double finalTranslationX = finalParameters[4];
const double finalTranslationY = finalParameters[5];
const unsigned int numberOfIterations = optimizer->GetCurrentIteration();
const double bestValue = optimizer->GetValue();
std::cout << "Result = " << std::endl;
std::cout << " Center X = " << finalRotationCenterX << std::endl;
std::cout << " Center Y = " << finalRotationCenterY << std::endl;
std::cout << " Translation X = " << finalTranslationX << std::endl;
std::cout << " Translation Y = " << finalTranslationY << std::endl;
std::cout << " Iterations = " << numberOfIterations << std::endl;
std::cout << " Metric value = " << bestValue << std::endl;
vnl_matrix<double> p(2, 2);
p[0][0] = static_cast<double>(finalParameters[0]);
p[0][1] = static_cast<double>(finalParameters[1]);
p[1][0] = static_cast<double>(finalParameters[2]);
p[1][1] = static_cast<double>(finalParameters[3]);
vnl_svd<double> svd(p);
vnl_matrix<double> r(2, 2);
r = svd.U() * vnl_transpose(svd.V());
const double angle = std::asin(r[1][0]);
std::cout << " Scale 1 = " << svd.W(0) << std::endl;
std::cout << " Scale 2 = " << svd.W(1) << std::endl;
std::cout << " Angle (degrees) = " << angleInDegrees << std::endl;
using ResampleFilterType =
auto resampler = ResampleFilterType::New();
resampler->SetTransform(transform);
resampler->SetInput(movingImageReader->GetOutput());
const FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();
resampler->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
resampler->SetOutputOrigin(fixedImage->GetOrigin());
resampler->SetOutputSpacing(fixedImage->GetSpacing());
resampler->SetOutputDirection(fixedImage->GetDirection());
resampler->SetDefaultPixelValue(100);
using OutputPixelType = unsigned char;
using CastFilterType =
auto writer = WriterType::New();
auto caster = CastFilterType::New();
writer->SetFileName(argv[3]);
caster->SetInput(resampler->GetOutput());
writer->SetInput(caster->GetOutput());
writer->Update();
using DifferenceFilterType =
auto difference = DifferenceFilterType::New();
difference->SetInput1(fixedImageReader->GetOutput());
difference->SetInput2(resampler->GetOutput());
auto writer2 = WriterType::New();
using RescalerType =
auto intensityRescaler = RescalerType::New();
intensityRescaler->SetInput(difference->GetOutput());
intensityRescaler->SetOutputMinimum(0);
intensityRescaler->SetOutputMaximum(255);
writer2->SetInput(intensityRescaler->GetOutput());
resampler->SetDefaultPixelValue(1);
if (argc > 5)
{
writer2->SetFileName(argv[5]);
writer2->Update();
}
auto identity = IdentityTransformType::New();
if (argc > 4)
{
resampler->SetTransform(identity);
writer2->SetFileName(argv[4]);
writer2->Update();
}
return EXIT_SUCCESS;
}
Casts input pixels to output pixel type.
Superclass for callback/observer methods.
virtual void Execute(Object *caller, const EventObject &event)=0
Abstraction of the Events used to communicating among filters and with GUIs.
Standard exception handling object.
Data source that reads image data from a single file.
Writes image data to a single file.
Templated n-dimensional image class.
Class implementing a mean squares metric.
Base class for most ITK classes.
Regular Step Gradient descent optimizer.
Resample an image via a coordinate transform.
Applies a linear transformation to the intensity levels of the input Image.
Implements transparent reference counting.
Pixel-wise subtraction of two images.
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
SmartPointer< Self > Pointer
constexpr unsigned int Dimension
static constexpr double pi