Use ImageRegionIterator without "WithIndex" in *.hxx files #5848
Conversation
*.hxx files
ImageToImageMetric uses ImageRegionConstIteratorWithIndex internally.
Paved the way to remove `ImageRegionIteratorWithIndex` uses from various `itk*.hxx` files.
ef2e670 to
4df6928
Compare
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.
4df6928 to
3180a2f
Compare
|
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!
} |
| ImageRegionIteratorWithIndex<RealImageType> it(derivativeImage, derivativeImage->GetRequestedRegion()); | ||
| ImageRegionIterator<RealImageType> it(derivativeImage, derivativeImage->GetRequestedRegion()); | ||
|
|
||
| ImageRegionIteratorWithIndex<OutputImageAdaptorType> ot(m_ImageAdaptor, m_ImageAdaptor->GetRequestedRegion()); |
There was a problem hiding this comment.
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!
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"
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"
Replaced
ImageRegionIteratorWithIndexwithImageRegionIterator, and replacedImageRegionConstIteratorWithIndexwithImageRegionConstIterator, in caseswhere the index of the iterator was not used.
When the index is not needed,
ImageRegionIteratorandImageRegionConstIteratorare much faster than the corresponding iterators "with index"!
Aims to speed up the following filters:
As well as: