Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
itkResampleDwiImageFilter.txx
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 /*=========================================================================
17 
18 Program: Tensor ToolKit - TTK
19 Module: $URL: svn://scm.gforge.inria.fr/svn/ttk/trunk/Algorithms/itkResampleDwiImageFilter.txx $
20 Language: C++
21 Date: $Date: 2010-06-07 13:39:13 +0200 (Mo, 07 Jun 2010) $
22 Version: $Revision: 68 $
23 
24 Copyright (c) INRIA 2010. All rights reserved.
25 See LICENSE.txt for details.
26 
27 This software is distributed WITHOUT ANY WARRANTY; without even
28 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
29 PURPOSE. See the above copyright notices for more information.
30 
31 =========================================================================*/
32 #ifndef _itk_ResampleDwiImageFilter_txx_
33 #define _itk_ResampleDwiImageFilter_txx_
34 #endif
35 
36 #define _USE_MATH_DEFINES
37 
38 #include "itkResampleDwiImageFilter.h"
39 #include <itkImageRegionIterator.h>
40 #include <itkImageRegion.h>
41 #include <itkResampleImageFilter.h>
42 #include <itkBSplineInterpolateImageFunction.h>
43 #include <itkNearestNeighborInterpolateImageFunction.h>
44 #include <itkLinearInterpolateImageFunction.h>
45 #include <itkWindowedSincInterpolateImageFunction.h>
46 
47 namespace itk
48 {
49 
50 template <class TScalarType>
51 ResampleDwiImageFilter<TScalarType>
52 ::ResampleDwiImageFilter()
53  : m_Interpolation(Interpolate_Linear)
54 {
55  this->SetNumberOfRequiredInputs( 1 );
56 }
57 
58 template <class TScalarType>
59 void
60 ResampleDwiImageFilter<TScalarType>
61 ::GenerateData()
62 {
63 // // initialize output image
64 // itk::Vector< double, 3 > spacing = this->GetInput()->GetSpacing();
65 // spacing[0] /= m_SamplingFactor[0];
66 // spacing[1] /= m_SamplingFactor[1];
67 // spacing[2] /= m_SamplingFactor[2];
68 // ImageRegion<3> region = this->GetInput()->GetLargestPossibleRegion();
69 // region.SetSize(0, region.GetSize(0)*m_SamplingFactor[0]);
70 // region.SetSize(1, region.GetSize(1)*m_SamplingFactor[1]);
71 // region.SetSize(2, region.GetSize(2)*m_SamplingFactor[2]);
72 
73  itk::Point<double,3> origin = this->GetInput()->GetOrigin();
74  origin[0] -= this->GetInput()->GetSpacing()[0]/2;
75  origin[1] -= this->GetInput()->GetSpacing()[1]/2;
76  origin[2] -= this->GetInput()->GetSpacing()[2]/2;
77 
78  origin[0] += m_NewSpacing[0]/2;
79  origin[1] += m_NewSpacing[1]/2;
80  origin[2] += m_NewSpacing[2]/2;
81 
82  typename DwiImageType::Pointer outImage = DwiImageType::New();
83  outImage->SetSpacing( m_NewSpacing );
84  outImage->SetOrigin( origin );
85  outImage->SetDirection( this->GetInput()->GetDirection() );
86  outImage->SetLargestPossibleRegion( m_NewImageRegion );
87  outImage->SetBufferedRegion( m_NewImageRegion );
88  outImage->SetRequestedRegion( m_NewImageRegion );
89  outImage->SetVectorLength( this->GetInput()->GetVectorLength() );
90  outImage->Allocate();
91 
92  typename itk::ResampleImageFilter<DwiChannelType, DwiChannelType>::Pointer resampler = itk::ResampleImageFilter<DwiChannelType, DwiChannelType>::New();
93  resampler->SetOutputParametersFromImage(outImage);
94 
95  switch (m_Interpolation)
96  {
97  case Interpolate_NearestNeighbour:
98  {
99  typename itk::NearestNeighborInterpolateImageFunction<DwiChannelType>::Pointer interp = itk::NearestNeighborInterpolateImageFunction<DwiChannelType>::New();
100  resampler->SetInterpolator(interp);
101  break;
102  }
103  case Interpolate_Linear:
104  {
105  typename itk::LinearInterpolateImageFunction<DwiChannelType>::Pointer interp = itk::LinearInterpolateImageFunction<DwiChannelType>::New();
106  resampler->SetInterpolator(interp);
107  break;
108  }
109  case Interpolate_BSpline:
110  {
111  typename itk::BSplineInterpolateImageFunction<DwiChannelType>::Pointer interp = itk::BSplineInterpolateImageFunction<DwiChannelType>::New();
112  resampler->SetInterpolator(interp);
113  break;
114  }
115  case Interpolate_WindowedSinc:
116  {
117  typename itk::WindowedSincInterpolateImageFunction<DwiChannelType, 3>::Pointer interp = itk::WindowedSincInterpolateImageFunction<DwiChannelType, 3>::New();
118  resampler->SetInterpolator(interp);
119  break;
120  }
121  default:
122  {
123  typename itk::LinearInterpolateImageFunction<DwiChannelType>::Pointer interp = itk::LinearInterpolateImageFunction<DwiChannelType>::New();
124  resampler->SetInterpolator(interp);
125  }
126  }
127 
128  for (unsigned int i=0; i<this->GetInput()->GetVectorLength(); i++)
129  {
130  typename DwiChannelType::Pointer channel = DwiChannelType::New();
131  channel->SetSpacing( this->GetInput()->GetSpacing() );
132  channel->SetOrigin( this->GetInput()->GetOrigin() );
133  channel->SetDirection( this->GetInput()->GetDirection() );
134  channel->SetLargestPossibleRegion( this->GetInput()->GetLargestPossibleRegion() );
135  channel->SetBufferedRegion( this->GetInput()->GetLargestPossibleRegion() );
136  channel->SetRequestedRegion( this->GetInput()->GetLargestPossibleRegion() );
137  channel->Allocate();
138 
139  ImageRegionIterator<DwiChannelType> it(channel, channel->GetLargestPossibleRegion());
140  while(!it.IsAtEnd())
141  {
142  typename DwiImageType::PixelType pix = this->GetInput()->GetPixel(it.GetIndex());
143  it.Set(pix[i]);
144  ++it;
145  }
146 
147  resampler->SetInput(channel);
148  resampler->Update();
149  channel = resampler->GetOutput();
150 
151  ImageRegionIterator<DwiImageType> it2(outImage, outImage->GetLargestPossibleRegion());
152  while(!it2.IsAtEnd())
153  {
154  typename DwiImageType::PixelType pix = it2.Get();
155  pix[i] = channel->GetPixel(it2.GetIndex());
156  it2.Set(pix);
157  ++it2;
158  }
159  }
160 
161  this->SetNthOutput(0, outImage);
162 }
163 
164 template <class TScalarType>
165 void
166 ResampleDwiImageFilter<TScalarType>
167 ::UpdateOutputInformation()
168 {
169  // Calls to superclass updateoutputinformation
170  //Superclass::UpdateOutputInformation();
171 
172  this->GetOutput()->SetSpacing(m_NewSpacing);
173  this->GetOutput()->SetLargestPossibleRegion(m_NewImageRegion);
174 }
175 
176 } // end of namespace