Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add "ReduceDimension" option to ImageSampler components #1083

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, UseForGroupwiseRegistration)
{
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.UseForGroupwiseRegistration();
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;
}
}
9 changes: 9 additions & 0 deletions Common/ImageSamplers/itkImageSamplerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ class ITK_TEMPLATE_EXPORT ImageSamplerBase
/** Allows disabling the use of multi-threading, by `SetUseMultiThread(false)`. */
itkSetMacro(UseMultiThread, bool);

/** Tells that the sampler is used for groupwise registration. */
void
UseForGroupwiseRegistration()
{
m_UseForGroupwiseRegistration = true;
}

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

InputImageRegionType m_CroppedInputImageRegion{};
InputImageRegionType m_DummyInputImageRegion{};

bool m_UseForGroupwiseRegistration{ 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_UseForGroupwiseRegistration)
{
m_CroppedInputImageRegion.GetModifiableSize().back() = 1;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstaring @stefanklein It does not seem to be necessary to also adjust the index (by doing index[N-1] = 0, maybe), but that depends on the use case, I guess... See #1064 (comment)

}

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

/**
Expand Down
Loading