Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkSplitDWImageFilter.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 #ifndef __itkSplitDWImageFilter_txx
17 #define __itkSplitDWImageFilter_txx
18 
19 #include "itkSplitDWImageFilter.h"
20 
21 #include <itkImageRegionConstIterator.h>
22 #include <itkImageRegionIterator.h>
23 
24 template< class TInputImagePixelType,
25  class TOutputImagePixelType>
26 itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
27 ::SplitDWImageFilter()
28  : m_IndexList(0),
29  m_ExtractAllImages(true)
30 {
31  this->SetNumberOfRequiredInputs(1);
32 }
33 
34 template< class TInputImagePixelType,
35  class TOutputImagePixelType>
36 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
37 ::CopyInformation( const DataObject* /*data*/)
38 {
39 
40 }
41 
42 template< class TInputImagePixelType,
43  class TOutputImagePixelType>
44 void itk::SplitDWImageFilter<
45 TInputImagePixelType, TOutputImagePixelType >::GenerateOutputInformation()
46 {
47 
48 }
49 
50 template< class TInputImagePixelType,
51  class TOutputImagePixelType>
52 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
53 ::SetExtractAllAboveThreshold(double b_threshold, BValueMapType map)
54 {
55  m_ExtractAllImages = false;
56  m_IndexList.clear();
57 
58  // create the index list following the given threshold
59  // iterate over the b-value map
60  BValueMapType::const_iterator bvalueIt = map.begin();
61  while( bvalueIt != map.end() )
62  {
63  // check threshold
64  if (bvalueIt->first > b_threshold)
65  {
66  // the map contains an index container, this needs to be inserted into the
67  // index list
68  IndexListType::const_iterator listIt = bvalueIt->second.begin();
69  while( listIt != bvalueIt->second.end() )
70  {
71  m_IndexList.push_back( *listIt );
72  ++listIt;
73  }
74  }
75 
76  ++bvalueIt;
77  }
78 }
79 
80 template< class TInputImagePixelType,
81  class TOutputImagePixelType>
82 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
83 ::SetExtractSingleShell(double b_value, BValueMapType map, double tol)
84 {
85  m_ExtractAllImages = false;
86  m_IndexList.clear();
87 
88  // create index list
89  BValueMapType::const_iterator bvalueIt = map.begin();
90  while( bvalueIt != map.end() )
91  {
92  IndexListType::const_iterator listIt = bvalueIt->second.begin();
93  if( std::fabs( bvalueIt->first - b_value) < tol)
94  {
95  m_IndexList.insert( m_IndexList.begin(), bvalueIt->second.begin(), bvalueIt->second.end() );
96  ++listIt;
97  }
98 
99  ++bvalueIt;
100  }
101 }
102 
103 template< class TInputImagePixelType,
104  class TOutputImagePixelType>
105 void itk::SplitDWImageFilter<
106 TInputImagePixelType, TOutputImagePixelType >::GenerateData()
107 {
108 
109  if( m_IndexList.empty() && !m_ExtractAllImages )
110  {
111  itkExceptionMacro(<<"The index list is empty and the option to extract all images is disabled. ");
112  }
113 
114  // construct the index list (for each component)
115  if( m_ExtractAllImages )
116  {
117  m_IndexList.clear();
118 
119  for( unsigned int i=0; i< this->GetInput()->GetNumberOfComponentsPerPixel(); i++)
120  m_IndexList.push_back(i);
121  }
122 
123  // declare the output image
124  // this will have the b0 images stored as timesteps
125  typename OutputImageType::Pointer outputImage = this->GetOutput();
126  //OutputImageType::New();
127 
128  // allocate image with
129  // - dimension: [DimX, DimY, DimZ, size(IndexList) ]
130  // - spacing: old one, 1.0 time
131 
132  typename OutputImageType::SpacingType spacing;
133  spacing.Fill(1);
134 
135  typename OutputImageType::PointType origin;
136  origin.Fill(0);
137 
138  OutputImageRegionType outputRegion;
139 
140  // the spacing and origin corresponds to the respective values in the input image (3D)
141  // the same for the region
142  for (unsigned int i=0; i< 3; i++)
143  {
144  spacing[i] = (this->GetInput()->GetSpacing())[i];
145  origin[i] = (this->GetInput()->GetOrigin())[i];
146  outputRegion.SetSize(i, this->GetInput()->GetLargestPossibleRegion().GetSize()[i]);
147  outputRegion.SetIndex(i, this->GetInput()->GetLargestPossibleRegion().GetIndex()[i]);
148  }
149 
150  // number of timesteps = number of b0 images
151  outputRegion.SetSize(3, m_IndexList.size());
152  outputRegion.SetIndex( 3, 0 );
153 
154  // output image direction (4x4)
155  typename OutputImageType::DirectionType outputDirection;
156  //initialize to identity
157  outputDirection.SetIdentity();
158  // get the input image direction ( 3x3 matrix )
159  typename InputImageType::DirectionType inputDirection = this->GetInput()->GetDirection();
160 
161  for( unsigned int i=0; i< 3; i++)
162  {
163  outputDirection(0,i) = inputDirection(0,i);
164  outputDirection(1,i) = inputDirection(1,i);
165  outputDirection(2,i) = inputDirection(2,i);
166  }
167 
168  outputImage->SetSpacing( spacing );
169  outputImage->SetOrigin( origin );
170  outputImage->SetDirection( outputDirection );
171  outputImage->SetRegions( outputRegion );
172  outputImage->Allocate();
173 
174  // input iterator
175  itk::ImageRegionConstIterator<InputImageType> inputIt( this->GetInput(),
176  this->GetInput()->GetLargestPossibleRegion() );
177 
178 
179  // we want to iterate separately over each 3D volume of the output image
180  // so reset the regions last dimension
181  outputRegion.SetSize(3,1);
182  unsigned int currentTimeStep = 0;
183 
184  // for each index in the iterator list, extract the image and insert it as a new time step
185  for(IndexListType::const_iterator indexIt = m_IndexList.begin();
186  indexIt != m_IndexList.end();
187  indexIt++)
188  {
189  // set the time step
190  outputRegion.SetIndex(3, currentTimeStep);
191  itk::ImageRegionIterator< OutputImageType> outputIt( outputImage.GetPointer(), outputRegion );
192 
193  // iterate over the current b0 image and store it to corresponding place
194  outputIt.GoToBegin();
195  inputIt.GoToBegin();
196  while( !outputIt.IsAtEnd() && !inputIt.IsAtEnd() )
197  {
198  // the input vector
199  typename InputImageType::PixelType vec = inputIt.Get();
200 
201  outputIt.Set( vec[*indexIt]);
202  ++outputIt;
203  ++inputIt;
204  }
205 
206  // increase time step
207  currentTimeStep++;
208  }
209 }
210 
211 
212 #endif // __itkSplitDWImageFilter_txx