6 template <
typename TInputImage, 
typename TMaskImage>
     9     if( this->m_RandomInitializer.IsNull() )
    11         std::cerr <<
" -- Error in ClassificationStrategy: RandomInitializer is null"<<std::endl;
    15     m_ListGaussianModels.clear();
    18     m_ListGaussianModels.resize(this->m_NumberOfEstimators.size());
    19     m_ListAlphas.resize(this->m_NumberOfEstimators.size());
    22     unsigned int errors = 0;
    24     for(
unsigned int iter = 0; iter < this->m_NumberOfEstimators[0]; iter++)
    26         this->m_RandomInitializer->Update();
    28         std::vector<GaussianFunctionType::Pointer> model = this->m_RandomInitializer->GetInitialization();
    29         std::vector<double> alphas = this->m_RandomInitializer->GetAlphas();
    31         this->m_Estimator->SetInitialGaussianModel(model);
    32         this->m_Estimator->SetInitialAlphas(alphas);
    33         this->m_Estimator->Update();
    34         double likelihood = m_Estimator->GetLikelihood();
    36         if( (!m_EM_Mode && likelihood > 0.0) || (m_EM_Mode && likelihood < 0.0) )
    38             std::vector<GaussianFunctionType::Pointer> result = this->m_Estimator->GetGaussianModel();
    39             std::vector<double> resultAlpha = this->m_Estimator->GetAlphas();
    43                 likelihood=-likelihood; 
    46             std::map< double, std::vector<GaussianFunctionType::Pointer> >::iterator mapIt = this->m_ListGaussianModels[0].begin();
    47             bool foundSame = 
false;
    49             for(mapIt = this->m_ListGaussianModels[0].begin(); mapIt != this->m_ListGaussianModels[0].end(); ++mapIt)
    51                 if(sameModel(mapIt->second, result))
    61                 this->m_ListGaussianModels[0].insert(std::map<
double, std::vector<GaussianFunctionType::Pointer> >::value_type(likelihood,result));
    62                 this->m_ListAlphas[0].insert(std::map<
double,std::vector<double> >::value_type(likelihood,resultAlpha));
    70             if(errors > 10 * this->m_NumberOfEstimators[0])
    75         double ratio = 
static_cast<double>(iter+1) / static_cast<double>(this->m_NumberOfEstimators[0]);
    76         this->UpdateProgress(ratio);
    81     for(
unsigned int step = 1; step < this->m_NumberOfEstimators.size(); step++)
    83         if(this->m_ListGaussianModels[step-1].size() == 0)
    85             std::cout<<
"-- ERROR in ClassificationStrategy: No solution found in step " << step-1 <<std::endl;
    89         std::map<double,std::vector<GaussianFunctionType::Pointer> >::iterator mapIt = this->m_ListGaussianModels[step-1].begin();
    90         for(
unsigned int iter = 0 ; mapIt != this->m_ListGaussianModels[step-1].end() && iter< this->m_NumberOfEstimators[step]; iter++, ++mapIt)
    92             this->m_Estimator->SetInitialGaussianModel(mapIt->second);
    94             m_Estimator->Update();
    95             double likelihood = m_Estimator->GetLikelihood();
    97             if((m_EM_Mode && likelihood < 0.0) || (!m_EM_Mode && likelihood > 0.0))
    99                 std::vector<GaussianFunctionType::Pointer> result = m_Estimator->GetGaussianModel();
   100                 std::vector<double> resultAlpha = m_Estimator->GetAlphas();
   102                 mapIt = this->m_ListGaussianModels[step].begin();
   103                 bool foundSame = 
false;
   104                 for(mapIt = this->m_ListGaussianModels[step].begin(); mapIt != this->m_ListGaussianModels[step].end(); ++mapIt)
   106                     if(sameModel(mapIt->second, result))
   114                     likelihood=-likelihood; 
   117                     this->m_ListGaussianModels[step].insert(std::map<
double,std::vector<GaussianFunctionType::Pointer> >::value_type(likelihood,result));
   118                     this->m_ListAlphas[step].insert(std::map<
double,std::vector<double> >::value_type(likelihood,resultAlpha));
   125                 if(errors > 10 * this->m_NumberOfEstimators[step])
   132 template <
typename TInputImage, 
typename TMaskImage>
   134                                                                     std::map< 
double, std::vector<double> > &solutionAlpha, 
int step)
   136     if(this->m_ListGaussianModels.size() == 0 || 
static_cast<int>(this->m_ListGaussianModels.size()) <= step)
   141         step=this->m_ListGaussianModels.size()-1;
   144     if(this->m_ListGaussianModels[step].size() == 0 )
   147     solution = this->m_ListGaussianModels[step];
   148     solutionAlpha = this->m_ListAlphas[step];
   153 template <
typename TInputImage, 
typename TMaskImage>
   156     this->m_NumberOfEstimators=ems;
   157     this->m_NumberOfIterations=iters;
   159     if(this->m_NumberOfEstimators.size()!= this->m_NumberOfIterations.size())
   161         while( this->m_NumberOfEstimators.size() > this->m_NumberOfIterations.size() )
   163             this->m_NumberOfEstimators.pop_back();
   165         while( this->m_NumberOfEstimators.size() < this->m_NumberOfIterations.size() )
   167             this->m_NumberOfIterations.pop_back();
   173 template <
typename TInputImage, 
typename TMaskImage>
   176     unsigned int NbClasses = mod1.size();
   177     unsigned int NbDimension = (mod1[0])->GetMean().Size();
   179     std::vector<int> comparisonVector(NbClasses,-1);
   180     unsigned int params = NbDimension + NbDimension*NbDimension;
   182     double *p1 = 
new double[params];
   183     double *p2 = 
new double[params];
   185     for(
unsigned int i = 0; i < NbClasses; i++)
   187         for(
unsigned int j = 0; j < NbClasses; j++)
   189             if((mod1[i])->GetMean().Size() != (mod2[j])->GetMean().Size())
   191                 comparisonVector[i] = -1;
   196             for(
unsigned int l = 0; l < NbDimension; l++)
   198                 p1[t] = 
static_cast<double>((mod1[i])->GetMean()[l]);
   199                 p2[t] = 
static_cast<double>((mod2[j])->GetMean()[l]);
   203             for(
unsigned int l = 0; l < NbDimension; l++)
   205                 for(
unsigned int k = l; k < NbDimension; k++)
   207                     p1[t] = 
static_cast<double>((mod1[i])->GetCovariance()[l][k]);
   208                     p2[t] = 
static_cast<double>((mod2[j])->GetCovariance()[l][k]);
   213             double distance = 0.0;
   214             for(
unsigned int k = 0; k < params; k++)
   216                 distance+=p1[k]-p2[k];
   219             distance/=
static_cast<double>(params);
   222                 comparisonVector[i]=j;
   231    for (
unsigned int i=0; i < NbClasses; i++)
   233         if(comparisonVector[i]==-1)
   235         for(
unsigned int j=i+1;j < NbClasses; j++)
   236             if( comparisonVector[i]==comparisonVector[j])
 bool sameModel(std::vector< GaussianFunctionType::Pointer > &mod1, std::vector< GaussianFunctionType::Pointer > &mod2)
compare two models 
void SetStrategy(std::vector< unsigned int > &ems, std::vector< unsigned int > &iters)
bool GetSolutionMap(std::map< double, std::vector< GaussianFunctionType::Pointer > > &solution, std::map< double, std::vector< double > > &solutionAlpha, int step=-1)
return the solutions map for each step 
void Update() ITK_OVERRIDE
executes the strategy