ANIMA  4.0
animaT1SERelaxometryEstimationImageFilter.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionIterator.h>
5 #include <itkImageRegionConstIterator.h>
6 
7 #include <animaNLOPTOptimizers.h>
9 
10 namespace anima
11 {
12 
13 template <typename TInputImage, typename TOutputImage>
14 void
15 T1SERelaxometryEstimationImageFilter <TInputImage,TOutputImage>
16 ::CheckComputationMask()
17 {
18  if (this->GetComputationMask())
19  return;
20 
21  typedef itk::ImageRegionConstIterator <InputImageType> IteratorType;
22  typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
23 
24  std::vector <IteratorType> inItrs(this->GetNumberOfIndexedInputs());
25  for (unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
26  inItrs[i] = IteratorType(this->GetInput(i),this->GetOutput()->GetLargestPossibleRegion());
27 
28  typename MaskImageType::Pointer maskImage = MaskImageType::New();
29  maskImage->Initialize();
30  maskImage->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
31  maskImage->SetSpacing (this->GetInput(0)->GetSpacing());
32  maskImage->SetOrigin (this->GetInput(0)->GetOrigin());
33  maskImage->SetDirection (this->GetInput(0)->GetDirection());
34  maskImage->Allocate();
35 
36  MaskIteratorType maskItr (maskImage,this->GetOutput()->GetLargestPossibleRegion());
37  while (!maskItr.IsAtEnd())
38  {
39  double averageVal = 0;
40  for (unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
41  averageVal += inItrs[i].Get();
42 
43  averageVal /= this->GetNumberOfIndexedInputs();
44 
45  bool maskPoint = (averageVal <= m_AverageSignalThreshold);
46 
47  if (maskPoint)
48  maskItr.Set(0);
49  else
50  maskItr.Set(1);
51 
52  for (unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
53  ++inItrs[i];
54 
55  ++maskItr;
56  }
57 
58  this->SetComputationMask(maskImage);
59 }
60 
61 template <typename TInputImage, typename TOutputImage>
62 void
64 ::DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread)
65 {
66  typedef itk::ImageRegionConstIterator <InputImageType> ImageIteratorType;
67  typedef itk::ImageRegionIterator <OutputImageType> OutImageIteratorType;
68 
69  OutImageIteratorType outT1Iterator(this->GetOutput(1),outputRegionForThread);
70  OutImageIteratorType outM0Iterator(this->GetOutput(0),outputRegionForThread);
71 
72  unsigned int numInputs = this->GetNumberOfIndexedInputs();
73  std::vector <ImageIteratorType> inIterators;
74  for (unsigned int i = 0;i < numInputs;++i)
75  inIterators.push_back(ImageIteratorType(this->GetInput(i),outputRegionForThread));
76 
77  typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
78  MaskIteratorType maskItr(this->GetComputationMask(),outputRegionForThread);
79 
81  cost->SetTRValues(m_TRValues);
82 
83  std::vector <double> relaxoData(numInputs,0);
84  typedef anima::NLOPTOptimizers OptimizerType;
85 
86  unsigned int dimension = cost->GetNumberOfParameters();
87  itk::Array<double> lowerBounds(dimension);
88  itk::Array<double> upperBounds(dimension);
89  OptimizerType::ParametersType p(dimension);
90 
91  lowerBounds[0] = 1.0e-4;
92  upperBounds[0] = m_M0UpperBound;
93 
94  lowerBounds[1] = 1.0e-4;
95  upperBounds[1] = m_T1UpperBound;
96 
97  while (!maskItr.IsAtEnd())
98  {
99  if (maskItr.Get() == 0)
100  {
101  outT1Iterator.Set(0);
102  outM0Iterator.Set(0);
103 
104  ++maskItr;
105  ++outT1Iterator;
106  ++outM0Iterator;
107 
108  for (unsigned int i = 0;i < numInputs;++i)
109  ++inIterators[i];
110 
111  continue;
112  }
113 
114  for (unsigned int i = 0;i < numInputs;++i)
115  relaxoData[i] = inIterators[i].Get();
116 
117  cost->SetRelaxometrySignals(relaxoData);
118 
119  OptimizerType::Pointer optimizer = OptimizerType::New();
120  optimizer->SetAlgorithm(NLOPT_LN_BOBYQA);
121 
122  optimizer->SetMaxEval(m_MaximumOptimizerIterations);
123  optimizer->SetXTolRel(m_OptimizerStopCondition);
124 
125  optimizer->SetLowerBoundParameters(lowerBounds);
126  optimizer->SetUpperBoundParameters(upperBounds);
127 
128  p[0] = 1500;
129  p[1] = 1500;
130 
131  optimizer->SetMaximize(false);
132 
133  optimizer->SetCostFunction(cost);
134 
135  optimizer->SetInitialPosition(p);
136  optimizer->StartOptimization();
137 
138  p = optimizer->GetCurrentPosition();
139 
140  outM0Iterator.Set(p[0]);
141  outT1Iterator.Set(p[1]);
142 
143  ++maskItr;
144  ++outT1Iterator;
145  ++outM0Iterator;
146 
147  for (unsigned int i = 0;i < numInputs;++i)
148  ++inIterators[i];
149  }
150 }
151 
152 } // end of namespace anima
Implements an ITK wrapper for the NLOPT library.
Superclass::OutputImageRegionType OutputImageRegionType