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

Pull request 52 (ReducedFullSampler) updated #1064

Draft
wants to merge 4 commits 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
123 changes: 123 additions & 0 deletions Common/ImageSamplers/itkImageReducedFullSampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#ifndef __ImageReducedFullSampler_h
#define __ImageReducedFullSampler_h

#include "itkImageSamplerBase.h"

namespace itk
{
/** \class ImageReducedFullSampler
*
* \brief Samples all voxels in the InputImageRegion for groupwise registration.
*
* This ImageSampler samples all voxels in the InputImageRegion.
* If a mask is given: only those voxels within the mask AND the
* InputImageRegion.
*
* \ingroup ImageSamplers
*/

template <class TInputImage>
class ImageReducedFullSampler : public ImageSamplerBase<TInputImage>
{
public:
/** Standard ITK-stuff. */
typedef ImageReducedFullSampler Self;
typedef ImageSamplerBase<TInputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;

/** Method for creation through the object factory. */
itkNewMacro(Self);

/** Run-time type information (and related methods). */
itkTypeMacro(ImageReducedFullSampler, ImageSamplerBase);

/** Typedefs inherited from the superclass. */
typedef typename Superclass::DataObjectPointer DataObjectPointer;
typedef typename Superclass::OutputVectorContainerType OutputVectorContainerType;
typedef typename Superclass::OutputVectorContainerPointer OutputVectorContainerPointer;
typedef typename Superclass::InputImageType InputImageType;
typedef typename Superclass::InputImagePointer InputImagePointer;
typedef typename Superclass::InputImageConstPointer InputImageConstPointer;
typedef typename Superclass::InputImageRegionType InputImageRegionType;
typedef typename Superclass::InputImagePixelType InputImagePixelType;
typedef typename Superclass::ImageSampleType ImageSampleType;
typedef typename Superclass::ImageSampleContainerType ImageSampleContainerType;
typedef typename Superclass::ImageSampleContainerPointer ImageSampleContainerPointer;
typedef typename Superclass::MaskType MaskType;

/** The input image dimension. */
itkStaticConstMacro(InputImageDimension, unsigned int, Superclass::InputImageDimension);

itkStaticConstMacro(ReducedInputImageDimension, unsigned int, Superclass::InputImageDimension - 1);

/** Other typdefs. */
typedef typename InputImageType::IndexType InputImageIndexType;
typedef typename InputImageType::SizeType InputImageSizeType;
typedef typename InputImageType::PointType InputImagePointType;

/** Selecting new samples makes no sense if nothing changed.
* The same samples would be selected anyway.
*/
bool
SelectNewSamplesOnUpdate(void) override
{
return false;
}


/** Returns whether the sampler supports SelectNewSamplesOnUpdate(). */
bool
SelectingNewSamplesOnUpdateSupported(void) const override
{
return false;
}


protected:
/** The constructor. */
ImageReducedFullSampler() {}
/** The destructor. */
~ImageReducedFullSampler() override {}

/** PrintSelf. */
void
PrintSelf(std::ostream & os, Indent indent) const override;

/** Function that does the work. */
void
GenerateData(void) override;

private:
/** The private constructor. */
ImageReducedFullSampler(const Self &); // purposely not implemented
/** The private copy constructor. */
void
operator=(const Self &); // purposely not implemented
};

} // end namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
# include "itkImageReducedFullSampler.hxx"
#endif

#endif // end #ifndef __ImageReducedFullSampler_h
145 changes: 145 additions & 0 deletions Common/ImageSamplers/itkImageReducedFullSampler.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#ifndef __ImageReducedFullSampler_txx
#define __ImageReducedFullSampler_txx

#include "itkImageReducedFullSampler.h"

#include "itkImageRegionConstIteratorWithIndex.h"

namespace itk
{

/**
* ******************* GenerateData *******************
*/

template <class TInputImage>
void
ImageReducedFullSampler<TInputImage>::GenerateData(void)
{

/** Get handles to the input image, output sample container, and the mask. */
InputImageConstPointer inputImage = this->GetInput();
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
typename MaskType::ConstPointer mask = this->GetMask();

/** Clear the container. */
sampleContainer->Initialize();

/** Set up a region iterator within the user specified image region. */
typedef ImageRegionConstIteratorWithIndex<InputImageType> InputImageIterator;
InputImageIndexType index = this->GetCroppedInputImageRegion().GetIndex();
index[ReducedInputImageDimension] = 0;
InputImageSizeType size = this->GetCroppedInputImageRegion().GetSize();
size[ReducedInputImageDimension] = 1;
InputImageRegionType region;
region.SetIndex(index);
region.SetSize(size);
InputImageIterator iter(inputImage, region);
Comment on lines +48 to +55
Copy link
Member Author

@N-Dekker N-Dekker Mar 3, 2024

Choose a reason for hiding this comment

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

This appears to be the essential part, compared to the "non-reduced" FullSampler. Looks like seven lines of code could be simplified to the following three:

    InputImageRegionType region = this->GetCroppedInputImageRegion();
    region.SetIndex(ReducedInputImageDimension, 0);
    region.SetSize(ReducedInputImageDimension, 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.

@MathiasPolfliet @mstaring @stefanklein Is it really necessary for the "reduced" sampler to do index[ReducedInputImageDimension] = 0? Or would it be sufficient to only adjust the size, and leave the index alone?

I'm asking, because ITK allows specifying start index of the buffered region of an image to be non-zero. So an index value of zero may be outside the buffered region. Trying to retrieve samples outside the buffered region might cause crashes. So if it is sufficient for the "reduced" sampler to only adjust the size, I would prefer to leave the index alone.

Another option might be something like:

// Instead of index[ReducedInputImageDimension] = 0
index[ReducedInputImageDimension] = bufferedRegion.GetIndex(ReducedInputImageDimension);

But I wonder if that's useful/necessary.


/** Fill the sample container. */
if (mask.IsNull())
{
/** Try to reserve memory. If no mask is used this can raise std
* exceptions when the input image is large.
*/
try
{
sampleContainer->Reserve(region.GetNumberOfPixels());
}
catch (std::exception & excp)
{
std::string message = "std: ";
message += excp.what();
message += "\nERROR: failed to allocate memory for the sample container.";
const char * message2 = message.c_str();
itkExceptionMacro(<< message2);
}
catch (...)
{
itkExceptionMacro(<< "ERROR: failed to allocate memory for the sample container.");
}

/** Simply loop over the image and store all samples in the container. */
ImageSampleType tempSample;
unsigned long ind = 0;
for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter, ++ind)
{
/** Get sampled index */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

/** Get sampled image value */
tempSample.m_ImageValue = iter.Get();

/** Store in container */
sampleContainer->SetElement(ind, tempSample);

} // end for
} // end if no mask
else
{
if (mask->GetSource())
{
mask->GetSource()->Update();
}

/** Loop over the image and check if the points falls within the mask. */
ImageSampleType tempSample;
for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter)
{
/** Get sampled index. */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point. */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

if (mask->IsInsideInWorldSpace(tempSample.m_ImageCoordinates))
{
/** Get sampled image value. */
tempSample.m_ImageValue = iter.Get();

/** Store in container. */
sampleContainer->push_back(tempSample);

} // end if
} // end for
} // end else (if mask exists)

} // end GenerateData()


/**
* ******************* PrintSelf *******************
*/

template <class TInputImage>
void
ImageReducedFullSampler<TInputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
} // end PrintSelf()


} // end namespace itk

#endif // end #ifndef __ReducedImageFullSampler_txx
5 changes: 5 additions & 0 deletions Components/ImageSamplers/ReducedFull/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ADD_ELXCOMPONENT( ReducedFullSampler
elxReducedFullSampler.h
elxReducedFullSampler.hxx
elxReducedFullSampler.cxx
)
17 changes: 17 additions & 0 deletions Components/ImageSamplers/ReducedFull/elxReducedFullSampler.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*======================================================================

This file is part of the elastix software.

Copyright (c) University Medical Center Utrecht. All rights reserved.
See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.

======================================================================*/

#include "elxReducedFullSampler.h"

elxInstallMacro(ReducedFullSampler);
Loading
Loading