Skip to content

Commit

Permalink
PERF: Use FastEvaluate in MattesMutualInformationImageToImageMetric + v4
Browse files Browse the repository at this point in the history
Removed the two dynamically allocated "cubic BSpline kernel" data
members from both `MattesMutualInformationImageToImageMetric` and
`MattesMutualInformationImageToImageMetricv4`. Replaced the virtual
`Evaluate` function calls by faster static member function calls, to
the newly added `FastEvaluate` member functions of the corresponding
`KernelFunction` classes.

Follow-up to pull request #2716
commit c23944b
"PERF: Remove BSplineInterpolationWeightFunction Kernel, use FastEvaluate"
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 14, 2021
1 parent 760ae12 commit 9961ccd
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ class ITK_TEMPLATE_EXPORT MattesMutualInformationImageToImageMetric
PDFValueType m_FixedImageBinSize{ 0.0 };
PDFValueType m_MovingImageBinSize{ 0.0 };

/** Cubic BSpline kernel for computing Parzen histograms. */
typename CubicBSplineFunctionType::Pointer m_CubicBSplineKernel;
typename CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel;

/** Helper array for storing the values of the JointPDF ratios. */
using PRatioType = PDFValueType;
using PRatioArrayType = Array2D<PRatioType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ namespace itk
*/
template <typename TFixedImage, typename TMovingImage>
MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::MattesMutualInformationImageToImageMetric()
: m_CubicBSplineKernel(nullptr)
, m_CubicBSplineDerivativeKernel(nullptr)
, m_PRatioArray(0, 0)
: m_PRatioArray(0, 0)
,
// Initialize memory
m_MovingImageMarginalPDF(0)
Expand Down Expand Up @@ -338,11 +336,6 @@ MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::Initialize
this->m_MMIMetricPerThreadVariables[workUnitID].MetricDerivative.Fill(NumericTraits<MeasureType>::ZeroValue());
}
}
/**
* Setup the kernels used for the Parzen windows.
*/
this->m_CubicBSplineKernel = CubicBSplineFunctionType::New();
this->m_CubicBSplineDerivativeKernel = CubicBSplineDerivativeFunctionType::New();

/**
* Pre-compute the fixed image parzen window index for
Expand Down Expand Up @@ -460,7 +453,7 @@ MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValueTh

while (pdfMovingIndex <= pdfMovingIndexMax)
{
*(pdfPtr++) += static_cast<PDFValueType>(this->m_CubicBSplineKernel->Evaluate(movingImageParzenWindowArg));
*(pdfPtr++) += CubicBSplineFunctionType::FastEvaluate(movingImageParzenWindowArg);
movingImageParzenWindowArg += 1;
++pdfMovingIndex;
}
Expand Down Expand Up @@ -705,13 +698,13 @@ MattesMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValueAn

while (pdfMovingIndex <= pdfMovingIndexMax)
{
*(pdfPtr++) += static_cast<PDFValueType>(this->m_CubicBSplineKernel->Evaluate(movingImageParzenWindowArg));
*(pdfPtr++) += CubicBSplineFunctionType::FastEvaluate(movingImageParzenWindowArg);

if (this->m_UseExplicitPDFDerivatives || this->m_ImplicitDerivativesSecondPass)
{
// Compute the cubicBSplineDerivative for later repeated use.
const PDFValueType cubicBSplineDerivativeValue =
this->m_CubicBSplineDerivativeKernel->Evaluate(movingImageParzenWindowArg);
CubicBSplineDerivativeFunctionType::FastEvaluate(movingImageParzenWindowArg);

// Compute PDF derivative contribution.
this->ComputePDFDerivatives(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,6 @@ class ITK_TEMPLATE_EXPORT MattesMutualInformationImageToImageMetricv4
PDFValueType m_FixedImageBinSize;
PDFValueType m_MovingImageBinSize;

/** Cubic BSpline kernel for computing Parzen histograms. */
typename CubicBSplineFunctionType::Pointer m_CubicBSplineKernel;
typename CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel;

/** Helper array for storing the values of the JointPDF ratios. */
using PRatioType = PDFValueType;
using PRatioArrayType = std::vector<PRatioType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ MattesMutualInformationImageToImageMetricv4<TFixedImage,
, m_MovingImageTrueMax(0.0)
, m_FixedImageBinSize(0.0)
, m_MovingImageBinSize(0.0)
, m_CubicBSplineKernel(nullptr)
, m_CubicBSplineDerivativeKernel(nullptr)
, m_PRatioArray(0)
,
// Initialize memory
Expand All @@ -60,8 +58,6 @@ MattesMutualInformationImageToImageMetricv4<TFixedImage,
// ImageToImageMetricv4 to use.
this->m_DenseGetValueAndDerivativeThreader = MattesMutualInformationDenseGetValueAndDerivativeThreaderType::New();
this->m_SparseGetValueAndDerivativeThreader = MattesMutualInformationSparseGetValueAndDerivativeThreaderType::New();
this->m_CubicBSplineKernel = CubicBSplineFunctionType::New();
this->m_CubicBSplineDerivativeKernel = CubicBSplineDerivativeFunctionType::New();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,14 @@ MattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader<
MovingTransformType::TransformCategoryEnum::DisplacementField;
while (pdfMovingIndex <= pdfMovingIndexMax)
{
const auto val =
static_cast<PDFValueType>(this->m_MattesAssociate->m_CubicBSplineKernel->Evaluate(movingImageParzenWindowArg));
const auto val = CubicBSplineFunctionType::FastEvaluate(movingImageParzenWindowArg);
*(pdfPtr++) += val;

if (doComputeDerivative)
{
// Compute the cubicBSplineDerivative for later repeated use.
const PDFValueType cubicBSplineDerivativeValue =
this->m_MattesAssociate->m_CubicBSplineDerivativeKernel->Evaluate(movingImageParzenWindowArg);

CubicBSplineDerivativeFunctionType::FastEvaluate(movingImageParzenWindowArg);

if (transformIsDisplacement)
{
Expand Down

0 comments on commit 9961ccd

Please sign in to comment.