Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add support for multiple metrics and regularization #482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef itkCombinationImageToImageMetric_h
#define itkCombinationImageToImageMetric_h

#include "itkTransformPenaltyTerm.h"
#include "itkAdvancedImageToImageMetric.h"
#include "itkSingleValuedPointSetToPointSetMetric.h"

Expand Down Expand Up @@ -135,6 +136,10 @@ class CombinationImageToImageMetric : public AdvancedImageToImageMetric<TFixedIm
typedef typename MovingImageType::RegionType MovingImageRegionType;
typedef FixedArray<double, itkGetStaticConstMacro(MovingImageDimension)> MovingImageDerivativeScalesType;

/** Typedef for transform penalty metrics. */
typedef TransformPenaltyTerm<TFixedImage> TransformMetricType;
typedef typename TransformMetricType::Pointer TransformMetricPointer;

/** Typedef for the PointSetMetric. */
typedef PointSet<CoordinateRepresentationType,
TFixedImage::ImageDimension,
Expand Down Expand Up @@ -170,6 +175,9 @@ class CombinationImageToImageMetric : public AdvancedImageToImageMetric<TFixedIm

/** Get the number of metrics to combine. */
itkGetConstMacro(NumberOfMetrics, unsigned int);
itkGetConstMacro(NumberOfImageMetrics, unsigned int);
itkGetConstMacro(NumberOfTransformMetrics, unsigned int);
itkGetConstMacro(NumberOfPointSetMetrics, unsigned int);

/** Set metric i. It may be a SingleValuedCostFunction, instead of
* a ImageToImageMetric, but the first one should be an
Expand Down Expand Up @@ -447,6 +455,9 @@ class CombinationImageToImageMetric : public AdvancedImageToImageMetric<TFixedIm

/** Store the metrics and the corresponding weights. */
unsigned int m_NumberOfMetrics;
unsigned int m_NumberOfImageMetrics;
unsigned int m_NumberOfTransformMetrics;
unsigned int m_NumberOfPointSetMetrics;
std::vector<SingleValuedCostFunctionPointer> m_Metrics;
std::vector<double> m_MetricWeights;
std::vector<double> m_MetricRelativeWeights;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ template <class TFixedImage, class TMovingImage>
CombinationImageToImageMetric<TFixedImage, TMovingImage>::CombinationImageToImageMetric()
{
this->m_NumberOfMetrics = 0;
this->m_NumberOfImageMetrics = 0;
this->m_NumberOfTransformMetrics = 0;
this->m_NumberOfPointSetMetrics = 0;
this->m_UseRelativeWeights = false;
this->ComputeGradientOff();

Expand All @@ -164,6 +167,9 @@ CombinationImageToImageMetric<TFixedImage, TMovingImage>::PrintSelf(std::ostream

/** Add debugging information. */
os << "NumberOfMetrics: " << this->m_NumberOfMetrics << std::endl;
os << "NumberOfImageMetrics: " << this->m_NumberOfImageMetrics << std::endl;
os << "NumberOfTransformMetrics: " << this->m_NumberOfTransformMetrics << std::endl;
os << "NumberOfPointSetMetrics: " << this->m_NumberOfPointSetMetrics << std::endl;
for (unsigned int i = 0; i < this->m_NumberOfMetrics; ++i)
{
os << "Metric " << i << ":\n";
Expand Down Expand Up @@ -280,6 +286,30 @@ CombinationImageToImageMetric<TFixedImage, TMovingImage>::SetMetric(SingleValued

if (metric != this->m_Metrics[pos])
{
PointSetMetricType * testPtr1 = dynamic_cast<PointSetMetricType *>(metric);
TransformMetricType * testPtr2 = dynamic_cast<TransformMetricType *>(metric);
if (!this->m_Metrics[pos]) // if the metric has not been set
{
// Update numberOfMetric counters
if (testPtr1)
{
this->m_NumberOfPointSetMetrics++;
}
else if (testPtr2)
{
this->m_NumberOfTransformMetrics++;
}
else
{
this->m_NumberOfImageMetrics++;
}
}
else
{
// /TODO: Logic to update numberOfMetric counters if a metric is replaced?
// not needed for elastix exe ...
}

this->m_Metrics[pos] = metric;
this->Modified();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,18 +565,18 @@ MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::Ch
}

/** nrofmetrics >= nrofinterpolators >= nrofpyramids >= nofimages */
unsigned int nrOfMetrics = this->GetCombinationMetric()->GetNumberOfMetrics();
if (this->GetNumberOfInterpolators() > nrOfMetrics)
unsigned int nrOfImageMetrics = this->GetCombinationMetric()->GetNumberOfImageMetrics();
if (this->GetNumberOfInterpolators() > nrOfImageMetrics)
{
itkExceptionMacro(<< "NumberOfInterpolators can not exceed the "
<< "NumberOfMetrics in the CombinationMetric!");
}
if (this->GetNumberOfFixedImagePyramids() > nrOfMetrics)
if (this->GetNumberOfFixedImagePyramids() > nrOfImageMetrics)
{
itkExceptionMacro(<< "NumberOfFixedImagePyramids can not exceed the "
<< "NumberOfMetrics in the CombinationMetric!");
}
if (this->GetNumberOfMovingImagePyramids() > nrOfMetrics)
if (this->GetNumberOfMovingImagePyramids() > nrOfImageMetrics)
{
itkExceptionMacro(<< "NumberOfMovingImagePyramids can not exceed the "
<< "NumberOfMetrics in the CombinationMetric!");
Expand All @@ -587,31 +587,36 @@ MultiMetricMultiResolutionImageRegistrationMethod<TFixedImage, TMovingImage>::Ch
<< "NumberOfInterpolators!");
}

/** For all components: ==nrofmetrics of ==1. */
if ((this->GetNumberOfInterpolators() != 1) && (this->GetNumberOfInterpolators() != nrOfMetrics))
/** For all components: ==nrOfImageMetrics or == 1. */
if ((this->GetNumberOfInterpolators() != 1) && (this->GetNumberOfInterpolators() != nrOfImageMetrics))
{
itkExceptionMacro(<< "The NumberOfInterpolators should equal 1 "
<< "or equal the NumberOfMetrics");
<< "or equal the NumberOfImageMetrics ("
<< nrOfImageMetrics << ")");
}
if ((this->GetNumberOfFixedImagePyramids() != 1) && (this->GetNumberOfFixedImagePyramids() != nrOfMetrics))
if ((this->GetNumberOfFixedImagePyramids() != 1) && (this->GetNumberOfFixedImagePyramids() != nrOfImageMetrics))
{
itkExceptionMacro(<< "The NumberOfFixedImagePyramids should equal 1 "
<< "or equal the NumberOfMetrics");
<< "or equal the NumberOfImageMetrics ("
<< nrOfImageMetrics << ")");
}
if ((this->GetNumberOfMovingImagePyramids() != 1) && (this->GetNumberOfMovingImagePyramids() != nrOfMetrics))
if ((this->GetNumberOfMovingImagePyramids() != 1) && (this->GetNumberOfMovingImagePyramids() != nrOfImageMetrics))
{
itkExceptionMacro(<< "The NumberOfMovingImagePyramids should equal 1 "
<< "or equal the NumberOfMetrics");
<< "or equal the NumberOfImageMetrics ("
<< nrOfImageMetrics << ")");
}
if ((this->GetNumberOfFixedImages() != 1) && (this->GetNumberOfFixedImages() != nrOfMetrics))
if ((this->GetNumberOfFixedImages() != 1) && (this->GetNumberOfFixedImages() != nrOfImageMetrics))
{
itkExceptionMacro(<< "The NumberOfFixedImages should equal 1 "
<< "or equal the NumberOfMetrics");
<< "or equal the NumberOfImageMetrics ("
<< nrOfImageMetrics << ")");
}
if ((this->GetNumberOfMovingImages() != 1) && (this->GetNumberOfMovingImages() != nrOfMetrics))
if ((this->GetNumberOfMovingImages() != 1) && (this->GetNumberOfMovingImages() != nrOfImageMetrics))
{
itkExceptionMacro(<< "The NumberOfMovingImages should equal 1 "
<< "or equal the NumberOfMetrics");
<< "or equal the NumberOfImageMetrics ("
<< nrOfImageMetrics << ")");
}

} // end CheckOnInitialize()
Expand Down