diff --git a/Modules/AlgorithmsExt/include/mitkPadImageFilter.h b/Modules/AlgorithmsExt/include/mitkPadImageFilter.h index cd0d63d72f..d83b134489 100644 --- a/Modules/AlgorithmsExt/include/mitkPadImageFilter.h +++ b/Modules/AlgorithmsExt/include/mitkPadImageFilter.h @@ -1,68 +1,71 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef mitkPadImageFilter_h #define mitkPadImageFilter_h #include "MitkAlgorithmsExtExports.h" #include "mitkCommon.h" #include "mitkImageTimeSelector.h" #include "mitkImageToImageFilter.h" #include "itkImage.h" namespace mitk { /** * \brief PadImageFilter class pads the first input image to the size of the second input image. * Two Images have to be set. * The first image is the image to pad. The second image defines the pad size. * It is also possible to use an included binary filter. * * \ingroup Process */ class MITKALGORITHMSEXT_EXPORT PadImageFilter : public ImageToImageFilter { public: mitkClassMacro(PadImageFilter, ImageToImageFilter); itkFactorylessNewMacro(Self); itkCloneMacro(Self); /** \brief Sets the intensity of the pixel to pad */ itkSetMacro(PadConstant, int); /** \brief sets the binary filter ON or OFF */ itkSetMacro(BinaryFilter, bool); /** \brief Sets the lower threshold of the included binary filter */ itkSetMacro(LowerThreshold, int); /** \brief Sets the upper threshold of the included binary filter */ itkSetMacro(UpperThreshold, int); protected: PadImageFilter(); ~PadImageFilter() override; void GenerateData() override; private: + template + void GenerateDataInternal(SourceImageType* sourceItkImage, mitk::Image::Pointer outputImage); + bool m_BinaryFilter; int m_PadConstant, m_LowerThreshold, m_UpperThreshold; }; } // namespace mitk #endif diff --git a/Modules/AlgorithmsExt/src/mitkPadImageFilter.cpp b/Modules/AlgorithmsExt/src/mitkPadImageFilter.cpp index 332f79e0b3..b845c63879 100644 --- a/Modules/AlgorithmsExt/src/mitkPadImageFilter.cpp +++ b/Modules/AlgorithmsExt/src/mitkPadImageFilter.cpp @@ -1,101 +1,105 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include "mitkPadImageFilter.h" #include "mitkImageCast.h" - +#include #include "itkBinaryThresholdImageFilter.h" #include "itkConstantPadImageFilter.h" mitk::PadImageFilter::PadImageFilter() { this->SetNumberOfIndexedInputs(2); this->SetNumberOfRequiredInputs(2); m_BinaryFilter = false; m_PadConstant = -32766; m_LowerThreshold = -32766; m_UpperThreshold = -32765; } mitk::PadImageFilter::~PadImageFilter() { } -void mitk::PadImageFilter::GenerateData() + +template +void mitk::PadImageFilter::GenerateDataInternal(SourceImageType* sourceItkImage, mitk::Image::Pointer outputImage) { - mitk::Image::ConstPointer image = this->GetInput(0); + mitk::Image::ConstPointer sourceImage = this->GetInput(0); mitk::Image::ConstPointer referenceImage = this->GetInput(1); - typedef itk::Image ImageType; - ImageType::Pointer itkImage = ImageType::New(); - mitk::CastToItkImage(image, itkImage); - - mitk::BaseGeometry *imageGeometry = image->GetGeometry(); + mitk::BaseGeometry* imageGeometry = sourceImage->GetGeometry(); mitk::Point3D origin = imageGeometry->GetOrigin(); mitk::Vector3D spacing = imageGeometry->GetSpacing(); - mitk::BaseGeometry *referenceImageGeometry = referenceImage->GetGeometry(); + mitk::BaseGeometry* referenceImageGeometry = referenceImage->GetGeometry(); mitk::Point3D referenceOrigin = referenceImageGeometry->GetOrigin(); double outputOrigin[3]; itk::SizeValueType padLowerBound[3]; itk::SizeValueType padUpperBound[3]; int i; for (i = 0; i < 3; ++i) { outputOrigin[i] = referenceOrigin[i]; padLowerBound[i] = static_cast((origin[i] - referenceOrigin[i]) / spacing[i] + 0.5); - padUpperBound[i] = referenceImage->GetDimension(i) - image->GetDimension(i) - padLowerBound[i]; + padUpperBound[i] = referenceImage->GetDimension(i) - sourceImage->GetDimension(i) - padLowerBound[i]; } // The origin of the input image is passed through the filter and used as // output origin as well. Hence, it needs to be overwritten accordingly. - itkImage->SetOrigin(outputOrigin); + sourceItkImage->SetOrigin(outputOrigin); - typedef itk::ConstantPadImageFilter PadFilterType; + typedef itk::ConstantPadImageFilter PadFilterType; PadFilterType::Pointer padFilter = PadFilterType::New(); - padFilter->SetInput(itkImage); + padFilter->SetInput(sourceItkImage); padFilter->SetConstant(m_PadConstant); padFilter->SetPadLowerBound(padLowerBound); padFilter->SetPadUpperBound(padUpperBound); - mitk::Image::Pointer outputImage = this->GetOutput(); - // If the Binary flag is set, use an additional binary threshold filter after // padding. if (m_BinaryFilter) { typedef itk::Image BinaryImageType; - typedef itk::BinaryThresholdImageFilter BinaryFilterType; + typedef itk::BinaryThresholdImageFilter BinaryFilterType; BinaryFilterType::Pointer binaryFilter = BinaryFilterType::New(); binaryFilter->SetInput(padFilter->GetOutput()); binaryFilter->SetLowerThreshold(m_LowerThreshold); binaryFilter->SetUpperThreshold(m_UpperThreshold); binaryFilter->SetInsideValue(1); binaryFilter->SetOutsideValue(0); binaryFilter->Update(); mitk::CastToMitkImage(binaryFilter->GetOutput(), outputImage); } else { padFilter->Update(); mitk::CastToMitkImage(padFilter->GetOutput(), outputImage); } +} + +void mitk::PadImageFilter::GenerateData() +{ + mitk::Image::Pointer image = this->GetInput(0); + mitk::Image::Pointer outputImage = this->GetOutput(); + + AccessFixedDimensionByItk_1(image, GenerateDataInternal, 3, outputImage); outputImage->SetRequestedRegionToLargestPossibleRegion(); }