5 #include <itkImageRegionIterator.h> 6 #include <itkImageRegionConstIterator.h> 14 template <
typename TInputImage,
typename TOutputImage>
16 T2EPGRelaxometryEstimationImageFilter <TInputImage,TOutputImage>
17 ::CheckComputationMask()
19 if (this->GetComputationMask())
22 typedef itk::ImageRegionConstIterator <InputImageType> IteratorType;
23 typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
25 std::vector <IteratorType> inItrs(this->GetNumberOfIndexedInputs());
26 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
27 inItrs[i] = IteratorType(this->GetInput(i),this->GetOutput()->GetLargestPossibleRegion());
29 typename MaskImageType::Pointer maskImage = MaskImageType::New();
30 maskImage->Initialize();
31 maskImage->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
32 maskImage->SetSpacing (this->GetInput(0)->GetSpacing());
33 maskImage->SetOrigin (this->GetInput(0)->GetOrigin());
34 maskImage->SetDirection (this->GetInput(0)->GetDirection());
35 maskImage->Allocate();
37 MaskIteratorType maskItr (maskImage,this->GetOutput()->GetLargestPossibleRegion());
38 while (!maskItr.IsAtEnd())
40 double averageVal = 0;
41 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
42 averageVal += inItrs[i].Get();
44 averageVal /= this->GetNumberOfIndexedInputs();
46 bool maskPoint = (averageVal <= m_AverageSignalThreshold);
53 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
59 this->SetComputationMask(maskImage);
62 template <
typename TInputImage,
typename TOutputImage>
65 ::BeforeThreadedGenerateData()
67 this->Superclass::BeforeThreadedGenerateData();
70 typename InitT2FilterType::Pointer initFilter = InitT2FilterType::New();
72 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
73 initFilter->SetInput(i,this->GetInput(i));
75 initFilter->SetT1Map(m_T1Map);
77 initFilter->SetEchoSpacing(m_EchoSpacing);
78 initFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
79 initFilter->SetComputationMask(this->GetComputationMask());
80 initFilter->SetTRValue(m_TRValue);
81 initFilter->SetT2UpperBoundValue(m_T2UpperBound);
82 initFilter->SetOptimizerStopCondition(m_OptimizerStopCondition);
83 initFilter->SetMaximumOptimizerIterations(m_MaximumOptimizerIterations);
87 m_InitialT2Image = initFilter->GetOutput();
88 m_InitialT2Image->DisconnectPipeline();
91 template <
typename TInputImage,
typename TOutputImage>
96 typedef itk::ImageRegionConstIterator <InputImageType> ImageIteratorType;
97 typedef itk::ImageRegionIterator <OutputImageType> OutImageIteratorType;
99 OutImageIteratorType initT2Iterator(m_InitialT2Image,outputRegionForThread);
100 OutImageIteratorType outT2Iterator(this->GetOutput(0),outputRegionForThread);
101 OutImageIteratorType outM0Iterator(this->GetOutput(1),outputRegionForThread);
102 OutImageIteratorType outB1Iterator(this->GetOutput(2),outputRegionForThread);
104 unsigned int numInputs = this->GetNumberOfIndexedInputs();
105 std::vector <ImageIteratorType> inIterators;
106 for (
unsigned int i = 0;i < numInputs;++i)
107 inIterators.push_back(ImageIteratorType(this->GetInput(i),outputRegionForThread));
109 typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
110 MaskIteratorType maskItr(this->GetComputationMask(),outputRegionForThread);
112 OutImageIteratorType t1MapItr;
114 t1MapItr = OutImageIteratorType(m_T1Map,outputRegionForThread);
116 std::vector <double> relaxoT2Data(numInputs,0);
121 typename CostFunctionType::Pointer cost = CostFunctionType::New();
122 cost->SetT2EchoSpacing(m_EchoSpacing);
123 cost->SetT2ExcitationFlipAngle(m_T2ExcitationFlipAngle);
124 cost->SetT2FlipAngles(m_T2FlipAngles);
126 unsigned int dimension = cost->GetNumberOfParameters();
127 itk::Array<double> lowerBounds(dimension);
128 itk::Array<double> upperBounds(dimension);
129 OptimizerType::ParametersType p(dimension);
131 while (!maskItr.IsAtEnd())
133 double t1Value = m_T2UpperBound;
137 t1Value = t1MapItr.Get();
142 double b1Value = 1.0;
143 double t2Value = initT2Iterator.Get();
145 if (maskItr.Get() == 0)
147 outT2Iterator.Set(0);
148 outM0Iterator.Set(0);
149 outB1Iterator.Set(0);
157 for (
unsigned int i = 0;i < numInputs;++i)
166 cost->SetT1Value(t1Value);
169 for (
unsigned int i = 0;i < numInputs;++i)
170 relaxoT2Data[i] = inIterators[i].Get();
172 cost->SetT2RelaxometrySignals(relaxoT2Data);
174 OptimizerType::Pointer optimizer = OptimizerType::New();
175 optimizer->SetAlgorithm(NLOPT_LN_BOBYQA);
176 optimizer->SetXTolRel(m_OptimizerStopCondition);
177 optimizer->SetFTolRel(1.0e-2 * m_OptimizerStopCondition);
178 optimizer->SetMaxEval(m_MaximumOptimizerIterations);
179 optimizer->SetVectorStorageSize(2000);
181 lowerBounds[0] = 1.0;
182 upperBounds[0] = m_T2UpperBound;
183 if (m_T1Map && (m_T2UpperBound > t1Value))
184 upperBounds[0] = t1Value;
186 lowerBounds[1] = 1.0;
187 upperBounds[1] = 2.0;
192 optimizer->SetLowerBoundParameters(lowerBounds);
193 optimizer->SetUpperBoundParameters(upperBounds);
194 optimizer->SetMaximize(
false);
196 optimizer->SetCostFunction(cost);
198 optimizer->SetInitialPosition(p);
199 optimizer->StartOptimization();
201 p = optimizer->GetCurrentPosition();
207 outT2Iterator.Set(t2Value);
208 outM0Iterator.Set(cost->GetM0Value());
209 outB1Iterator.Set(b1Value);
211 this->IncrementNumberOfProcessedPoints();
218 for (
unsigned int i = 0;i < numInputs;++i)
Implements an ITK wrapper for the NLOPT library.
Superclass::OutputImageRegionType OutputImageRegionType