Skip to content

Commit

Permalink
PERF: Remove Image::GetPixel calls from ImageGridSampler inner loop
Browse files Browse the repository at this point in the history
itk::Image::GetPixel(const IndexType &) is rather expensive, as it has to compute the offset relative to the start of the pixel buffer.

When having a 4096x4096 grid (no mask), it was observed that the duration of an ImageGridSampler::Update() went from slightly more than 0.20 second (before this commit) to slightly _less_ than 0.20 second  (after this commit), using Visual C++ 2019, Release configuration.
  • Loading branch information
N-Dekker committed Dec 7, 2023
1 parent e375051 commit 8b2e5a4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Common/ImageSamplers/itkImageGridSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ ImageGridSampler<TInputImage>::GenerateData()
{
for (unsigned int y = 0; y < sampleGridSize[1]; ++y)
{
const InputImagePixelType * const pixel = &(inputImage.GetPixel(index));

for (unsigned int x = 0; x < sampleGridSize[0]; ++x)
{
ImageSampleType tempSample;

// Get sampled fixed image value.
tempSample.m_ImageValue = inputImage.GetPixel(index);
tempSample.m_ImageValue = pixel[x];

// Translate index to point.
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
Expand Down Expand Up @@ -139,6 +141,8 @@ ImageGridSampler<TInputImage>::GenerateData()
{
for (unsigned int y = 0; y < sampleGridSize[1]; ++y)
{
const InputImagePixelType * const pixel = &(inputImage.GetPixel(index));

for (unsigned int x = 0; x < sampleGridSize[0]; ++x)
{
ImageSampleType tempSample;
Expand All @@ -149,7 +153,7 @@ ImageGridSampler<TInputImage>::GenerateData()
if (mask->IsInsideInWorldSpace(tempSample.m_ImageCoordinates))
{
// Get sampled fixed image value.
tempSample.m_ImageValue = inputImage.GetPixel(index);
tempSample.m_ImageValue = pixel[x];

// Store sample in container.
sampleVector.push_back(tempSample);
Expand Down

0 comments on commit 8b2e5a4

Please sign in to comment.