1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
16 #ifndef __itkSplitDWImageFilter_txx
17 #define __itkSplitDWImageFilter_txx
19 #include "itkSplitDWImageFilter.h"
21 #include <itkImageRegionConstIterator.h>
22 #include <itkImageRegionIterator.h>
24 template< class TInputImagePixelType,
25 class TOutputImagePixelType>
26 itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
27 ::SplitDWImageFilter()
29 m_ExtractAllImages(true)
31 this->SetNumberOfRequiredInputs(1);
34 template< class TInputImagePixelType,
35 class TOutputImagePixelType>
36 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
37 ::CopyInformation( const DataObject* /*data*/)
42 template< class TInputImagePixelType,
43 class TOutputImagePixelType>
44 void itk::SplitDWImageFilter<
45 TInputImagePixelType, TOutputImagePixelType >::GenerateOutputInformation()
50 template< class TInputImagePixelType,
51 class TOutputImagePixelType>
52 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
53 ::SetExtractAllAboveThreshold(double b_threshold, BValueMapType map)
55 m_ExtractAllImages = false;
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() )
64 if (bvalueIt->first > b_threshold)
66 // the map contains an index container, this needs to be inserted into the
68 IndexListType::const_iterator listIt = bvalueIt->second.begin();
69 while( listIt != bvalueIt->second.end() )
71 m_IndexList.push_back( *listIt );
80 template< class TInputImagePixelType,
81 class TOutputImagePixelType>
82 void itk::SplitDWImageFilter< TInputImagePixelType, TOutputImagePixelType >
83 ::SetExtractSingleShell(double b_value, BValueMapType map, double tol)
85 m_ExtractAllImages = false;
89 BValueMapType::const_iterator bvalueIt = map.begin();
90 while( bvalueIt != map.end() )
92 IndexListType::const_iterator listIt = bvalueIt->second.begin();
93 if( std::fabs( bvalueIt->first - b_value) < tol)
95 m_IndexList.insert( m_IndexList.begin(), bvalueIt->second.begin(), bvalueIt->second.end() );
103 template< class TInputImagePixelType,
104 class TOutputImagePixelType>
105 void itk::SplitDWImageFilter<
106 TInputImagePixelType, TOutputImagePixelType >::GenerateData()
109 if( m_IndexList.empty() && !m_ExtractAllImages )
111 itkExceptionMacro(<<"The index list is empty and the option to extract all images is disabled. ");
114 // construct the index list (for each component)
115 if( m_ExtractAllImages )
119 for( unsigned int i=0; i< this->GetInput()->GetNumberOfComponentsPerPixel(); i++)
120 m_IndexList.push_back(i);
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();
128 // allocate image with
129 // - dimension: [DimX, DimY, DimZ, size(IndexList) ]
130 // - spacing: old one, 1.0 time
132 typename OutputImageType::SpacingType spacing;
135 typename OutputImageType::PointType origin;
138 OutputImageRegionType outputRegion;
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++)
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]);
150 // number of timesteps = number of b0 images
151 outputRegion.SetSize(3, m_IndexList.size());
152 outputRegion.SetIndex( 3, 0 );
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();
161 for( unsigned int i=0; i< 3; i++)
163 outputDirection(0,i) = inputDirection(0,i);
164 outputDirection(1,i) = inputDirection(1,i);
165 outputDirection(2,i) = inputDirection(2,i);
168 outputImage->SetSpacing( spacing );
169 outputImage->SetOrigin( origin );
170 outputImage->SetDirection( outputDirection );
171 outputImage->SetRegions( outputRegion );
172 outputImage->Allocate();
175 itk::ImageRegionConstIterator<InputImageType> inputIt( this->GetInput(),
176 this->GetInput()->GetLargestPossibleRegion() );
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;
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();
190 outputRegion.SetIndex(3, currentTimeStep);
191 itk::ImageRegionIterator< OutputImageType> outputIt( outputImage.GetPointer(), outputRegion );
193 // iterate over the current b0 image and store it to corresponding place
194 outputIt.GoToBegin();
196 while( !outputIt.IsAtEnd() && !inputIt.IsAtEnd() )
199 typename InputImageType::PixelType vec = inputIt.Get();
201 outputIt.Set( vec[*indexIt]);
206 // increase time step
212 #endif // __itkSplitDWImageFilter_txx