Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkToFImageDownsamplingFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <itkResampleImageFilter.h>
15 #include <mitkImageCast.h>
16 #include <mitkImageAccessByItk.h>
17 #include <itkImageFileWriter.h>
18 #include <itkIdentityTransform.h>
19 #include <itkLinearInterpolateImageFunction.h>
20 #include <itkNearestNeighborInterpolateImageFunction.h>
21 
23 m_ResampledX(100), m_ResampledY(100),m_ResampledZ(1)
24 {
25 }
26 
28 {
29 }
30 
31 
32 
33 
34 
36 {
37  // set input image
38 
39  mitk::Image::ConstPointer inputImage = this->GetInput(0) ;
40  if ( (inputImage->GetDimension() > 3) || (inputImage->GetDimension() < 2) )
41  {
42  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData works only with 2D and 3D images, sorry." << std::endl;
43  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData works only with 2D and 3D images, sorry.");
44  return;
45  }
46 
47  if ( (inputImage->GetDimension(0)<m_ResampledX) || (inputImage->GetDimension(1)<m_ResampledY) || (inputImage->GetDimension(2)<m_ResampledZ) )
48  {
49  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData only downsamples. Your requested dimensions exceed the original image dimensions." << std::endl;
50  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData only downsamples. Your requested dimensions exceed the original image dimensions.");
51  return;
52  }
53 
54  if ( (m_ResampledX < 1) || (m_ResampledY < 1)|| (m_ResampledZ < 1) )
55  {
56  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData works only for positive input dimensions " << std::endl;
57  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData works only for positive input dimensions");
58  return;
59  }
60 
61  switch(inputImage->GetDimension())
62  {
63  case 2:
64  {
65  AccessFixedDimensionByItk( inputImage.GetPointer(), ItkImageResampling, 2 ); break;
66  }
67  case 3:
68  {
69  AccessFixedDimensionByItk( inputImage.GetPointer(), ItkImageResampling, 3 ); break;
70  }
71 
72  default: break;
73  }
74 
75 
76 }
77 
78 template<typename TPixel, unsigned int VImageDimension>
79 void mitk::ToFImageDownsamplingFilter::ItkImageResampling( const itk::Image<TPixel,VImageDimension>* itkImage )
80 {
81  // declare typdef for itk image from input mitk image
82  typedef itk::Image< TPixel, VImageDimension > ItkImageType;
83 
84  //declare itk filter related typedefs (transform type, interpolater, and size type)
85  typedef itk::ResampleImageFilter<ItkImageType,ItkImageType> ResamplerFilterType;
86  typedef itk::IdentityTransform<double, VImageDimension> TransformType;
87  typedef itk::NearestNeighborInterpolateImageFunction<ItkImageType, double > InterpolatorType;
88  typedef typename ItkImageType::SizeType::SizeValueType SizeValueType;
89 
90  //instantiate filter related parameters
91  typename ResamplerFilterType::Pointer resampler = ResamplerFilterType::New();
92  typename TransformType::Pointer transform = TransformType::New();
93  typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
94 
95  // establish size for downsampled image ( the result of this filter)
96  typename ItkImageType::SizeType inputSize = itkImage->GetLargestPossibleRegion().GetSize();
97  typename ItkImageType::SizeType size;
98 
99  size[0] = static_cast< SizeValueType >( m_ResampledX );
100  size[1] = static_cast< SizeValueType >( m_ResampledY );
101  size[2] = static_cast< SizeValueType >( m_ResampledZ );
102 
103  //establish spacing for new downsampled image ( resulting image)
104  const typename ItkImageType::SpacingType& inputSpacing = itkImage->GetSpacing();
105  typename ItkImageType::SpacingType outputSpacing;
106 
107  outputSpacing[0] = inputSpacing[0] * ( inputSize[0]/ m_ResampledX );
108  outputSpacing[1] = inputSpacing[1] * ( inputSize[1]/ m_ResampledY );
109 
110  if (VImageDimension > 2)
111  {
112  outputSpacing[2] = inputSpacing[2] * ( inputSize[2]/ m_ResampledZ );
113  }
114  else
115  {
116  outputSpacing[2] = 0.0;
117  }
118 
119  mitk::Vector3D mitkspacing;
120  mitkspacing[0] = outputSpacing[0];
121  mitkspacing[1] = outputSpacing[1];
122  mitkspacing[2] = outputSpacing[2];
123 
124  mitk::Point3D mitkorig;
125  mitkorig[0] = itkImage->GetOrigin()[0];
126  mitkorig[1] = itkImage->GetOrigin()[1];
127  mitkorig[2] = 0.0;
128 
129  // set filter parameters and update
130  transform->SetIdentity();
131  resampler->SetTransform(transform);
132  resampler->SetInterpolator(interpolator);
133  resampler->SetOutputSpacing(outputSpacing);
134  resampler->SetOutputOrigin(itkImage->GetOrigin());
135  resampler->SetSize(size);
136  resampler->SetInput(itkImage);
137  resampler->UpdateLargestPossibleRegion();
138 
139  // Create mitk container for resulting image
141  resultImage->SetSpacing(mitkspacing);
142  resultImage->SetOrigin(mitkorig);
143 
144  // Cast itk image to mitk image
145  mitk::CastToMitkImage(resampler->GetOutput(), resultImage);
146 }
double m_ResampledX
length of x dimension of output image in pixels
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void ItkImageResampling(const itk::Image< TPixel, VImageDimension > *itkImage)
Templated method for ITK image type which performs the resampling with an itk filter.
double m_ResampledZ
length of z dimension of output image in pixels (if 2D, default is set to 1)
double m_ResampledY
length of y dimension of output image in pixels
void GenerateData() override
Method generating the output of this filter. Called in the updated process of the pipeline...
InputImageType * GetInput(void)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
OutputType * GetOutput()
Get the output data of this image source object.
~ToFImageDownsamplingFilter() override
Standard destructor.