Skip to content

Commit 180b941

Browse files
committed
ENH: Add "ReduceDimension" option to ImageSampler components
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.
1 parent 094b101 commit 180b941

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Common/GTesting/itkImageFullSamplerGTest.cxx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,42 @@ GTEST_TEST(ImageFullSampler, ExactlyEqualVersusSlightlyDifferentMaskImageDomain)
185185
EXPECT_FALSE(samplesOnExactlyEqualImageDomains.empty());
186186

187187
EXPECT_EQ(samplesOnExactlyEqualImageDomains, samplesOnSlightlyDifferentImageDomains);
188+
}
189+
190+
191+
GTEST_TEST(ImageFullSampler, ReduceDimension)
192+
{
193+
using PixelType = std::uint8_t;
194+
static constexpr auto Dimension = 3U;
195+
using ImageType = itk::Image<PixelType, Dimension>;
196+
using ImageFullSamplerType = itk::ImageFullSampler<ImageType>;
197+
198+
std::mt19937 randomNumberEngine{};
199+
const auto imageDomain = CreateRandomImageDomain<Dimension>(randomNumberEngine);
200+
const auto image = CreateImageFilledWithSequenceOfNaturalNumbers<PixelType>(imageDomain);
201+
elx::DefaultConstruct<ImageFullSamplerType> sampler{};
202+
203+
sampler.SetInput(image);
204+
sampler.SetReduceDimension(true);
205+
sampler.Update();
206+
207+
const auto & output = DerefRawPointer(sampler.GetOutput());
208+
209+
const auto reducedRegion = [imageDomain] {
210+
auto size = imageDomain.size;
211+
size.back() = 1;
212+
return itk::ImageRegion{ imageDomain.index, size };
213+
}();
214+
215+
const itk::ImageRegionRange imageRegionRange(*image, reducedRegion);
216+
const std::size_t numberOfSamples{ output.size() };
217+
218+
ASSERT_EQ(numberOfSamples, imageRegionRange.size());
219+
220+
auto imageRegionIterator = imageRegionRange.cbegin();
221+
for (std::size_t i{}; i < numberOfSamples; ++i)
222+
{
223+
EXPECT_EQ(output[i].m_ImageValue, *imageRegionIterator);
224+
++imageRegionIterator;
225+
}
188226
}

Common/ImageSamplers/itkImageSamplerBase.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ class ITK_TEMPLATE_EXPORT ImageSamplerBase
209209
/** Allows disabling the use of multi-threading, by `SetUseMultiThread(false)`. */
210210
itkSetMacro(UseMultiThread, bool);
211211

212+
/** Tells whether the dimensionality is reduced. */
213+
itkGetConstMacro(ReduceDimension, bool);
214+
215+
/** Allows reducing the dimensionality by 1. */
216+
itkSetMacro(ReduceDimension, bool);
217+
212218
protected:
213219
/** The constructor. */
214220
ImageSamplerBase();
@@ -264,6 +270,8 @@ class ITK_TEMPLATE_EXPORT ImageSamplerBase
264270

265271
InputImageRegionType m_CroppedInputImageRegion{};
266272
InputImageRegionType m_DummyInputImageRegion{};
273+
274+
bool m_ReduceDimension{ false };
267275
};
268276

269277
} // end namespace itk

Common/ImageSamplers/itkImageSamplerBase.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ ImageSamplerBase<TInputImage>::CropInputImageRegion()
301301
* InputImageRegion and the BoundingBoxRegion.
302302
*/
303303
m_CroppedInputImageRegion = m_InputImageRegion;
304+
305+
if (m_ReduceDimension)
306+
{
307+
m_CroppedInputImageRegion.GetModifiableSize().back() = 1;
308+
}
309+
304310
if (!m_Mask.IsNull())
305311
{
306312
/** Get a handle to the input image. */

Core/ComponentBaseClasses/elxImageSamplerBase.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ImageSamplerBase<TElastix>::BeforeRegistrationBase()
3636
const Configuration & configuration = Deref(Superclass::GetConfiguration());
3737
ITKBaseType & sampler = GetSelf();
3838
sampler.SetUseMultiThread(configuration.RetrieveParameterValue(true, "UseMultiThreadingForSamplers", 0, false));
39+
sampler.SetReduceDimension(configuration.RetrieveParameterValue(false, "ReduceDimension", 0, false));
3940
}
4041

4142
/**

0 commit comments

Comments
 (0)