Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <itkResampleImageFilter.h>
19 #include <mitkImageCast.h>
20 #include <mitkImageAccessByItk.h>
21 #include <itkImageFileWriter.h>
22 #include <itkIdentityTransform.h>
23 #include <itkLinearInterpolateImageFunction.h>
24 #include <itkNearestNeighborInterpolateImageFunction.h>
25 
27 m_ResampledX(100), m_ResampledY(100),m_ResampledZ(1)
28 {
29 }
30 
32 {
33 }
34 
35 
36 
37 
38 
40 {
41  // set input image
42 
43  mitk::Image::ConstPointer inputImage = this->GetInput(0) ;
44  if ( (inputImage->GetDimension() > 3) || (inputImage->GetDimension() < 2) )
45  {
46  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData works only with 2D and 3D images, sorry." << std::endl;
47  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData works only with 2D and 3D images, sorry.");
48  return;
49  }
50 
51  if ( (inputImage->GetDimension(0)<m_ResampledX) || (inputImage->GetDimension(1)<m_ResampledY) || (inputImage->GetDimension(2)<m_ResampledZ) )
52  {
53  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData only downsamples. Your requested dimensions exceed the original image dimensions." << std::endl;
54  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData only downsamples. Your requested dimensions exceed the original image dimensions.");
55  return;
56  }
57 
58  if ( (m_ResampledX < 1) || (m_ResampledY < 1)|| (m_ResampledZ < 1) )
59  {
60  MITK_ERROR << "mitk::TofImageDownsamplingFilter:GenerateData works only for positive input dimensions " << std::endl;
61  itkExceptionMacro("mitk::TofImageDownsamplingFilter:GenerateData works only for positive input dimensions");
62  return;
63  }
64 
65  switch(inputImage->GetDimension())
66  {
67  case 2:
68  {
69  AccessFixedDimensionByItk( inputImage.GetPointer(), ItkImageResampling, 2 ); break;
70  }
71  case 3:
72  {
73  AccessFixedDimensionByItk( inputImage.GetPointer(), ItkImageResampling, 3 ); break;
74  }
75 
76  default: break;
77  }
78 
79 
80 }
81 
82 template<typename TPixel, unsigned int VImageDimension>
83 void mitk::ToFImageDownsamplingFilter::ItkImageResampling( const itk::Image<TPixel,VImageDimension>* itkImage )
84 {
85  // declare typdef for itk image from input mitk image
86  typedef itk::Image< TPixel, VImageDimension > ItkImageType;
87 
88  //declare itk filter related typedefs (transform type, interpolater, and size type)
89  typedef itk::ResampleImageFilter<ItkImageType,ItkImageType> ResamplerFilterType;
90  typedef itk::IdentityTransform<double, VImageDimension> TransformType;
91  typedef itk::NearestNeighborInterpolateImageFunction<ItkImageType, double > InterpolatorType;
92  typedef typename ItkImageType::SizeType::SizeValueType SizeValueType;
93 
94  //instantiate filter related parameters
96  typename TransformType::Pointer transform = TransformType::New();
97  typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
98 
99  // establish size for downsampled image ( the result of this filter)
100  typename ItkImageType::SizeType inputSize = itkImage->GetLargestPossibleRegion().GetSize();
101  typename ItkImageType::SizeType size;
102 
103  size[0] = static_cast< SizeValueType >( m_ResampledX );
104  size[1] = static_cast< SizeValueType >( m_ResampledY );
105  size[2] = static_cast< SizeValueType >( m_ResampledZ );
106 
107  //establish spacing for new downsampled image ( resulting image)
108  const typename ItkImageType::SpacingType& inputSpacing = itkImage->GetSpacing();
109  typename ItkImageType::SpacingType outputSpacing;
110 
111  outputSpacing[0] = inputSpacing[0] * ( inputSize[0]/ m_ResampledX );
112  outputSpacing[1] = inputSpacing[1] * ( inputSize[1]/ m_ResampledY );
113  outputSpacing[2] = inputSpacing[2] * ( inputSize[2]/ m_ResampledZ );
114 
115  mitk::Vector3D mitkspacing;
116  mitkspacing[0] = outputSpacing[0];
117  mitkspacing[1] = outputSpacing[1];
118  mitkspacing[2] = outputSpacing[2];
119 
120  mitk::Point3D mitkorig;
121  mitkorig[0] = itkImage->GetOrigin()[0];
122  mitkorig[1] = itkImage->GetOrigin()[1];
123  mitkorig[2] = 0.0;
124 
125  // set filter parameters and update
126  transform->SetIdentity();
127  resampler->SetTransform(transform);
128  resampler->SetInterpolator(interpolator);
129  resampler->SetOutputSpacing(outputSpacing);
130  resampler->SetOutputOrigin(itkImage->GetOrigin());
131  resampler->SetSize(size);
132  resampler->SetInput(itkImage);
133  resampler->UpdateLargestPossibleRegion();
134 
135  // Create mitk container for resulting image
137  resultImage->SetSpacing(mitkspacing);
138  resultImage->SetOrigin(mitkorig);
139 
140  // Cast itk image to mitk image
141  mitk::CastToMitkImage(resampler->GetOutput(), resultImage);
142 }
itk::SmartPointer< Self > Pointer
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void ItkImageResampling(const itk::Image< TPixel, VImageDimension > *itkImage)
Templated method for ITK image type which performs the resampling with an itk filter.
virtual void GenerateData() override
Method generating the output of this filter. Called in the updated process of the pipeline...
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:78
OutputType * GetOutput()
Get the output data of this image source object.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.