Skip to content

Commit c217151

Browse files
authored
Merge pull request #5805 from N-Dekker/Avoid-multiple-ComputeIndex-calls
Avoid calling `ImageConstIterator::ComputeIndex()` multiple times, in filters and segmenter
2 parents 7d89dbc + cea19d9 commit c217151

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

Modules/Filtering/DisplacementField/include/itkTransformToDisplacementFieldFilter.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@ TransformToDisplacementFieldFilter<TOutputImage, TParametersValueType>::LinearTh
230230
// Loop over the vector image, walk the output region for this thread.
231231
for (ImageScanlineIterator outIt(outputPtr, outputRegionForThread); !outIt.IsAtEnd(); outIt.NextLine())
232232
{
233+
const auto computedIndex = outIt.ComputeIndex();
233234

234235
// Compare with the ResampleImageFilter
235236
// The region may be split along the fast scan-line direction, so
236237
// the computation is done for the beginning and end of the largest
237238
// possible region to improve consistent numerics.
238-
IndexType index = outIt.ComputeIndex();
239+
IndexType index = computedIndex;
239240
index[0] = largestPossibleRegion.GetIndex(0);
240241

241242
outputPtr->TransformIndexToPhysicalPoint(index, outputPoint);
@@ -247,7 +248,7 @@ TransformToDisplacementFieldFilter<TOutputImage, TParametersValueType>::LinearTh
247248
inputPoint = transformPtr->TransformPoint(outputPoint);
248249
const typename PointType::VectorType endDisplacement = inputPoint - outputPoint;
249250

250-
IndexValueType scanlineIndex = outIt.ComputeIndex()[0];
251+
IndexValueType scanlineIndex = computedIndex[0];
251252

252253
while (!outIt.IsAtEndOfLine())
253254
{

Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,11 @@ CannyEdgeDetectionImageFilter<TInputImage, TOutputImage>::HysteresisThresholding
293293

294294
if (value > m_UpperThreshold)
295295
{
296+
const auto computedIndex = oit.ComputeIndex();
296297
node = m_NodeStore->Borrow();
297-
node->m_Value = oit.ComputeIndex();
298+
node->m_Value = computedIndex;
298299
m_NodeList->PushFront(node);
299-
FollowEdge(oit.ComputeIndex(), multiplyImageFilterOutput);
300+
FollowEdge(computedIndex, multiplyImageFilterOutput);
300301
}
301302

302303
++oit;

Modules/Filtering/ImageFeature/include/itkHoughTransform2DLinesImageFilter.hxx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ HoughTransform2DLinesImageFilter<TInputPixelType, TOutputPixelType>::GetLines()
282282
// Create the line.
283283
LineType::LinePointListType list; // Insert two points per line.
284284

285-
const double radius = it_input.ComputeIndex()[0];
286-
const double teta = ((it_input.ComputeIndex()[1]) * 2 * Math::pi / this->GetAngleResolution()) - Math::pi;
285+
const auto computedIndex = it_input.ComputeIndex();
286+
const double radius = computedIndex[0];
287+
const double teta = (computedIndex[1] * 2 * Math::pi / this->GetAngleResolution()) - Math::pi;
287288
const double Vx = radius * std::cos(teta);
288289
const double Vy = radius * std::sin(teta);
289290
const double norm = std::sqrt(Vx * Vx + Vy * Vy);
@@ -326,8 +327,8 @@ HoughTransform2DLinesImageFilter<TInputPixelType, TOutputPixelType>::GetLines()
326327
{
327328
for (double length = 0; length < m_DiscRadius; length += 1)
328329
{
329-
index[0] = static_cast<IndexValueType>(it_input.ComputeIndex()[0] + length * std::cos(angle));
330-
index[1] = static_cast<IndexValueType>(it_input.ComputeIndex()[1] + length * std::sin(angle));
330+
index[0] = static_cast<IndexValueType>(computedIndex[0] + length * std::cos(angle));
331+
index[1] = static_cast<IndexValueType>(computedIndex[1] + length * std::sin(angle));
331332
if (postProcessImage->GetBufferedRegion().IsInside(index))
332333
{
333334
postProcessImage->SetPixel(index, 0);

Modules/Filtering/ImageGrid/include/itkResampleImageFilter.hxx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,16 @@ ResampleImageFilter<TInputImage, TOutputImage, TInterpolatorPrecisionType, TTran
466466
// Determine the continuous index of the first and end pixel of output
467467
// scan line when mapped to the input coordinate frame.
468468

469-
IndexType index = outIt.ComputeIndex();
469+
const auto computedIndex = outIt.ComputeIndex();
470+
471+
IndexType index = computedIndex;
470472
index[0] = firstIndexValueOfLargestPossibleRegion;
471473

472474
const ContinuousInputIndexType startIndex = transformIndex(index);
473475
index[0] += firstSizeValueOfLargestPossibleRegion;
474476
const auto vectorFromStartIndex = transformIndex(index) - startIndex;
475477

476-
IndexValueType scanlineIndex = outIt.ComputeIndex()[0];
478+
IndexValueType scanlineIndex = computedIndex[0];
477479

478480

479481
while (!outIt.IsAtEndOfLine())

Modules/Filtering/ImageLabel/include/itkBinaryContourImageFilter.hxx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ BinaryContourImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData
138138
{
139139
const InputImagePixelType PVal = inLineIt.Get();
140140

141+
// We've hit the start of a run
142+
SizeValueType length = 0;
143+
const IndexType thisIndex = inLineIt.ComputeIndex();
144+
141145
if (Math::AlmostEquals(PVal, m_ForegroundValue))
142146
{
143-
// We've hit the start of a run
144-
SizeValueType length = 0;
145-
const IndexType thisIndex = inLineIt.ComputeIndex();
146-
147147
outLineIt.Set(m_BackgroundValue);
148148

149149
++length;
@@ -162,10 +162,6 @@ BinaryContourImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData
162162
}
163163
else
164164
{
165-
// We've hit the start of a run
166-
SizeValueType length = 0;
167-
const IndexType thisIndex = inLineIt.ComputeIndex();
168-
169165
outLineIt.Set(PVal);
170166
++length;
171167
++inLineIt;

Modules/Filtering/MathematicalMorphology/include/itkValuedRegionalExtremaImageFilter.hxx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ ValuedRegionalExtremaImageFilter<TInputImage, TOutputImage, TFunction1, TFunctio
130130
// already visited this pixel and don't need to do so again
131131
if (compareOut(V, m_MarkerValue))
132132
{
133+
const auto computedIndex = outIt.ComputeIndex();
134+
133135
// reposition the input iterator
134-
inNIt += outIt.ComputeIndex() - inNIt.GetIndex();
136+
inNIt += computedIndex - inNIt.GetIndex();
135137

136138
auto Cent = static_cast<InputImagePixelType>(V);
137139

@@ -156,7 +158,7 @@ ValuedRegionalExtremaImageFilter<TInputImage, TOutputImage, TFunction1, TFunctio
156158
// indexes of all neighbors with value V on the stack. The
157159
// process terminates when the stack is empty.
158160

159-
outNIt += outIt.ComputeIndex() - outNIt.GetIndex();
161+
outNIt += computedIndex - outNIt.GetIndex();
160162

161163
OutputImagePixelType NVal;
162164
OutIndexType idx;

Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,9 @@ Segmenter<TInputImage>::GradientDescent(InputImageTypePointer img, ImageRegionTy
820820
{
821821
if (it.Get() == NULL_LABEL)
822822
{
823-
valueIt.SetLocation(it.ComputeIndex());
824-
labelIt.SetLocation(it.ComputeIndex());
823+
const auto computeIndex = it.ComputeIndex();
824+
valueIt.SetLocation(computeIndex);
825+
labelIt.SetLocation(computeIndex);
825826
IdentifierType newLabel = NULL_LABEL; // Follow the path of steep-
826827
while (newLabel == NULL_LABEL) // est descent until a label
827828
{

0 commit comments

Comments
 (0)