Skip to content

Commit

Permalink
ENH: Add "ReduceDimension" option to ImageSampler components
Browse files Browse the repository at this point in the history
Allows reducing the dimensionality of any image sampler by a new elastix parameter:

    (ReduceDimension "true")

Aims to implement a generalization to the ReducedFullSampler, pull request #52 proposed by Mathias Polfliet in 2018.
  • Loading branch information
N-Dekker committed Mar 22, 2024
1 parent 23aad88 commit e03c538
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Common/GTesting/itkImageFullSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,42 @@ GTEST_TEST(ImageFullSampler, ExactlyEqualVersusSlightlyDifferentMaskImageDomain)
EXPECT_FALSE(samplesOnExactlyEqualImageDomains.empty());

EXPECT_EQ(samplesOnExactlyEqualImageDomains, samplesOnSlightlyDifferentImageDomains);
}


GTEST_TEST(ImageFullSampler, ReduceDimension)
{
using PixelType = std::uint8_t;
static constexpr auto Dimension = 3U;
using ImageType = itk::Image<PixelType, Dimension>;
using ImageFullSamplerType = itk::ImageFullSampler<ImageType>;

std::mt19937 randomNumberEngine{};
const auto imageDomain = CreateRandomImageDomain<Dimension>(randomNumberEngine);
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<PixelType>(imageDomain);
elx::DefaultConstruct<ImageFullSamplerType> sampler{};

sampler.SetInput(image);
sampler.SetReduceDimension(true);
sampler.Update();

const auto & output = DerefRawPointer(sampler.GetOutput());

const auto reducedRegion = [imageDomain] {
auto size = imageDomain.size;
size.back() = 1;
return itk::ImageRegion<Dimension>{ imageDomain.index, size };
}();

const itk::ImageRegionRange imageRegionRange(*image, reducedRegion);
const std::size_t numberOfSamples{ output.size() };

ASSERT_EQ(numberOfSamples, imageRegionRange.size());

auto imageRegionIterator = imageRegionRange.cbegin();
for (std::size_t i{}; i < numberOfSamples; ++i)
{
EXPECT_EQ(output[i].m_ImageValue, *imageRegionIterator);
++imageRegionIterator;
}
}
8 changes: 8 additions & 0 deletions Common/ImageSamplers/itkImageSamplerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ class ITK_TEMPLATE_EXPORT ImageSamplerBase
/** Allows disabling the use of multi-threading, by `SetUseMultiThread(false)`. */
itkSetMacro(UseMultiThread, bool);

/** Tells whether the dimensionality is reduced. */
itkGetConstMacro(ReduceDimension, bool);

/** Allows reducing the dimensionality by 1. */
itkSetMacro(ReduceDimension, bool);

protected:
/** The constructor. */
ImageSamplerBase();
Expand Down Expand Up @@ -264,6 +270,8 @@ class ITK_TEMPLATE_EXPORT ImageSamplerBase

InputImageRegionType m_CroppedInputImageRegion{};
InputImageRegionType m_DummyInputImageRegion{};

bool m_ReduceDimension{ false };
};

} // end namespace itk
Expand Down
6 changes: 6 additions & 0 deletions Common/ImageSamplers/itkImageSamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ ImageSamplerBase<TInputImage>::CropInputImageRegion()
* InputImageRegion and the BoundingBoxRegion.
*/
m_CroppedInputImageRegion = m_InputImageRegion;

if (m_ReduceDimension)
{
m_CroppedInputImageRegion.GetModifiableSize().back() = 1;
}

if (!m_Mask.IsNull())
{
/** Get a handle to the input image. */
Expand Down
1 change: 1 addition & 0 deletions Core/ComponentBaseClasses/elxImageSamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ImageSamplerBase<TElastix>::BeforeRegistrationBase()
const Configuration & configuration = Deref(Superclass::GetConfiguration());
ITKBaseType & sampler = GetSelf();
sampler.SetUseMultiThread(configuration.RetrieveParameterValue(true, "UseMultiThreadingForSamplers", 0, false));
sampler.SetReduceDimension(configuration.RetrieveParameterValue(false, "ReduceDimension", 0, false));
}

/**
Expand Down

0 comments on commit e03c538

Please sign in to comment.