Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#5077 from blowekamp/comp…
Browse files Browse the repository at this point in the history
…ose_image_filter_perf

Performance improvements to ComposeImageFilter
  • Loading branch information
blowekamp authored Dec 16, 2024
2 parents 44e7133 + 25bd86a commit be0c8a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
23 changes: 2 additions & 21 deletions Modules/Filtering/ImageCompose/include/itkComposeImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "itkImageToImageFilter.h"
#include "itkVectorImage.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageScanlineIterator.h"
#include <vector>

namespace itk
Expand Down Expand Up @@ -108,27 +108,8 @@ class ITK_TEMPLATE_EXPORT ComposeImageFilter : public ImageToImageFilter<TInputI
private:
// we have to specialize the code for complex, because it provides no operator[]
// method
using InputIteratorType = ImageRegionConstIterator<InputImageType>;
using InputIteratorType = ImageScanlineConstIterator<InputImageType>;
using InputIteratorContainerType = std::vector<InputIteratorType>;

template <typename T>
void
ComputeOutputPixel(std::complex<T> & pix, InputIteratorContainerType & inputItContainer)
{
pix = std::complex<T>(inputItContainer[0].Get(), inputItContainer[1].Get());
++(inputItContainer[0]);
++(inputItContainer[1]);
}
template <typename TPixel>
void
ComputeOutputPixel(TPixel & pix, InputIteratorContainerType & inputItContainer)
{
for (unsigned int i = 0; i < this->GetNumberOfInputs(); ++i)
{
pix[i] = static_cast<typename NumericTraits<OutputPixelType>::ValueType>(inputItContainer[i].Get());
++(inputItContainer[i]);
}
}
};
} // namespace itk

Expand Down
46 changes: 33 additions & 13 deletions Modules/Filtering/ImageCompose/include/itkComposeImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ComposeImageFilter<TInputImage, TOutputImage>::BeforeThreadedGenerateData()

for (unsigned int i = 0; i < numberOfInputs; ++i)
{
auto * input = itkDynamicCastInDebugMode<InputImageType *>(this->ProcessObject::GetInput(i));
auto * input = this->GetInput(i);
if (!input)
{
itkExceptionMacro("Input " << i << " not set!");
Expand All @@ -108,33 +108,53 @@ template <typename TInputImage, typename TOutputImage>
void
ComposeImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(const RegionType & outputRegionForThread)
{
const typename OutputImageType::Pointer outputImage =
static_cast<OutputImageType *>(this->ProcessObject::GetOutput(0));

const typename OutputImageType::Pointer outputImage = this->GetOutput();

TotalProgressReporter progress(this, outputImage->GetRequestedRegion().GetNumberOfPixels());

ImageRegionIterator<OutputImageType> oit(outputImage, outputRegionForThread);

InputIteratorContainerType inputItContainer;
inputItContainer.reserve(this->GetNumberOfIndexedInputs());

for (unsigned int i = 0; i < this->GetNumberOfIndexedInputs(); ++i)
{
const InputImageType * inputImage = this->GetInput(i);

InputIteratorType iit(inputImage, outputRegionForThread);
iit.GoToBegin();
inputItContainer.push_back(iit);
inputItContainer.emplace_back(inputImage, outputRegionForThread);
}

OutputPixelType pix;
NumericTraits<OutputPixelType>::SetLength(pix, static_cast<unsigned int>(this->GetNumberOfIndexedInputs()));
while (!oit.IsAtEnd())
for (ImageScanlineIterator oit(outputImage, outputRegionForThread); !oit.IsAtEnd(); oit.NextLine())
{
ComputeOutputPixel(pix, inputItContainer);
oit.Set(pix);
++oit;
progress.CompletedPixel();
while (!oit.IsAtEndOfLine())
{
if constexpr (std::is_same<OutputPixelType,
std::complex<typename NumericTraits<OutputPixelType>::ValueType>>::value)
{
oit.Set({ inputItContainer[0].Get(), inputItContainer[1].Get() });
++(inputItContainer[0]);
++(inputItContainer[1]);
}
else
{
unsigned int i = 0;
for (auto & it : inputItContainer)
{
pix[i] = static_cast<typename NumericTraits<OutputPixelType>::ValueType>(it.Get());
++i;
++it;
}

oit.Set(pix);
}
++oit;
}
for (auto & it : inputItContainer)
{
it.NextLine();
}
progress.Completed(outputRegionForThread.GetSize()[0]);
}
}
} // end namespace itk
Expand Down

0 comments on commit be0c8a0

Please sign in to comment.