8 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
11 this->SetNthInput(0, const_cast<TMaskImage*>(mask));
14 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
17 this->SetNthInput(1, const_cast<TInputImage*>(image));
20 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
23 this->SetNthInput(2, const_cast<TInputImage*>(image));
26 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
29 this->SetNthInput(3, const_cast<TInputImage*>(image));
32 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
35 this->SetNthInput(4, const_cast<TAtlasImage*>(atlas));
38 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
41 this->SetNthInput(5, const_cast<TAtlasImage*>(atlas));
44 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
47 this->SetNthInput(6, const_cast<TAtlasImage*>(atlas));
52 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
56 ( this->itk::ProcessObject::GetInput(0) );
59 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
62 return static_cast< const TInputImage *
> 63 ( this->itk::ProcessObject::GetInput(1) );
66 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
69 return static_cast< const TInputImage *
> 70 ( this->itk::ProcessObject::GetInput(2) );
73 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
76 return static_cast< const TInputImage *
> 77 ( this->itk::ProcessObject::GetInput(3) );
80 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
83 return static_cast< const TAtlasImage *
> 84 ( this->itk::ProcessObject::GetInput(4) );
87 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
90 return static_cast< const TAtlasImage *
> 91 ( this->itk::ProcessObject::GetInput(5) );
94 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
97 return static_cast< const TAtlasImage *
> 98 ( this->itk::ProcessObject::GetInput(6) );
102 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
106 std::vector<GaussianFunctionType::Pointer> tmpGaussianModel;
107 std::vector<double> tmpAlphas;
109 typedef std::map<double,unsigned int> OrderType;
111 OrderType::iterator it;
114 for(
unsigned int i = 0; i < m_GaussianModel.size(); i++)
116 GaussianFunctionType::MeanVectorType mean = (m_GaussianModel[i])->GetMean();
117 ordered.insert(OrderType::value_type(mean[0],i));
120 for(it = ordered.begin(); it != ordered.end(); ++it)
122 tmpGaussianModel.push_back(m_GaussianModel[it->second]);
123 tmpAlphas.push_back(m_Alphas[it->second]);
125 m_GaussianModel = tmpGaussianModel;
126 m_Alphas = tmpAlphas;
129 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
134 if( (this->GetMask().IsNull()) || (this->GetInputImage1().IsNull()) || (this->GetInputImage2().IsNull()) || (this->GetInputImage3().IsNull()) )
136 std::cerr <<
"Error: Inputs are missing... Exiting..." << std::endl;
140 if(m_UseT2 && m_UseDP && m_UseFLAIR)
142 std::cerr <<
"-- Error in Automatic segmentation: only 2 images among T2, DP and FLAIR must be used for automatic segmentation" << std::endl;
146 if(m_InitMethodType==0)
148 if((this->GetInputCSFAtlas().IsNull()) || (this->GetInputGMAtlas().IsNull()) || (this->GetInputWMAtlas().IsNull()))
150 std::cerr <<
"Error: Some atlas images are missing for the initialization... Exiting..." << std::endl;
155 if(m_InitMethodType==1)
157 if((m_UseT2==
false) || (m_UseDP==
false))
159 std::cerr <<
"Error: Automatic segmentation with Hierarchical DP initialisation requires T2 and DP images... Exiting..." << std::endl;
164 if(m_InitMethodType==2)
166 if(m_UseFLAIR==
false)
168 std::cerr <<
"Error: Automatic segmentation with Hierarchical FLAIR initialisation requires FLAIR image... Exiting..." << std::endl;
175 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
180 m_InputImage_T1_UC = ImageTypeUC::New();
181 m_InputImage_T2_DP_UC = ImageTypeUC::New();
182 m_InputImage_DP_FLAIR_UC = ImageTypeUC::New();
184 double desiredMinimum=0,desiredMaximum=255;
186 typename RescaleFilterType::Pointer rescaleFilter1 = RescaleFilterType::New();
187 rescaleFilter1->SetInput( this->GetInputImage1() );
188 rescaleFilter1->SetOutputMinimum( desiredMinimum );
189 rescaleFilter1->SetOutputMaximum( desiredMaximum );
190 rescaleFilter1->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
191 rescaleFilter1->UpdateLargestPossibleRegion();
192 m_InputImage_T1_UC = rescaleFilter1->GetOutput();
194 typename RescaleFilterType::Pointer rescaleFilter2 = RescaleFilterType::New();
195 rescaleFilter2->SetInput( this->GetInputImage2() );
196 rescaleFilter2->SetOutputMinimum( desiredMinimum );
197 rescaleFilter2->SetOutputMaximum( desiredMaximum );
198 rescaleFilter2->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
199 rescaleFilter2->UpdateLargestPossibleRegion();
200 m_InputImage_T2_DP_UC = rescaleFilter2->GetOutput();
202 typename RescaleFilterType::Pointer rescaleFilter3 = RescaleFilterType::New();
203 rescaleFilter3->SetInput( this->GetInputImage3() );
204 rescaleFilter3->SetOutputMinimum( desiredMinimum );
205 rescaleFilter3->SetOutputMaximum( desiredMaximum );
206 rescaleFilter3->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
207 rescaleFilter3->UpdateLargestPossibleRegion();
208 m_InputImage_DP_FLAIR_UC = rescaleFilter3->GetOutput();
212 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
215 ::WriteSolution(std::string filename)
217 const unsigned int ARows = 1;
218 const unsigned int ACols = 39;
220 typedef itk::Array2D<double> MatrixType;
221 MatrixType matrix(ARows,ACols);
224 for(
unsigned int i = 0; i < m_NbTissus; i++)
226 matrix[0][t] = m_Alphas[i];
229 GaussianFunctionType::MeanVectorType mu = (m_GaussianModel[i])->GetMean();
230 for(
unsigned int j = 0; j < m_NbModalities; j++)
232 matrix[0][t] = mu[j];
236 GaussianFunctionType::CovarianceMatrixType covar = (m_GaussianModel[i])->GetCovariance();
237 for(
unsigned int k = 0; k < m_NbModalities; k++)
239 for(
unsigned int j = 0; j < m_NbModalities; j++)
241 matrix[0][t] = covar(k,j);
248 typedef itk::CSVNumericObjectFileWriter<double, ARows, ACols> WriterType;
249 WriterType::Pointer writer = WriterType::New();
251 writer->SetFieldDelimiterCharacter(
';');
252 writer->SetFileName( filename );
253 writer->SetInput( &matrix );
258 catch (itk::ExceptionObject& exp)
260 std::cerr <<
"Exception caught!" << std::endl;
261 std::cerr << exp << std::endl;
269 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
272 ::ReadSolution(std::string filename)
274 typedef itk::Array2D<double> MatrixType;
275 typedef itk::CSVArray2DFileReader<double > ReaderType;
276 ReaderType::Pointer reader = ReaderType::New();
277 reader->SetFileName( filename );
278 reader->SetFieldDelimiterCharacter(
';' );
279 reader->SetStringDelimiterCharacter(
'"' );
280 reader->HasColumnHeadersOff();
281 reader->HasRowHeadersOff();
288 catch (itk::ExceptionObject& exp)
290 std::cerr <<
"Exception caught!" << std::endl;
291 std::cerr << exp << std::endl;
295 typedef itk::CSVArray2DDataObject<double> DataFrameObjectType;
296 DataFrameObjectType::Pointer dfo = reader->GetOutput();
297 MatrixType matrix = dfo->GetMatrix();
299 unsigned int nbCols = m_NbTissus*(1+m_NbModalities+m_NbModalities*m_NbModalities);
300 if(matrix.rows()!=1 || matrix.cols()!=nbCols)
302 std::cout<<
"wrong type of matrix file... cannot read solution" << std::endl;
307 for(
unsigned int i = 0; i < m_NbTissus; i++)
309 m_Alphas.push_back(matrix[0][t]);
312 GaussianFunctionType::Pointer densityFunction = GaussianFunctionType::New();
313 densityFunction->SetMeasurementVectorSize( m_NbTissus );
314 GaussianFunctionType::MeanVectorType mean( m_NbModalities );
315 GaussianFunctionType::CovarianceMatrixType cov;
316 cov.SetSize( m_NbModalities, m_NbModalities );
319 for(
unsigned int j = 0; j < m_NbModalities; j++)
321 mean[j] = matrix[0][t];
325 for(
unsigned int k = 0; k < m_NbModalities; k++)
327 for(
unsigned int j = 0; j < m_NbModalities; j++)
329 cov[k][j] = matrix[0][t];
334 densityFunction->SetMean( mean );
335 densityFunction->SetCovariance( cov );
336 m_GaussianModel.push_back( densityFunction );
342 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
345 ::PrintSolution(std::vector<double> alphas, std::vector<GaussianFunctionType::Pointer> model)
347 unsigned int nbTissus = alphas.size();
348 unsigned int nbModalities = (model[0])->GetMean().Size();
349 for(
unsigned int i = 0; i < nbTissus; i++)
351 std::cout <<
"* Class: " << i << std::endl;
352 std::cout <<
" Alpha: " << alphas[i] << std::endl;
353 GaussianFunctionType::MeanVectorType mu = (model[i])->GetMean();
354 std::cout <<
" Mean: " << std::endl;
355 for(
unsigned int j = 0; j < nbModalities; j++)
357 std::cout <<
" " << mu[j] << std::endl;
360 GaussianFunctionType::CovarianceMatrixType covar = (model[i])->GetCovariance();
361 std::cout <<
" covar: " << std::endl;
362 for(
unsigned int k = 0; k < nbModalities; k++)
364 std::cout <<
" " << covar(k,0) <<
" " << covar(k,1) <<
" " << covar(k,2) << std::endl;
371 template <
typename TInputImage,
typename TMaskImage,
typename TAtlasImage>
377 this->RescaleImages();
379 if((m_GaussianModel.size() == m_NbTissus) && (m_Alphas.size() == m_NbTissus))
381 m_SolutionSet =
true;
382 std::cout <<
"Solution already set..."<< std::endl;
385 this->PrintSolution(m_Alphas, m_GaussianModel);
390 if(m_SolutionReadFilename!=
"")
392 m_SolutionSet =
true;
393 std::cout <<
"Reading solution file..." << std::endl;
394 if(ReadSolution(m_SolutionReadFilename))
396 m_SolutionSet =
false;
400 this->PrintSolution(m_Alphas, m_GaussianModel);
409 switch (m_InitMethodType)
413 initializer = AtlasInitializerType::New();
415 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetInputImage1( m_InputImage_T1_UC );
416 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetInputImage2( m_InputImage_T2_DP_UC );
417 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetInputImage3( m_InputImage_DP_FLAIR_UC );
418 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetAtlasImage1( this->GetInputCSFAtlas() );
419 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetAtlasImage2( this->GetInputGMAtlas() );
420 dynamic_cast<AtlasInitializerType *
>( initializer.GetPointer() ) ->SetAtlasImage3( this->GetInputWMAtlas() );
421 std::cout<<
"Choosen initializer: Atlas" << std::endl;
426 bool use_HierarFLAIR =
false;
427 initializer = HierarchicalType::New();
428 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetMask( this->GetMask() );
429 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage1( m_InputImage_T1_UC );
430 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage2( m_InputImage_T2_DP_UC );
431 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage3( m_InputImage_DP_FLAIR_UC );
432 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetThirdIsFLAIR( use_HierarFLAIR );
433 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetRobust( m_RejRatioHierar );
434 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetTol( m_Tol );
435 std::cout<<
"Choosen initializer: Hierarchical DP " << std::endl;
440 bool use_HierarFLAIR =
true;
441 initializer = HierarchicalType::New();
442 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetMask( this->GetMask() );
443 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage1( m_InputImage_T1_UC );
444 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage2( m_InputImage_T2_DP_UC );
445 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetInputImage3( m_InputImage_DP_FLAIR_UC );
446 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetThirdIsFLAIR( use_HierarFLAIR );
447 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetRobust( m_RejRatioHierar );
448 dynamic_cast<HierarchicalType *
>( initializer.GetPointer() ) ->SetTol( m_Tol );
449 std::cout<<
"Choosen initializer: Hierarchical FLAIR" << std::endl;
454 std::cerr<<
"-- Error in compute solution filter: initialisation failed" << std::endl;
459 std::cout <<
"Computing initialization for EM..." << std::endl;
460 initializer->Update();
461 std::vector<GaussianFunctionType::Pointer> initia = initializer->GetInitialization();
462 std::vector<double> initiaAlphas = initializer->GetAlphas();
464 std::cout <<
"Computing gaussian model..." << std::endl;
466 estimator ->SetMaxIterationsConc( m_EmIter_concentration );
467 estimator ->SetStremMode( m_EM_before_concentration );
468 estimator ->SetRejectionRatio( m_RejRatio );
469 estimator ->SetMaxIterations( m_EmIter );
470 estimator ->SetModelMinDistance( m_MinDistance );
471 estimator ->SetMask( this->GetMask() );
472 estimator ->SetInputImage1( m_InputImage_T1_UC );
473 estimator ->SetInputImage2( m_InputImage_T2_DP_UC );
474 estimator ->SetInputImage3( m_InputImage_DP_FLAIR_UC );
475 estimator ->SetVerbose( m_Verbose );
477 itk::CStyleCommand::Pointer callback = itk::CStyleCommand::New();
479 estimator ->AddObserver(itk::ProgressEvent(), callback );
480 estimator ->SetInitialGaussianModel(initia);
481 estimator ->SetInitialAlphas(initiaAlphas);
482 estimator ->Update();
484 m_GaussianModel = estimator->GetGaussianModel();
485 m_Alphas = estimator->GetAlphas();
487 this->SortGaussianModel();
491 std::cout << std::endl;
492 std::cout<<
"EM summary: " << std::endl << std::endl;
493 std::cout<<
"* Initial model: " << std::endl;
494 PrintSolution(initiaAlphas, initia);
495 std::cout << std::endl;
496 std::cout<<
"* Final model: " << std::endl;
497 this->PrintSolution(m_Alphas, m_GaussianModel);
498 std::cout << std::endl;
503 if(m_SolutionWriteFilename!=
"")
505 std::cout <<
"Writing solution in csv file..." << std::endl;
506 WriteSolution(m_SolutionWriteFilename);
Class computing the 3-class GMM respresenting the NABT, where each Gaussian represents one of the bra...
itk::SmartPointer< Self > Pointer
Class initializing a gaussian mixture with hierarchical information It uses 'a priori' knowledge of t...
itk::Image< PixelTypeUC, 3 > ImageTypeUC
void eventCallback(itk::Object *caller, const itk::EventObject &event, void *clientData)
itk::SmartPointer< Self > Pointer