{
public:
using Self = CommandIterationUpdate;
protected:
CommandIterationUpdate() = default;
public:
using OptimizerPointer = const OptimizerType *;
void
{
}
void
{
auto optimizer = static_cast<OptimizerPointer>(object);
if (typeid(event) != typeid(itk::IterationEvent))
{
return;
}
OptimizerType::DerivativeType gradient = optimizer->GetGradient();
OptimizerType::ScalesType scales = optimizer->GetScales();
double magnitude2 = 0.0;
for (unsigned int i = 0; i < gradient.size(); ++i)
{
const double fc = gradient[i] / scales[i];
magnitude2 += fc * fc;
}
const double gradientMagnitude = std::sqrt(magnitude2);
std::cout << optimizer->GetCurrentIteration() << " ";
std::cout << optimizer->GetValue() << " ";
std::cout << gradientMagnitude << " ";
std::cout << optimizer->GetCurrentPosition() << std::endl;
}
};
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Missing argument" << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " movingImageFileName [initialX initialY] "
<< std::endl;
std::cerr << "[rasterizedObjectFileName] [BoxSizeX BoxSizeY]"
<< std::endl;
return EXIT_FAILURE;
}
using MaskPixelType = unsigned char;
using SpatialObjectToImageFilterType =
using NarrowBandFilterType =
FixedPointSetType>;
using PixelType = short;
using MaskPixelType = unsigned char;
using ParametersType = TransformType::ParametersType;
using LinearInterpolatorType =
using MetricType =
ImageType>;
using OptimizerScalesType = OptimizerType::ScalesType;
using RegistrationType =
using IterationObserverType = CommandIterationUpdate;
SpatialObjectType::Pointer spatialObject;
TransformType::Pointer transform;
OptimizerType::Pointer optimizer;
IterationObserverType::Pointer iterationObserver;
LinearInterpolatorType::Pointer linearInterpolator;
MetricType::Pointer metric;
RegistrationType::Pointer registrationMethod;
ImageReaderType::Pointer movingImageReader;
FixedPointSetType::Pointer fixedPointSet;
ImageType::ConstPointer movingImage;
SpatialObjectToImageFilterType::Pointer rasterizationFilter;
NarrowBandFilterType::Pointer narrowBandPointSetFilter;
metric = MetricType::New();
transform = TransformType::New();
optimizer = OptimizerType::New();
linearInterpolator = LinearInterpolatorType::New();
registrationMethod = RegistrationType::New();
iterationObserver = IterationObserverType::New();
spatialObject = SpatialObjectType::New();
rasterizationFilter = SpatialObjectToImageFilterType::New();
narrowBandPointSetFilter = NarrowBandFilterType::New();
movingImageReader = ImageReaderType::New();
movingImageReader->SetFileName(argv[1]);
try
{
movingImageReader->Update();
}
{
std::cerr << "Problem reading Moving image from = " << std::endl;
std::cerr << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
movingImage = movingImageReader->GetOutput();
SpatialObjectType::SizeType boxSize;
boxSize[0] = 60.0;
boxSize[1] = 60.0;
if (argc > 6)
{
boxSize[0] = std::stod(argv[5]);
boxSize[1] = std::stod(argv[6]);
}
spatialObject->SetSizeInObjectSpace(boxSize);
const ImageType::RegionType region =
movingImage->GetLargestPossibleRegion();
ImageType::SizeType imageSize = region.GetSize();
ImageType::SpacingType spacing = movingImage->GetSpacing();
ImageType::PointType origin;
origin[0] = (boxSize[0] - imageSize[0] * spacing[0]) / 2.0;
origin[1] = (boxSize[1] - imageSize[1] * spacing[1]) / 2.0;
rasterizationFilter->SetInput(spatialObject);
rasterizationFilter->SetSize(imageSize);
rasterizationFilter->SetSpacing(spacing);
rasterizationFilter->SetOrigin(origin);
narrowBandPointSetFilter->SetBandWidth(5.0);
narrowBandPointSetFilter->SetInput(rasterizationFilter->GetOutput());
narrowBandPointSetFilter->Update();
if (argc > 4)
{
auto maskWriter = MaskWriterType::New();
maskWriter->SetInput(rasterizationFilter->GetOutput());
maskWriter->SetFileName(argv[4]);
maskWriter->Update();
}
fixedPointSet = narrowBandPointSetFilter->GetOutput();
fixedPointSet->Print(std::cout);
registrationMethod->SetOptimizer(optimizer);
registrationMethod->SetInterpolator(linearInterpolator);
registrationMethod->SetMetric(metric);
registrationMethod->SetTransform(transform);
registrationMethod->SetMovingImage(movingImage);
registrationMethod->SetFixedPointSet(fixedPointSet);
optimizer->SetMaximumStepLength(1.00);
optimizer->SetMinimumStepLength(0.001);
optimizer->SetNumberOfIterations(300);
optimizer->SetRelaxationFactor(0.90);
optimizer->SetGradientMagnitudeTolerance(0.05);
optimizer->MinimizeOn();
optimizer->AddObserver(itk::IterationEvent(), iterationObserver);
TransformType::TranslationType initialTranslation;
initialTranslation[0] = 0.0;
initialTranslation[1] = 0.0;
if (argc >= 4)
{
initialTranslation[0] = std::stod(argv[2]);
initialTranslation[1] = std::stod(argv[3]);
}
TransformType::OutputPointType rotationCenter;
rotationCenter[0] = boxSize[0] / 2.0;
rotationCenter[1] = boxSize[1] / 2.0;
transform->SetIdentity();
transform->SetCenter(rotationCenter);
transform->SetTranslation(initialTranslation);
registrationMethod->SetInitialTransformParameters(
transform->GetParameters());
OptimizerScalesType optimizerScales(transform->GetNumberOfParameters());
constexpr double translationScale = 1.0 / 1000.0;
optimizerScales[0] = 1.0;
optimizerScales[1] = translationScale;
optimizerScales[2] = translationScale;
optimizer->SetScales(optimizerScales);
try
{
registrationMethod->Update();
std::cout
<< "Optimizer stop condition: "
<< registrationMethod->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
{
std::cerr << "Problem found during the registration" << std::endl;
std::cerr << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
const ParametersType transformParameters =
registrationMethod->GetLastTransformParameters();
const TransformType::OutputPointType center = transform->GetCenter();
std::cout << "Registration parameter = " << std::endl;
std::cout << "Rotation center = " << center << std::endl;
std::cout << "Parameters = " << transformParameters << std::endl;
return EXIT_SUCCESS;
}
Generate a PointSet containing the narrow band around the edges of a input binary image.
The class may be used to represent N-dimensional boxes. In two dimensions it is a rectangle,...
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.
Linearly interpolate an image at specified positions.
Computes similarity between pixel values of a point set and intensity values of an image.
Base class for most ITK classes.
Base class for PointSet to Image Registration Methods.
A superclass of the N-dimensional mesh structure; supports point (geometric coordinate and attribute)...
Implement a gradient descent optimizer.
Implements transparent reference counting.
Base class for filters that take a SpatialObject as input and produce an image as output....
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
SmartPointer< Self > Pointer
constexpr unsigned int Dimension