ITK
5.2.0
Insight Toolkit
Examples/RegistrationITKv4/DeformableRegistration8.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates the use of the \doxygen{BSplineTransform}
// class for performing registration of two $3D$ images and for the case of
// multi-modality images. The image metric of choice in this case is the
// \doxygen{MattesMutualInformationImageToImageMetricv4}.
//
// \index{itk::BSplineTransform}
// \index{itk::BSplineTransform!DeformableRegistration}
// \index{itk::LBFGSBOptimizerv4}
//
// Software Guide : EndLatex
#include "
itkImageRegistrationMethodv4.h
"
#include "
itkTimeProbesCollectorBase.h
"
#include "
itkMemoryProbesCollectorBase.h
"
// Software Guide : BeginLatex
//
// The following are the most relevant headers to this example.
//
// \index{itk::BSplineTransform!header}
// \index{itk::LBFGSBOptimizerv4!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "
itkBSplineTransform.h
"
#include "
itkLBFGSBOptimizerv4.h
"
#include "
itkMattesMutualInformationImageToImageMetricv4.h
"
// Software Guide : EndCodeSnippet
#include "
itkImageFileReader.h
"
#include "
itkImageFileWriter.h
"
#include "
itkResampleImageFilter.h
"
#include "
itkCastImageFilter.h
"
#include "
itkSquaredDifferenceImageFilter.h
"
#include "
itkTransformFileReader.h
"
#include "
itkBSplineTransformInitializer.h
"
#include "
itkTransformToDisplacementFieldFilter.h
"
// The following section of code implements a Command observer
// used to monitor the evolution of the registration process.
//
#include "
itkCommand.h
"
class
CommandIterationUpdate :
public
itk::Command
{
public
:
using
Self = CommandIterationUpdate;
using
Superclass =
itk::Command
;
using
Pointer =
itk::SmartPointer<Self>
;
itkNewMacro(Self);
protected
:
CommandIterationUpdate() =
default
;
public
:
using
OptimizerType =
itk::LBFGSBOptimizerv4
;
using
OptimizerPointer =
const
OptimizerType *;
void
Execute
(
itk::Object
* caller,
const
itk::EventObject
& event)
override
{
Execute
((
const
itk::Object
*)caller, event);
}
void
Execute
(
const
itk::Object
*
object
,
const
itk::EventObject
& event)
override
{
auto
optimizer = static_cast<OptimizerPointer>(
object
);
if
(!(itk::IterationEvent().CheckEvent(&event)))
{
return
;
}
std::cout << optimizer->GetCurrentIteration() <<
" "
;
std::cout << optimizer->GetCurrentMetricValue() <<
" "
;
std::cout << optimizer->GetInfinityNormOfProjectedGradient() << 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 outputImagefile "
;
std::cerr <<
" [differenceOutputfile] [differenceBeforeRegistration] "
;
std::cerr <<
" [deformationField] "
;
std::cerr <<
" [filenameForFinalTransformParameters] "
"[numberOfGridNodesInOneDimension]"
;
std::cerr << std::endl;
return
EXIT_FAILURE;
}
constexpr
unsigned
int
ImageDimension = 3;
using
PixelType = float;
using
FixedImageType =
itk::Image<PixelType, ImageDimension>
;
using
MovingImageType =
itk::Image<PixelType, ImageDimension>
;
// Software Guide : BeginLatex
//
// We instantiate now the type of the \code{BSplineTransform} using
// as template parameters the type for coordinates representation, the
// dimension of the space, and the order of the BSpline.
//
// \index{BSplineTransform!New}
// \index{BSplineTransform!Instantiation}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const
unsigned
int
SpaceDimension = ImageDimension;
constexpr
unsigned
int
SplineOrder = 3;
using
CoordinateRepType = double;
using
TransformType =
itk::BSplineTransform<CoordinateRepType, SpaceDimension, SplineOrder>
;
// Software Guide : EndCodeSnippet
using
OptimizerType =
itk::LBFGSBOptimizerv4
;
using
MetricType =
itk::MattesMutualInformationImageToImageMetricv4
<FixedImageType,
MovingImageType>;
using
RegistrationType =
itk::ImageRegistrationMethodv4<FixedImageType, MovingImageType>
;
MetricType::Pointer metric = MetricType::New();
OptimizerType::Pointer optimizer = OptimizerType::New();
RegistrationType::Pointer registration = RegistrationType::New();
registration->SetMetric(metric);
registration->SetOptimizer(optimizer);
using
FixedImageReaderType =
itk::ImageFileReader<FixedImageType>
;
using
MovingImageReaderType =
itk::ImageFileReader<MovingImageType>
;
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();
// Software Guide : BeginLatex
//
// The transform object is constructed, initialized like previous example
// and passed to the registration method.
//
// \index{itk::ImageRegistrationMethodv4!SetInitialTransform()}
// \index{itk::ImageRegistrationMethodv4!InPlaceOn()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
TransformType::Pointer transform = TransformType::New();
// Software Guide : EndCodeSnippet
// Initialize the transform
unsigned
int
numberOfGridNodesInOneDimension = 5;
if
(argc > 8)
{
numberOfGridNodesInOneDimension = std::stoi(argv[8]);
}
using
InitializerType =
itk::BSplineTransformInitializer<TransformType, FixedImageType>
;
InitializerType::Pointer transformInitializer = InitializerType::New();
TransformType::MeshSizeType meshSize;
meshSize.Fill(numberOfGridNodesInOneDimension - SplineOrder);
transformInitializer->SetTransform(transform);
transformInitializer->SetImage(fixedImage);
transformInitializer->SetTransformDomainMeshSize(meshSize);
transformInitializer->InitializeTransform();
// Set transform to identity
transform->SetIdentity();
// Software Guide : BeginCodeSnippet
registration->SetInitialTransform(transform);
registration->InPlaceOn();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Next we set the parameters of the LBFGSB Optimizer.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const
unsigned
int
numParameters = transform->GetNumberOfParameters();
OptimizerType::BoundSelectionType boundSelect(numParameters);
OptimizerType::BoundValueType upperBound(numParameters);
OptimizerType::BoundValueType lowerBound(numParameters);
boundSelect.Fill(OptimizerType::UNBOUNDED);
upperBound.Fill(0.0);
lowerBound.Fill(0.0);
optimizer->SetBoundSelection(boundSelect);
optimizer->SetUpperBound(upperBound);
optimizer->SetLowerBound(lowerBound);
optimizer->SetCostFunctionConvergenceFactor(1.e7);
optimizer->SetGradientConvergenceTolerance(1
e
-6);
optimizer->SetNumberOfIterations(200);
optimizer->SetMaximumNumberOfFunctionEvaluations(30);
optimizer->SetMaximumNumberOfCorrections(5);
// Software Guide : EndCodeSnippet
// Create the Command observer and register it with the optimizer.
//
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
optimizer->AddObserver(itk::IterationEvent(), observer);
// A single level registration process is run using
// the shrink factor 1 and smoothing sigma 0.
//
constexpr
unsigned
int
numberOfLevels = 1;
RegistrationType::ShrinkFactorsArrayType shrinkFactorsPerLevel;
shrinkFactorsPerLevel.SetSize(numberOfLevels);
shrinkFactorsPerLevel[0] = 1;
RegistrationType::SmoothingSigmasArrayType smoothingSigmasPerLevel;
smoothingSigmasPerLevel.SetSize(numberOfLevels);
smoothingSigmasPerLevel[0] = 0;
registration->SetNumberOfLevels(numberOfLevels);
registration->SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel);
registration->SetShrinkFactorsPerLevel(shrinkFactorsPerLevel);
// Software Guide : BeginLatex
//
// Next we set the parameters of the Mattes Mutual Information Metric.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
metric->SetNumberOfHistogramBins(50);
// Software Guide : EndCodeSnippet
// Add time and memory probes
itk::TimeProbesCollectorBase
chronometer;
itk::MemoryProbesCollectorBase
memorymeter;
std::cout << std::endl <<
"Starting Registration"
<< std::endl;
try
{
memorymeter.
Start
(
"Registration"
);
chronometer.
Start
(
"Registration"
);
registration->Update();
chronometer.
Stop
(
"Registration"
);
memorymeter.
Stop
(
"Registration"
);
std::cout <<
"Optimizer stop condition = "
<< registration->GetOptimizer()->GetStopConditionDescription()
<< std::endl;
}
catch
(
const
itk::ExceptionObject & err)
{
std::cerr <<
"ExceptionObject caught !"
<< std::endl;
std::cerr << err << std::endl;
return
EXIT_FAILURE;
}
// Report the time and memory taken by the registration
chronometer.
Report
(std::cout);
memorymeter.
Report
(std::cout);
OptimizerType::ParametersType finalParameters = transform->GetParameters();
std::cout <<
"Last Transform Parameters"
<< std::endl;
std::cout << finalParameters << std::endl;
// Finally we use the last transform in order to resample the image.
//
using
ResampleFilterType =
itk::ResampleImageFilter<MovingImageType, FixedImageType>
;
ResampleFilterType::Pointer resample = ResampleFilterType::New();
resample->SetTransform(transform);
resample->SetInput(movingImageReader->GetOutput());
resample->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
resample->SetOutputOrigin(fixedImage->GetOrigin());
resample->SetOutputSpacing(fixedImage->GetSpacing());
resample->SetOutputDirection(fixedImage->GetDirection());
// This value is set to zero in order to make easier to perform
// regression testing in this example. However, for didactic
// exercise it will be better to set it to a medium gray value
// such as 100 or 128.
resample->SetDefaultPixelValue(0);
using
OutputPixelType =
signed
short;
using
OutputImageType =
itk::Image<OutputPixelType, ImageDimension>
;
using
CastFilterType =
itk::CastImageFilter<FixedImageType, OutputImageType>
;
using
WriterType =
itk::ImageFileWriter<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();
}
catch
(
const
itk::ExceptionObject & err)
{
std::cerr <<
"ExceptionObject caught !"
<< std::endl;
std::cerr << err << std::endl;
return
EXIT_FAILURE;
}
using
DifferenceFilterType =
itk::SquaredDifferenceImageFilter
<FixedImageType,
FixedImageType,
OutputImageType>;
DifferenceFilterType::Pointer difference = DifferenceFilterType::New();
WriterType::Pointer writer2 = WriterType::New();
writer2->SetInput(difference->GetOutput());
// Compute the difference image between the
// fixed and resampled moving image.
if
(argc > 4)
{
difference->SetInput1(fixedImageReader->GetOutput());
difference->SetInput2(resample->GetOutput());
writer2->SetFileName(argv[4]);
try
{
writer2->Update();
}
catch
(
const
itk::ExceptionObject & err)
{
std::cerr <<
"ExceptionObject caught !"
<< std::endl;
std::cerr << err << std::endl;
return
EXIT_FAILURE;
}
}
// Compute the difference image between the
// fixed and moving image before registration.
if
(argc > 5)
{
writer2->SetFileName(argv[5]);
difference->SetInput1(fixedImageReader->GetOutput());
difference->SetInput2(movingImageReader->GetOutput());
try
{
writer2->Update();
}
catch
(
const
itk::ExceptionObject & err)
{
std::cerr <<
"ExceptionObject caught !"
<< std::endl;
std::cerr << err << std::endl;
return
EXIT_FAILURE;
}
}
// Generate the explicit deformation field resulting from
// the registration.
if
(argc > 6)
{
using
VectorPixelType =
itk::Vector<float, ImageDimension>
;
using
DisplacementFieldImageType =
itk::Image<VectorPixelType, ImageDimension>
;
using
DisplacementFieldGeneratorType =
itk::TransformToDisplacementFieldFilter
<DisplacementFieldImageType,
CoordinateRepType>;
DisplacementFieldGeneratorType::Pointer dispfieldGenerator =
DisplacementFieldGeneratorType::New();
dispfieldGenerator->UseReferenceImageOn();
dispfieldGenerator->SetReferenceImage(fixedImage);
dispfieldGenerator->SetTransform(transform);
try
{
dispfieldGenerator->Update();
}
catch
(
const
itk::ExceptionObject & err)
{
std::cerr <<
"Exception detected while generating deformation field"
;
std::cerr <<
" : "
<< err << std::endl;
return
EXIT_FAILURE;
}
using
FieldWriterType =
itk::ImageFileWriter<DisplacementFieldImageType>
;
FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
fieldWriter->SetInput(dispfieldGenerator->GetOutput());
fieldWriter->SetFileName(argv[6]);
try
{
fieldWriter->Update();
}
catch
(
const
itk::ExceptionObject & excp)
{
std::cerr <<
"Exception thrown "
<< std::endl;
std::cerr << excp << std::endl;
return
EXIT_FAILURE;
}
}
// Optionally, save the transform parameters in a file
if
(argc > 7)
{
std::ofstream parametersFile;
parametersFile.open(argv[7]);
parametersFile << finalParameters << std::endl;
parametersFile.close();
}
return
EXIT_SUCCESS;
}
itk::CastImageFilter
Casts input pixels to output pixel type.
Definition:
itkCastImageFilter.h:104
itkTimeProbesCollectorBase.h
itk::SquaredDifferenceImageFilter
Implements pixel-wise the computation of squared difference.
Definition:
itkSquaredDifferenceImageFilter.h:88
itkTransformToDisplacementFieldFilter.h
itkTransformFileReader.h
itk::LBFGSBOptimizerv4
Limited memory Broyden Fletcher Goldfarb Shannon minimization with simple bounds.
Definition:
itkLBFGSBOptimizerv4.h:67
itk::ResourceProbesCollectorBase::Start
virtual void Start(const char *id)
itk::MemoryProbesCollectorBase
Aggregates a set of memory probes.
Definition:
itkMemoryProbesCollectorBase.h:37
itk::Vector
A templated class holding a n-Dimensional vector.
Definition:
itkVector.h:62
itkImageFileReader.h
itk::ResourceProbesCollectorBase::Report
virtual void Report(std::ostream &os=std::cout, bool printSystemInfo=true, bool printReportHead=true, bool useTabs=false)
itk::SmartPointer< Self >
itkCastImageFilter.h
itk::TransformToDisplacementFieldFilter
Generate a displacement field from a coordinate transform.
Definition:
itkTransformToDisplacementFieldFilter.h:55
itkLBFGSBOptimizerv4.h
itk::BSplineTransformInitializer
BSplineTransformInitializer is a helper class intended to initialize the control point grid such that...
Definition:
itkBSplineTransformInitializer.h:41
itkImageRegistrationMethodv4.h
itkMemoryProbesCollectorBase.h
itk::ImageFileReader
Data source that reads image data from a single file.
Definition:
itkImageFileReader.h:75
itk::Command
Superclass for callback/observer methods.
Definition:
itkCommand.h:45
itk::BSplineTransform
Deformable transform using a BSpline representation.
Definition:
itkBSplineTransform.h:103
itk::ImageFileWriter
Writes image data to a single file.
Definition:
itkImageFileWriter.h:88
itkBSplineTransform.h
itk::Command
class ITK_FORWARD_EXPORT Command
Definition:
itkObject.h:43
itk::ResourceProbesCollectorBase::Stop
virtual void Stop(const char *id)
itk::Command::Execute
virtual void Execute(Object *caller, const EventObject &event)=0
itkImageFileWriter.h
itkSquaredDifferenceImageFilter.h
itk::ImageRegistrationMethodv4
Interface method for the current registration framework.
Definition:
itkImageRegistrationMethodv4.h:117
itk::ResampleImageFilter
Resample an image via a coordinate transform.
Definition:
itkResampleImageFilter.h:90
itk::Object
Base class for most ITK classes.
Definition:
itkObject.h:62
itkBSplineTransformInitializer.h
itk::Math::e
static constexpr double e
Definition:
itkMath.h:54
itk::Image
Templated n-dimensional image class.
Definition:
itkImage.h:86
itk::EventObject
Abstraction of the Events used to communicating among filters and with GUIs.
Definition:
itkEventObject.h:57
itkResampleImageFilter.h
itkCommand.h
itkMattesMutualInformationImageToImageMetricv4.h
itk::MattesMutualInformationImageToImageMetricv4
Computes the mutual information between two images to be registered using the method of Mattes et al.
Definition:
itkMattesMutualInformationImageToImageMetricv4.h:103
itk::TimeProbesCollectorBase
Aggregates a set of time probes.
Definition:
itkTimeProbesCollectorBase.h:38
Generated on Thu Apr 1 2021 01:33:45 for ITK by
1.8.16