{
public:
using Self = CommandIterationUpdate;
protected:
CommandIterationUpdate() { m_LastMetricValue = 0.0; }
public:
using OptimizerPointer = const OptimizerType *;
void
{
}
void
{
auto optimizer = static_cast<OptimizerPointer>(object);
if (!itk::IterationEvent().CheckEvent(&event))
{
return;
}
const double currentValue = optimizer->GetValue();
{
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << currentValue << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;
m_LastMetricValue = currentValue;
}
}
private:
double m_LastMetricValue;
};
int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Missing Parameters " << std::endl;
std::cerr << "Usage: " << argv[0];
std::cerr << " fixedImageFile movingImageFile ";
std::cerr << "outputImagefile ";
std::cerr << "[samplingPercentage ] " << std::endl;
return EXIT_FAILURE;
}
using PixelType = float;
using RegistrationType =
using MetricType =
MovingImageType>;
auto transform = TransformType::New();
auto optimizer = OptimizerType::New();
auto metric = MetricType::New();
auto registration = RegistrationType::New();
registration->SetOptimizer(optimizer);
registration->SetMetric(metric);
metric->SetNumberOfHistogramBins(20);
double samplingPercentage = 0.20;
if (argc > 4)
{
samplingPercentage = std::stod(argv[4]);
}
registration->SetMetricSamplingPercentage(samplingPercentage);
constexpr RegistrationType::MetricSamplingStrategyEnum samplingStrategy =
RegistrationType::MetricSamplingStrategyEnum::RANDOM;
registration->SetMetricSamplingStrategy(samplingStrategy);
auto fixedImageReader = FixedImageReaderType::New();
auto movingImageReader = MovingImageReaderType::New();
fixedImageReader->SetFileName(argv[1]);
movingImageReader->SetFileName(argv[2]);
registration->SetFixedImage(fixedImageReader->GetOutput());
registration->SetMovingImage(movingImageReader->GetOutput());
fixedImageReader->Update();
using ParametersType = TransformType::ParametersType;
ParametersType initialParameters(transform->GetNumberOfParameters());
initialParameters[0] = 0.0;
initialParameters[1] = 0.0;
transform->SetParameters(initialParameters);
registration->SetInitialTransform(transform);
registration->InPlaceOn();
auto generator = GeneratorType::New();
generator->Initialize(12345);
optimizer->SetNormalVariateGenerator(generator);
optimizer->Initialize(10);
optimizer->SetEpsilon(1.0);
optimizer->SetMaximumIteration(4000);
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);
auto observer = CommandIterationUpdate::New();
optimizer->AddObserver(itk::IterationEvent(), observer);
try
{
registration->Update();
std::cout << "Registration completed!" << std::endl;
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 = transform->GetParameters();
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;
using ResampleFilterType =
auto resample = ResampleFilterType::New();
resample->SetTransform(transform);
resample->SetInput(movingImageReader->GetOutput());
const FixedImageType::Pointer fixedImage = fixedImageReader->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;
using CastFilterType =
auto writer = WriterType::New();
auto caster = CastFilterType::New();
writer->SetFileName(argv[3]);
caster->SetInput(resample->GetOutput());
writer->SetInput(caster->GetOutput());
writer->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.
Interface method for the current registration framework.
Templated n-dimensional image class.
Base class for most ITK classes.
1+1 evolutionary strategy optimizer
Resample an image via a coordinate transform.
Implements transparent reference counting.
Normal random variate generator.
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
SmartPointer< Self > Pointer
constexpr unsigned int Dimension