Skip to content

Use ImageRegionIterator without "WithIndex" in *.hxx files #5848

Merged
dzenanz merged 3 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:Use-ImageRegionIterators-without-Index
Mar 3, 2026
Merged

Use ImageRegionIterator without "WithIndex" in *.hxx files #5848
dzenanz merged 3 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:Use-ImageRegionIterators-without-Index

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

@N-Dekker N-Dekker commented Mar 1, 2026

Replaced ImageRegionIteratorWithIndex with ImageRegionIterator, and replaced
ImageRegionConstIteratorWithIndex with ImageRegionConstIterator, in cases
where the index of the iterator was not used.

When the index is not needed, ImageRegionIterator and ImageRegionConstIterator
are much faster than the corresponding iterators "with index"!

Aims to speed up the following filters:

  • BinomialBlurImageFilter
  • EigenAnalysis2DImageFilter
  • GradientRecursiveGaussianImageFilter
  • HessianRecursiveGaussianImageFilter
  • HoughTransform2DLinesImageFilter
  • VoronoiPartitioningImageFilter
  • VoronoiSegmentationImageFilter
  • VoronoiSegmentationImageFilterBase
  • VoronoiSegmentationRGBImageFilter

As well as:

  • GradientDifferenceImageToImageMetric
  • MetaImageConverter
  • PeakSignalToNoiseRatioCalculator
  • RobustAutomaticThresholdCalculator

@github-actions github-actions Bot added area:Python wrapping Python bindings for a class type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module area:Numerics Issues affecting the Numerics module labels Mar 1, 2026
@N-Dekker N-Dekker changed the title Use ImageRegionIterator without "WithIndex" in *.hxx files Use ImageRegionIterator without "WithIndex" in *.hxx files Mar 1, 2026
N-Dekker added 2 commits March 1, 2026 13:06
ImageToImageMetric uses ImageRegionConstIteratorWithIndex  internally.
Paved the way to remove `ImageRegionIteratorWithIndex` uses from various
`itk*.hxx` files.
@N-Dekker N-Dekker force-pushed the Use-ImageRegionIterators-without-Index branch from ef2e670 to 4df6928 Compare March 1, 2026 12:10
Replaced `ImageRegionIteratorWithIndex` with `ImageRegionIterator`, and replaced
`ImageRegionConstIteratorWithIndex` with `ImageRegionConstIterator`, in cases
where the index of the iterator was not used.

When the index is not needed, `ImageRegionIterator` and `ImageRegionConstIterator`
are _much_ faster than the corresponding iterators "with index"!

Aims to speed up the following filters:
- BinomialBlurImageFilter
- EigenAnalysis2DImageFilter
- GradientRecursiveGaussianImageFilter
- HessianRecursiveGaussianImageFilter
- HoughTransform2DLinesImageFilter
- VoronoiPartitioningImageFilter
- VoronoiSegmentationImageFilter
- VoronoiSegmentationImageFilterBase
- VoronoiSegmentationRGBImageFilter

As well as:
- GradientDifferenceImageToImageMetric
- MetaImageConverter
- PeakSignalToNoiseRatioCalculator
- RobustAutomaticThresholdCalculator

Found by regular expression ` ImageRegionIteratorWithIndex(?!.*\.GetIndex\(\))`
and ` ImageRegionConstIteratorWithIndex(?!.*\.GetIndex\(\))`.

Excluded `ImageRegionIteratorWithIndex<OutputImageAdaptorType>`, to avoid test
failures.
@N-Dekker N-Dekker force-pushed the Use-ImageRegionIterators-without-Index branch from 4df6928 to 3180a2f Compare March 2, 2026 22:12
@N-Dekker
Copy link
Copy Markdown
Contributor Author

N-Dekker commented Mar 3, 2026

FYI I just observed that unnecessarily using an iterator "WithIndex" (when the index in not retrieved) may cause a performance penalty of 50%. Specifically, on VS2026 (Release build) the duration of an iteration on a large (100'000 x 10'000) image region went down from ~0.6 sec. to ~0.4 sec when I switched from ImageRegionConstIteratorWithIndex to ImageRegionConstIterator for the following code:

for (IteratorType it(image, region); !it.IsAtEnd(); ++it)
{
  result += it.Get();  // Retrieves the pixel value, not the index!
}

@N-Dekker N-Dekker marked this pull request as ready for review March 3, 2026 10:09
Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a glance.

@dzenanz dzenanz merged commit 80cff0d into InsightSoftwareConsortium:main Mar 3, 2026
18 checks passed
ImageRegionIteratorWithIndex<RealImageType> it(derivativeImage, derivativeImage->GetRequestedRegion());
ImageRegionIterator<RealImageType> it(derivativeImage, derivativeImage->GetRequestedRegion());

ImageRegionIteratorWithIndex<OutputImageAdaptorType> ot(m_ImageAdaptor, m_ImageAdaptor->GetRequestedRegion());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that replacing ImageRegionIteratorWithIndex<OutputImageAdaptorType> with ImageRegionIterator<OutputImageAdaptorType> here in itkGradientRecursiveGaussianImageFilter.hxx
didn't work, even though I wanted to! It caused test failures at Windows_NT-Build14707-PR5848-Use-ImageRegionIterators-without-Index, of the following tests:

itkSimpleImageRegistrationTest4
itkMeanSquaresImageMetricTest
itkImageRegistrationMethodTest_9
itkImageRegistrationMethodTest_8
itkImageRegistrationMethodTest_7
itkImageRegistrationMethodTest_6
itkImageRegistrationMethodTest_5
itkImageRegistrationMethodTest_4
itkImageRegistrationMethodTest_3
itkImageRegistrationMethodTest_2
itkImageRegistrationMethodTest_1
itkQuasiNewtonOptimizerv4Test
itkAutoScaledGradientDescentRegistrationOnVectorTest
itkAutoScaledGradientDescentRegistrationTest3
itkAutoScaledGradientDescentRegistrationTest2
itkAutoScaledGradientDescentRegistrationTest
itkObjectToObjectMultiMetricv4RegistrationTest
itkDemonsImageToImageMetricv4RegistrationTest3
itkImageToImageMetricv4RegistrationTest3
itkMetricImageGradientTest
itkMeanSquaresImageToImageMetricv4OnVectorTest2
itkCurvesLevelSetImageFilterTest
itkNarrowBandCurvesLevelSetImageFilterTest1
itkGeodesicActiveContourLevelSetImageFilterTest
itkGradientRecursiveGaussianFilterTest4
itkGradientRecursiveGaussianFilterTest3
itkGradientRecursiveGaussianFilterTest
itkHessianRecursiveGaussianFilterScaleSpaceTest
itkHessianToObjectnessMeasureImageFilterTest2
itkHessianToObjectnessMeasureImageFilterTest
itkDeformableSimplexMesh3DGradientConstaintForceFilterTest

I still wonder why! I would expect ImageRegionIterator to behave exactly like ImageRegionIteratorWithIndex in this case, only faster!

N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 8, 2026
Following pull request InsightSoftwareConsortium#5848
commit 3180a2f
"PERF: Use ImageRegionIterator without "WithIndex" in `*.hxx` files"

Note that this improvement is only possible because of the following fix:
pull request InsightSoftwareConsortium#5872
commit 1e3a883
"BUG: Add ImageAdaptor::ComputeOffset, to fix ImageRegionIterator support"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 10, 2026
Following pull request InsightSoftwareConsortium#5848
commit 3180a2f
"PERF: Use ImageRegionIterator without "WithIndex" in `*.hxx` files"

Note that this improvement is only possible because of the following fix:
pull request InsightSoftwareConsortium#5872
commit 1e3a883
"BUG: Add ImageAdaptor::ComputeOffset, to fix ImageRegionIterator support"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Numerics Issues affecting the Numerics module area:Python wrapping Python bindings for a class area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants