diff --git a/Modules/Core/Common/include/itkSobelOperator.h b/Modules/Core/Common/include/itkSobelOperator.h index 36811a4b9dc..1fd4f47ad31 100644 --- a/Modules/Core/Common/include/itkSobelOperator.h +++ b/Modules/Core/Common/include/itkSobelOperator.h @@ -99,6 +99,9 @@ class ITK_TEMPLATE_EXPORT SobelOperator : public NeighborhoodOperator; + /** Additional type alias. */ + using typename Superclass::SizeType; + itkOverrideGetNameOfClassMacro(SobelOperator); static_assert( diff --git a/Modules/Core/Common/include/itkSobelOperator.hxx b/Modules/Core/Common/include/itkSobelOperator.hxx index ba3a6e14573..a3acf0384d7 100644 --- a/Modules/Core/Common/include/itkSobelOperator.hxx +++ b/Modules/Core/Common/include/itkSobelOperator.hxx @@ -18,6 +18,7 @@ #ifndef itkSobelOperator_hxx #define itkSobelOperator_hxx +#include "itkIndexRange.h" #include "itkObject.h" namespace itk @@ -32,41 +33,26 @@ SobelOperator::Fill(const CoefficientVector & co // coefficients in the exact center of the neighborhood const unsigned int center = this->GetCenterNeighborhoodIndex(); - if constexpr (VDimension == 2) - { - unsigned int coeff_index = 0; - for (int y = -1; y <= 1; ++y) - { - for (int x = -1; x <= 1; ++x) - { - const int pos = center + y * this->GetStride(1) + x * this->GetStride(0); - // Note, The following line copies the double precision - // coefficients of SobelOperator to the pixel type - // of the neighborhood operator which may not support - // negative numbers, or floating point numbers. - this->operator[](pos) = static_cast(coeff[coeff_index]); + using IndexType = Index; - ++coeff_index; - } - } - } - if constexpr (VDimension == 3) + unsigned int coeff_index = 0; + for (const IndexType & index : + ImageRegionIndexRange(ImageRegion{ IndexType::Filled(-1), SizeType::Filled(3) })) { - unsigned int coeff_index = 0; - for (int z = -1; z <= 1; ++z) + auto pos = static_cast(center); + + for (unsigned int i{}; i < VDimension; ++i) { - for (int y = -1; y <= 1; ++y) - { - for (int x = -1; x <= 1; ++x) - { - const int pos = center + z * this->GetStride(2) + y * this->GetStride(1) + x * this->GetStride(0); + pos += static_cast(index[i] * this->GetStride(i)); + } - this->operator[](pos) = static_cast(coeff[coeff_index]); + // Note, The following line copies the double precision + // coefficients of SobelOperator to the pixel type + // of the neighborhood operator which may not support + // negative numbers, or floating point numbers. + this->operator[](pos) = static_cast(coeff[coeff_index]); - ++coeff_index; - } - } - } + ++coeff_index; } }