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 ===================================================================*/
17 /*===================================================================
19 This file is based heavily on a corresponding ITK filter.
21 ===================================================================*/
22 #ifndef __itkB0ImageExtractionImageFilter_txx
23 #define __itkB0ImageExtractionImageFilter_txx
25 #include "itkB0ImageExtractionImageFilter.h"
27 #include "itkImageRegionIterator.h"
31 template< class TInputImagePixelType,
32 class TOutputImagePixelType >
33 B0ImageExtractionImageFilter< TInputImagePixelType,
34 TOutputImagePixelType >
35 ::B0ImageExtractionImageFilter()
37 // At least 1 inputs is necessary for a vector image.
38 // For images added one at a time we need at least six
39 this->SetNumberOfRequiredInputs( 1 );
42 template< class TInputImagePixelType,
43 class TOutputImagePixelType >
44 void B0ImageExtractionImageFilter< TInputImagePixelType,
45 TOutputImagePixelType >
49 typename GradientDirectionContainerType::Iterator begin = m_Directions->Begin();
50 typename GradientDirectionContainerType::Iterator end = m_Directions->End();
52 // Find the index of the b0 image
53 std::vector<int> indices;
57 GradientDirectionType grad = begin->Value();
59 if(grad[0] == 0 && grad[1] == 0 && grad[2] == 0)
61 indices.push_back(index);
67 typedef itk::Image<float,3> TempImageType;
68 TempImageType::Pointer tmp = TempImageType::New();
69 typename TempImageType::RegionType region = this->GetInput()->GetLargestPossibleRegion();
71 tmp->SetSpacing(this->GetInput()->GetSpacing());
72 tmp->SetOrigin(this->GetInput()->GetOrigin());
73 tmp->SetDirection(this->GetInput()->GetDirection());
74 tmp->SetRegions(region);
77 itk::ImageRegionIterator<TempImageType> it(tmp.GetPointer(), tmp->GetLargestPossibleRegion() );
78 itk::ImageRegionConstIterator<InputImageType> vectorIt(this->GetInput(), this->GetInput()->GetLargestPossibleRegion() );
87 //Sum all images that have zero diffusion weighting (indices stored in vector index)
88 for(std::vector<int>::iterator indexIt = indices.begin();
89 indexIt != indices.end();
95 while(!it.IsAtEnd() && !vectorIt.IsAtEnd())
97 typename InputImageType::PixelType vec = vectorIt.Get();
98 it.Set((1.0 * it.Get()) + (1.0 * vec[*indexIt]) / (1.0 * indices.size()));
104 typename OutputImageType::Pointer b0Image =
105 static_cast< OutputImageType * >(this->ProcessObject::GetPrimaryOutput());
106 typename OutputImageType::RegionType outregion = this->GetInput()->GetLargestPossibleRegion();
107 b0Image->SetSpacing(this->GetInput()->GetSpacing());
108 b0Image->SetOrigin(this->GetInput()->GetOrigin());
109 b0Image->SetDirection(this->GetInput()->GetDirection());
110 b0Image->SetRegions(outregion);
112 itk::ImageRegionIterator<TempImageType> itIn(tmp, tmp->GetLargestPossibleRegion() );
113 itk::ImageRegionIterator<OutputImageType> itOut(b0Image, b0Image->GetLargestPossibleRegion() );
116 while(!itIn.IsAtEnd())
118 itOut.Set(itIn.Get());
126 #endif // __itkB0ImageExtractionImageFilter_txx