18 #include <itkBSplineInterpolateImageFunction.h> 19 #include <itkLinearInterpolateImageFunction.h> 20 #include <itkNearestNeighborInterpolateImageFunction.h> 24 struct mitk::ExtractSliceFilter2::Impl
31 itk::Object::Pointer InterpolateImageFunction;
34 mitk::ExtractSliceFilter2::Impl::Impl()
39 mitk::ExtractSliceFilter2::Impl::~Impl()
45 template <
class TInputImage>
48 typename itk::InterpolateImageFunction<TInputImage>::Pointer interpolateImageFunction;
53 interpolateImageFunction = itk::NearestNeighborInterpolateImageFunction<TInputImage>::New().GetPointer();
57 interpolateImageFunction = itk::LinearInterpolateImageFunction<TInputImage>::New().GetPointer();
62 auto bSplineInterpolateImageFunction = itk::BSplineInterpolateImageFunction<TInputImage>::New();
63 bSplineInterpolateImageFunction->SetSplineOrder(2);
64 interpolateImageFunction = bSplineInterpolateImageFunction.GetPointer();
72 interpolateImageFunction->SetInputImage(inputImage);
74 result = interpolateImageFunction.GetPointer();
77 template <
typename TPixel,
unsigned int VImageDimension>
80 typedef itk::Image<TPixel, VImageDimension> TInputImage;
81 typedef itk::InterpolateImageFunction<TInputImage> TInterpolateImageFunction;
84 auto interpolator =
static_cast<TInterpolateImageFunction*
>(interpolateImageFunction);
86 auto origin = outputGeometry->
GetOrigin();
87 auto spacing = outputGeometry->GetSpacing();
88 auto xDirection = outputGeometry->GetAxisVector(0);
89 auto yDirection = outputGeometry->GetAxisVector(1);
91 xDirection.Normalize();
92 yDirection.Normalize();
94 auto spacingAlongXDirection = xDirection * spacing[0];
95 auto spacingAlongYDirection = yDirection * spacing[1];
98 const std::size_t width = outputGeometry->GetExtent(0);
99 const std::size_t xBegin = outputRegion.GetIndex(0);
100 const std::size_t yBegin = outputRegion.GetIndex(1);
101 const std::size_t xEnd = xBegin + outputRegion.GetSize(0);
102 const std::size_t yEnd = yBegin + outputRegion.GetSize(1);
105 auto data =
static_cast<char*
>(writeAccess.GetData());
107 const TPixel backgroundPixel = std::numeric_limits<TPixel>::lowest();
110 itk::ContinuousIndex<mitk::ScalarType, 3> index;
114 for (std::size_t y = yBegin; y < yEnd; ++y)
116 yPoint = origin + spacingAlongYDirection * y;
118 for (std::size_t x = xBegin; x < xEnd; ++x)
120 point = yPoint + spacingAlongXDirection * x;
122 if (inputImage->TransformPhysicalPointToContinuousIndex(point, index))
124 pixel = interpolator->EvaluateAtContinuousIndex(index);
125 memcpy(static_cast<void*>(data + pixelSize * (width * y + x)), static_cast<const void*>(&pixel), pixelSize);
129 memcpy(static_cast<void*>(data + pixelSize * (width * y + x)), static_cast<const void*>(&backgroundPixel), pixelSize);
135 void VerifyInputImage(
const mitk::Image* inputImage)
140 mitkThrow() <<
"Input images with " << dimension <<
" dimensions are not supported.";
143 mitkThrow() <<
"Input image is not initialized.";
146 mitkThrow() <<
"Input image volume is not set.";
150 if (
nullptr == geometry || !geometry->IsValid())
151 mitkThrow() <<
"Input image has invalid geometry.";
153 if (!geometry->GetImageGeometry())
154 mitkThrow() <<
"Geometry of input image is not an image geometry.";
159 if (
nullptr == outputGeometry)
160 mitkThrow() <<
"Output geometry is not set.";
163 mitkThrow() <<
"Output geometry is not an image geometry.";
167 mitk::ExtractSliceFilter2::ExtractSliceFilter2()
172 mitk::ExtractSliceFilter2::~ExtractSliceFilter2()
177 void mitk::ExtractSliceFilter2::AllocateOutputs()
179 const auto* inputImage = this->
GetInput();
180 const auto* outputGeometry = this->GetOutputGeometry();
184 outputImage->
Initialize(pixelType, 1, *outputGeometry);
186 auto data =
new char[
static_cast<std::size_t
>(pixelType.GetSize() * outputGeometry->
GetExtent(0) * outputGeometry->
GetExtent(1))];
214 void mitk::ExtractSliceFilter2::GenerateData()
216 if (
nullptr != m_Impl->InterpolateImageFunction && this->GetInput()->GetMTime() < this->GetMTime())
219 const auto* inputImage = this->
GetInput();
224 auto outputRegion = this->
GetOutput()->GetLargestPossibleRegion();
235 m_Impl->InterpolateImageFunction =
nullptr;
241 mitkThrow() <<
"Input index " << index <<
" is invalid.";
248 return m_Impl->OutputGeometry;
253 if (m_Impl->OutputGeometry != outputGeometry)
255 m_Impl->OutputGeometry = outputGeometry;
262 return m_Impl->Interpolator;
267 if (m_Impl->Interpolator != interpolator)
269 m_Impl->Interpolator = interpolator;
270 m_Impl->InterpolateImageFunction =
nullptr;
275 void mitk::ExtractSliceFilter2::VerifyInputInformation()
277 Superclass::VerifyInputInformation();
280 VerifyOutputGeometry(this->GetOutputGeometry());
virtual bool GetImageGeometry() const
Is this an ImageGeometry?
virtual void Initialize(const mitk::PixelType &type, unsigned int dimension, const unsigned int *dimensions, unsigned int channels=1)
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
bool IsVolumeSet(int t=0, int n=0) const override
Check whether volume at time t in channel n is set.
virtual void SetInput(const InputImageType *image)
ScalarType GetExtent(unsigned int direction) const
Set the time bounds (in ms)
SlicedData::RegionType OutputImageRegionType
#define AccessFixedDimensionByItk_3(mitkImage, itkImageTypeFunction, dimension, arg1, arg2, arg3)
virtual mitk::PlaneGeometry * GetPlaneGeometry(int s) const
Returns the PlaneGeometry of the slice (s).
itk::SmartPointer< Self > Pointer
unsigned int GetDimension() const
Get dimension of the image.
#define AccessFixedDimensionByItk_2(mitkImage, itkImageTypeFunction, dimension, arg1, arg2)
Image class for storing images.
virtual bool SetImportVolume(void *data, int t=0, int n=0, ImportMemoryManagementType importMemoryManagement=CopyMemory)
Set data as volume at time t in channel n. It is in the responsibility of the caller to ensure that t...
SlicedGeometry3D * GetSlicedGeometry(unsigned int t=0) const
Convenience access method for the geometry, which is of type SlicedGeometry3D (or a sub-class of it)...
mitk::Image::Pointer image
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
vcl_size_t GetSize() const
Get size of the PixelType in bytes.
InputImageType * GetInput(void)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
virtual void AllocateOutputs()
The GenerateData method normally allocates the buffers for all of the outputs of a filter...
OutputType * GetOutput()
Get the output data of this image source object.
ImageWriteAccessor class to get locked write-access for a particular image part.
Describes a two-dimensional, rectangular plane.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
static itk::InterpolateImageFunction< TInputImage >::Pointer CreateInterpolateImageFunction(InterpolateImageFunction::Enum interpolator)