diff --git a/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.h b/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.h index a5dcdd11f45..bbf6348fc49 100644 --- a/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.h +++ b/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.h @@ -137,6 +137,22 @@ class ITK_TEMPLATE_EXPORT GradientDifferenceImageToImageMetric : public ImageToI itkSetMacro(DerivativeDelta, double); itkGetConstReferenceMacro(DerivativeDelta, double); /** @ITKEndGrouping */ + + /** Allows specifying whether or not the Sobel operators should use the legacy coordinate values, compatible with ITK + * <= 5.4. + * \sa SobelOperator::SetUseLegacyCoefficients */ + void + UseLegacySobelOperatorCoordinates(const bool useLegacyCoefficients); + + /** Tells whether or not the Sobel operators are using the legacy coordinate values, compatible with ITK <= 5.4. + * \sa SobelOperator::SetUseLegacyCoefficients */ + bool + IsUsingLegacySobelOperatorCoordinates() const + { + // It is sufficient to just check the first operator, because this property is the same for all operators. + return m_FixedSobelOperators[0].IsUsingLegacyCoefficients(); + } + protected: GradientDifferenceImageToImageMetric(); ~GradientDifferenceImageToImageMetric() override = default; diff --git a/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.hxx b/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.hxx index df6c1aa6b67..57974aefdd8 100644 --- a/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.hxx +++ b/Modules/Registration/Common/include/itkGradientDifferenceImageToImageMetric.hxx @@ -126,6 +126,27 @@ GradientDifferenceImageToImageMetric::Initialize() } } + +template +void +GradientDifferenceImageToImageMetric::UseLegacySobelOperatorCoordinates( + const bool useLegacyCoefficients) +{ + if (useLegacyCoefficients != IsUsingLegacySobelOperatorCoordinates()) + { + for (auto & sobelOperator : m_FixedSobelOperators) + { + sobelOperator.UseLegacyCoefficients(useLegacyCoefficients); + } + for (auto & sobelOperator : m_MovedSobelOperators) + { + sobelOperator.UseLegacyCoefficients(useLegacyCoefficients); + } + this->Modified(); + } +} + + template void GradientDifferenceImageToImageMetric::PrintSelf(std::ostream & os, Indent indent) const diff --git a/Modules/Registration/Common/test/CMakeLists.txt b/Modules/Registration/Common/test/CMakeLists.txt index 250eb7584a0..be62163c1c9 100644 --- a/Modules/Registration/Common/test/CMakeLists.txt +++ b/Modules/Registration/Common/test/CMakeLists.txt @@ -509,7 +509,11 @@ itk_add_test( 1 ) -set(ITKRegistrationGTests itkTransformInitializersGTest.cxx) +set( + ITKRegistrationGTests + itkGradientDifferenceImageToImageMetricGTest.cxx + itkTransformInitializersGTest.cxx +) creategoogletestdriver(ITKRegistration "${ITKRegistrationCommon-Test_LIBRARIES}" "${ITKRegistrationGTests}") if(BUILD_EXAMPLES) diff --git a/Modules/Registration/Common/test/itkGradientDifferenceImageToImageMetricGTest.cxx b/Modules/Registration/Common/test/itkGradientDifferenceImageToImageMetricGTest.cxx new file mode 100644 index 00000000000..4e6ba38b04c --- /dev/null +++ b/Modules/Registration/Common/test/itkGradientDifferenceImageToImageMetricGTest.cxx @@ -0,0 +1,37 @@ +/*========================================================================= + * + * 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 + * + * https://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. + * + *=========================================================================*/ + +// First include the header file to be tested: +#include "itkGradientDifferenceImageToImageMetric.h" +#include "itkImage.h" +#include + + +TEST(GradientDifferenceImageToImageMetric, UseLegacySobelOperatorCoordinates) +{ + using ImageType = itk::Image; + const auto metric = itk::GradientDifferenceImageToImageMetric::New(); + + EXPECT_TRUE(metric->IsUsingLegacySobelOperatorCoordinates()); + + for (const bool useLegacyCoefficients : { false, true }) + { + metric->UseLegacySobelOperatorCoordinates(useLegacyCoefficients); + EXPECT_EQ(metric->IsUsingLegacySobelOperatorCoordinates(), useLegacyCoefficients); + } +}