diff --git a/Common/ImageSamplers/itkImageReducedFullSampler.h b/Common/ImageSamplers/itkImageReducedFullSampler.h new file mode 100644 index 000000000..f532addab --- /dev/null +++ b/Common/ImageSamplers/itkImageReducedFullSampler.h @@ -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 ImageReducedFullSampler : public ImageSamplerBase +{ +public: + /** Standard ITK-stuff. */ + typedef ImageReducedFullSampler Self; + typedef ImageSamplerBase Superclass; + typedef SmartPointer Pointer; + typedef SmartPointer 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 diff --git a/Common/ImageSamplers/itkImageReducedFullSampler.hxx b/Common/ImageSamplers/itkImageReducedFullSampler.hxx new file mode 100644 index 000000000..e0ab73132 --- /dev/null +++ b/Common/ImageSamplers/itkImageReducedFullSampler.hxx @@ -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 +void +ImageReducedFullSampler::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 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); + + /** 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 +void +ImageReducedFullSampler::PrintSelf(std::ostream & os, Indent indent) const +{ + Superclass::PrintSelf(os, indent); +} // end PrintSelf() + + +} // end namespace itk + +#endif // end #ifndef __ReducedImageFullSampler_txx diff --git a/Components/ImageSamplers/ReducedFull/CMakeLists.txt b/Components/ImageSamplers/ReducedFull/CMakeLists.txt new file mode 100644 index 000000000..7b0d3857c --- /dev/null +++ b/Components/ImageSamplers/ReducedFull/CMakeLists.txt @@ -0,0 +1,5 @@ +ADD_ELXCOMPONENT( ReducedFullSampler + elxReducedFullSampler.h + elxReducedFullSampler.hxx + elxReducedFullSampler.cxx +) diff --git a/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.cxx b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.cxx new file mode 100644 index 000000000..78af02fc6 --- /dev/null +++ b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.cxx @@ -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); diff --git a/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.h b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.h new file mode 100644 index 000000000..5a7ae4b0f --- /dev/null +++ b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.h @@ -0,0 +1,112 @@ +/*====================================================================== + + 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. + +======================================================================*/ +#ifndef __elxReducedFullSampler_h +#define __elxReducedFullSampler_h + +#include "elxIncludes.h" // include first to avoid MSVS warning +#include "itkImageReducedFullSampler.h" + +namespace elastix +{ + +/** + * \class ReducedFullSampler + * \brief An interpolator based on the itk::ImageReducedFullSampler. + * + * This image sampler samples all voxels in + * the InputImageRegion for a groupwise registration + * + * This sampler does not react to the NewSamplesEveryIteration parameter. + * + * The parameters used in this class are: + * \parameter ImageSampler: Select this image sampler as follows:\n + * (ImageSampler "ReducedFull") + * + * \ingroup ImageSamplers + */ + +template +class ReducedFullSampler + : public itk::ImageReducedFullSampler::InputImageType> + , public elx::ImageSamplerBase +{ +public: + /** Standard ITK-stuff. */ + typedef ReducedFullSampler Self; + typedef itk::ImageReducedFullSampler::InputImageType> Superclass1; + typedef elx::ImageSamplerBase Superclass2; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(ReducedFullSampler, itk::ImageFullSampler); + + /** Name of this class. + * Use this name in the parameter file to select this specific interpolator. \n + * example: (ImageSampler "Full")\n + */ + elxClassNameMacro("ReducedFull"); + + /** Typedefs inherited from the superclass. */ + typedef typename Superclass1::DataObjectPointer DataObjectPointer; + typedef typename Superclass1::OutputVectorContainerType OutputVectorContainerType; + typedef typename Superclass1::OutputVectorContainerPointer OutputVectorContainerPointer; + typedef typename Superclass1::InputImageType InputImageType; + typedef typename Superclass1::InputImagePointer InputImagePointer; + typedef typename Superclass1::InputImageConstPointer InputImageConstPointer; + typedef typename Superclass1::InputImageRegionType InputImageRegionType; + typedef typename Superclass1::InputImagePixelType InputImagePixelType; + typedef typename Superclass1::ImageSampleType ImageSampleType; + typedef typename Superclass1::ImageSampleContainerType ImageSampleContainerType; + typedef typename Superclass1::MaskType MaskType; + typedef typename Superclass1::InputImageIndexType InputImageIndexType; + typedef typename Superclass1::InputImagePointType InputImagePointType; + + /** The input image dimension. */ + itkStaticConstMacro(InputImageDimension, unsigned int, Superclass1::InputImageDimension); + + /** The input image dimension. */ + itkStaticConstMacro(ReducedInputImageDimension, unsigned int, Superclass1::InputImageDimension - 1); + + /** Typedefs inherited from Elastix. */ + typedef typename Superclass2::ElastixType ElastixType; + typedef typename Superclass2::RegistrationType RegistrationType; + typedef typename Superclass2::ITKBaseType ITKBaseType; + +protected: + /** The constructor. */ + ReducedFullSampler() {} + /** The destructor. */ + virtual ~ReducedFullSampler() {} + +private: + /** The private constructor. */ + ReducedFullSampler(const Self &); // purposely not implemented + /** The private copy constructor. */ + void + operator=(const Self &); // purposely not implemented + + elxOverrideGetSelfMacro; +}; + +} // end namespace elastix + +#ifndef ITK_MANUAL_INSTANTIATION +# include "elxReducedFullSampler.hxx" +#endif + +#endif // end #ifndef __elxReducedFullSampler_h diff --git a/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.hxx b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.hxx new file mode 100644 index 000000000..91f895489 --- /dev/null +++ b/Components/ImageSamplers/ReducedFull/elxReducedFullSampler.hxx @@ -0,0 +1,27 @@ +/*====================================================================== + + 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. + +======================================================================*/ + +#ifndef __elxReducedFullSampler_hxx +#define __elxReducedFullSampler_hxx + +#include "elxReducedFullSampler.h" + +namespace elastix +{ + +// nothing + +} // end namespace elastix + +#endif // end #ifndef __elxReducedFullSampler_hxx