4 #include <itkImageRegionIterator.h> 5 #include <itkImageRegionConstIterator.h> 13 template <
class TPixelScalarType>
15 GammaMixtureT2RelaxometryEstimationImageFilter <TPixelScalarType>
16 ::BeforeThreadedGenerateData()
18 Superclass::BeforeThreadedGenerateData();
20 this->GetOutput(2)->FillBuffer(1.0);
22 m_WeightsImage = VectorOutputImageType::New();
23 m_WeightsImage->Initialize();
24 m_WeightsImage->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
25 m_WeightsImage->SetSpacing (this->GetInput(0)->GetSpacing());
26 m_WeightsImage->SetOrigin (this->GetInput(0)->GetOrigin());
27 m_WeightsImage->SetDirection (this->GetInput(0)->GetDirection());
28 m_WeightsImage->SetVectorLength(3);
29 m_WeightsImage->Allocate();
33 m_WeightsImage->FillBuffer(zero);
35 m_MeanParamImage = VectorOutputImageType::New();
36 m_MeanParamImage->Initialize();
37 m_MeanParamImage->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
38 m_MeanParamImage->SetSpacing (this->GetInput(0)->GetSpacing());
39 m_MeanParamImage->SetOrigin (this->GetInput(0)->GetOrigin());
40 m_MeanParamImage->SetDirection (this->GetInput(0)->GetDirection());
41 m_MeanParamImage->SetVectorLength(3);
42 m_MeanParamImage->Allocate();
44 m_MeanParamImage->FillBuffer(zero);
47 template <
class TPixelScalarType>
50 ::CheckComputationMask()
52 if (this->GetComputationMask())
55 typedef itk::ImageRegionConstIterator <InputImageType> IteratorType;
56 typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
58 std::vector <IteratorType> inItrs(this->GetNumberOfIndexedInputs());
59 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
60 inItrs[i] = IteratorType(this->GetInput(i),this->GetOutput(0)->GetLargestPossibleRegion());
62 typename MaskImageType::Pointer maskImage = MaskImageType::New();
63 maskImage->Initialize();
64 maskImage->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
65 maskImage->SetSpacing (this->GetInput(0)->GetSpacing());
66 maskImage->SetOrigin (this->GetInput(0)->GetOrigin());
67 maskImage->SetDirection (this->GetInput(0)->GetDirection());
68 maskImage->Allocate();
70 MaskIteratorType maskItr (maskImage,this->GetOutput(0)->GetLargestPossibleRegion());
71 while (!maskItr.IsAtEnd())
73 double averageVal = 0;
74 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
75 averageVal += inItrs[i].Get();
77 averageVal /= this->GetNumberOfIndexedInputs();
79 bool maskPoint = (averageVal <= m_AverageSignalThreshold);
86 for (
unsigned int i = 0;i < this->GetNumberOfIndexedInputs();++i)
92 this->SetComputationMask(maskImage);
95 template <
class TPixelScalarType>
100 typedef itk::ImageRegionConstIterator <InputImageType> ImageConstIteratorType;
101 typedef itk::ImageRegionIterator <InputImageType> ImageIteratorType;
102 typedef itk::ImageRegionIterator <VectorOutputImageType> VectorImageIteratorType;
104 VectorImageIteratorType outWeightsIterator(this->GetWeightsImage(),outputRegionForThread);
105 VectorImageIteratorType outMeanParamsIterator(this->GetMeanParamImage(),outputRegionForThread);
107 ImageIteratorType outM0Iterator(this->GetM0OutputImage(),outputRegionForThread);
108 ImageIteratorType outMWFIterator(this->GetMWFOutputImage(),outputRegionForThread);
109 ImageIteratorType outB1Iterator(this->GetB1OutputImage(),outputRegionForThread);
110 ImageIteratorType outSigmaSqIterator(this->GetSigmaSquareOutputImage(),outputRegionForThread);
112 unsigned int numInputs = this->GetNumberOfIndexedInputs();
113 std::vector <ImageConstIteratorType> inIterators;
114 for (
unsigned int i = 0;i < numInputs;++i)
115 inIterators.push_back(ImageConstIteratorType(this->GetInput(i),outputRegionForThread));
117 typedef itk::ImageRegionIterator <MaskImageType> MaskIteratorType;
118 MaskIteratorType maskItr(this->GetComputationMask(),outputRegionForThread);
120 ImageIteratorType t1MapItr;
122 t1MapItr = ImageIteratorType(m_T1Map,outputRegionForThread);
124 std::vector <double> VarParamsFixed(3, m_ShortT2Var);
125 VarParamsFixed[1] = m_MediumT2Var;
126 VarParamsFixed[2] = m_HighT2Var;
128 std::vector <double> MeanParamsInit(3, m_ShortT2Mean);
129 MeanParamsInit[1] = 110.0;
130 MeanParamsInit[2] = m_HighT2Mean;
135 unsigned int n_compartments = 3;
136 OptimizerType::ParametersType signalValues(numInputs);
137 OptimizerType::ParametersType t2OptimizedWeights(n_compartments);
142 typename CostFunctionType::Pointer cost = CostFunctionType::New();
143 cost->SetEchoSpacing(m_EchoSpacing);
144 cost->SetExcitationFlipAngle(m_T2ExcitationFlipAngle);
145 cost->SetGammaMeans(MeanParamsInit);
146 cost->SetGammaVariances(VarParamsFixed);
147 cost->SetConstrainedParameters(m_ConstrainedParameters);
148 cost->SetGammaIntegralTolerance(m_GammaIntegralTolerance);
150 unsigned int dimension = cost->GetNumberOfParameters();
152 itk::Array<double> lowerBounds(dimension);
153 itk::Array<double> upperBounds(dimension);
154 OptimizerType::ParametersType p(dimension);
155 lowerBounds[0] = 1.0 * m_T2FlipAngles[0];
156 upperBounds[0] = 2.0 * m_T2FlipAngles[0];
159 if (!m_ConstrainedParameters)
161 lowerBounds[1] = m_LowerShortT2;
162 lowerBounds[2] = m_LowerMediumT2;
163 lowerBounds[3] = m_LowerHighT2;
165 upperBounds[1] = m_UpperShortT2;
166 upperBounds[2] = m_UpperMediumT2;
167 upperBounds[3] = m_UpperHighT2;
171 lowerBounds[1] = m_LowerMediumT2;
172 upperBounds[1] = m_UpperMediumT2;
175 while (!maskItr.IsAtEnd())
177 outputT2Weights.Fill(0);
178 outputMeanParams.Fill(0);
180 if (maskItr.Get() == 0)
182 outWeightsIterator.Set(outputT2Weights);
183 outMeanParamsIterator.Set(outputMeanParams);
184 outM0Iterator.Set(0);
185 outMWFIterator.Set(0.0);
186 outB1Iterator.Set(1.0);
187 outSigmaSqIterator.Set(0.0);
190 ++outWeightsIterator;
191 ++outMeanParamsIterator;
195 ++outSigmaSqIterator;
197 for (
unsigned int i = 0;i < numInputs;++i)
206 double t1Value = 1000.0;
207 double m0Value = 0.0;
209 for (
unsigned int i = 0;i < numInputs;++i)
210 signalValues[i] = inIterators[i].Get();
214 t1Value = t1MapItr.Get();
219 cost->SetT1Value(t1Value);
220 cost->SetT2RelaxometrySignals(signalValues);
222 OptimizerType::Pointer opt = OptimizerType::New();
223 opt->SetAlgorithm(NLOPT_LD_CCSAQ);
224 opt->SetXTolRel(1.0e-5);
225 opt->SetFTolRel(1.0e-7);
226 opt->SetMaxEval(500);
227 opt->SetVectorStorageSize(2000);
229 opt->SetLowerBoundParameters(lowerBounds);
230 opt->SetUpperBoundParameters(upperBounds);
232 p[0] = 1.2 * m_T2FlipAngles[0];
233 if (!m_ConstrainedParameters)
243 opt->SetInitialPosition(p);
244 opt->SetMaximize(
false);
245 opt->SetCostFunction(cost);
247 opt->StartOptimization();
248 p = opt->GetCurrentPosition();
250 t2OptimizedWeights = cost->GetOptimalT2Weights();
254 for (
unsigned int i = 0;i < n_compartments;++i)
255 m0Value += t2OptimizedWeights[i];
257 for (
unsigned int i = 0;i < n_compartments;++i)
260 outputT2Weights[i] = t2OptimizedWeights[i] / m0Value;
262 outputT2Weights[i] = 0.0;
265 outM0Iterator.Set(m0Value);
266 outWeightsIterator.Set(outputT2Weights);
268 if (!m_ConstrainedParameters)
270 for (
unsigned int i = 0;i < 3;++i)
271 outputMeanParams[i] = p[i + 1];
275 outputMeanParams[0] = m_ShortT2Mean;
276 outputMeanParams[1] = p[1];
277 outputMeanParams[2] = m_HighT2Mean;
280 outMeanParamsIterator.Set(outputMeanParams);
282 double mwfValue = outputT2Weights[0];
284 outMWFIterator.Set(mwfValue);
286 double b1Value = p[0] / m_T2FlipAngles[0];
287 outB1Iterator.Set(b1Value);
289 outSigmaSqIterator.Set(cost->GetSigmaSquare());
291 this->IncrementNumberOfProcessedPoints();
293 ++outWeightsIterator;
294 ++outMeanParamsIterator;
298 ++outSigmaSqIterator;
300 for (
unsigned int i = 0;i < numInputs;++i)
Superclass::OutputImageRegionType OutputImageRegionType
Implements an ITK wrapper for the NLOPT library.
VectorOutputImageType::PixelType OutputVectorType