6 template <
typename TInputImage,
typename TMaskImage>
9 std::vector<vnl_matrix<GaussianREMEstimator::NumericType> > inverseCovariance;
10 inverseCovariance.reserve(this->m_GaussianModel.size());
11 double *determinantCovariance =
new double[this->m_GaussianModel.size()];
13 if(!this->m_APosterioriProbability.empty())
14 this->m_APosterioriProbability.clear();
15 if(!this->m_JointHistogram.empty())
16 this->m_JointHistogram.clear();
18 GaussianFunctionType::CovarianceMatrixType *covari =
new GaussianFunctionType::CovarianceMatrixType[this->m_GaussianModel.size()];
19 for(
unsigned int i = 0; i < this->m_GaussianModel.size(); i++)
21 covari[i] = (this->m_GaussianModel[i])->GetCovariance();
25 for(
unsigned int i = 0; i < this->m_GaussianModel.size(); i++)
27 determinantCovariance[i] = vnl_determinant(covari[i].GetVnlMatrix());
28 if(std::fabs(determinantCovariance[i]) < 1e-12)
32 inverseCovariance.push_back(covari[i].GetInverse());
35 Histogram::iterator histoIt;
36 GaussianFunctionType::CovarianceMatrixType intensities(this->m_OriginalJointHistogram.begin()->first.size(),1);
37 std::vector<double> probas(this->m_GaussianModel.size());
40 double numberOfPixels=0;
42 GaussianFunctionType::CovarianceMatrixType *
xi =
new GaussianFunctionType::CovarianceMatrixType[probas.size()];
43 for(
unsigned int i = 0; i < probas.size(); i++)
45 GaussianFunctionType::MeanVectorType mu = (this->m_GaussianModel[i])->GetMean();
46 (xi[i]).SetSize(mu.Size(),1);
47 for(
unsigned int j = 0; j < mu.Size(); j++)
53 GaussianFunctionType::CovarianceMatrixType x,xT;
54 for(histoIt = this->m_OriginalJointHistogram.begin(); histoIt != this->m_OriginalJointHistogram.end(); ++histoIt)
57 for(
unsigned int i = 0; i < histoIt->first.size(); i++)
59 intensities(i,0) =
static_cast<double>(histoIt->first[i]);
62 double concentrationValue = 0.0;
65 for(
unsigned int i = 0; i < probas.size(); i++)
67 x = intensities - xi[i];
68 xT = (x.GetTranspose());
69 vnl_matrix<GaussianREMEstimator::NumericType> result = xT.GetVnlMatrix() * inverseCovariance[i] * x.GetVnlMatrix();
70 probas[i] = result(0,0)/2;
71 concentrationValue += this->m_Alphas[i] * std::exp(-probas[i]) / std::sqrt( determinantCovariance[i] );
78 residualMap.insert(ResidualMap::value_type(std::log(concentrationValue),histoIt->first));
79 numberOfPixels+=histoIt->second;
82 ResidualMap::iterator it = residualMap.begin();
85 double numberOfRejections = this->m_RejectionRatio * numberOfPixels;
87 this->m_JointHistogram = this->m_OriginalJointHistogram;
89 for(it=residualMap.begin(); it != residualMap.end(); ++it)
91 if(rejected >= numberOfRejections)
93 histoIt = this->m_JointHistogram.find(it->second);
94 if(histoIt == this->m_JointHistogram.end())
98 double actual = histoIt->second;
99 if(actual+rejected >= numberOfRejections)
102 histoIt->second = actual+rejected-numberOfRejections;
108 this->m_JointHistogram.erase(histoIt);
113 delete[] determinantCovariance;
114 determinantCovariance = NULL;
122 template <
typename TInputImage,
typename TMaskImage>
124 ::PrintSolution(std::vector<double> alphas, std::vector<GaussianFunctionType::Pointer> model)
126 unsigned int nbTissus = alphas.size();
127 unsigned int nbModalities = (model[0])->GetMean().Size();
128 for(
unsigned int i = 0; i < nbTissus; i++)
130 std::cout <<
"* Class: " << i << std::endl;
131 std::cout <<
" Alpha: " << alphas[i] << std::endl;
132 GaussianFunctionType::MeanVectorType mu = (model[i])->GetMean();
133 std::cout <<
" Mean: " << std::endl;
134 for(
unsigned int j = 0; j < nbModalities; j++)
136 std::cout <<
" " << mu[j] << std::endl;
139 GaussianFunctionType::CovarianceMatrixType covar = (model[i])->GetCovariance();
140 std::cout <<
" covar: " << std::endl;
141 for(
unsigned int k = 0; k < nbModalities; k++)
143 std::cout <<
" " << covar(k,0) <<
" " << covar(k,1) <<
" " << covar(k,2) << std::endl;
149 template <
typename TInputImage,
typename TMaskImage>
152 this->createJointHistogram();
154 this->m_OriginalJointHistogram = this->m_JointHistogramInitial;
156 unsigned int iter = 0;
157 double distance = 0.0;
159 this->m_Likelihood = 0.0;
161 if( this->m_StremMode )
162 this->m_JointHistogram = this->m_OriginalJointHistogram;
165 if( !this->concentration() )
167 this->m_Likelihood = 0.0;
177 this->m_Likelihood = this->expectation();
179 if( this->m_Likelihood >= 0.0 )
181 this->m_Likelihood = 0.0;
185 std::vector<GaussianFunctionType::Pointer> newModel;
186 std::vector<double> newAlphas;
187 if( !this->maximization(newModel,newAlphas) )
189 this->m_Likelihood = 0.0;
193 distance = this->computeDistance(newModel);
195 this->m_GaussianModel = newModel;
196 this->m_Alphas = newAlphas;
198 if( this->m_Verbose )
200 std::cout <<
"current iteration / max iteration between concentration step: "<< emIter <<
" / " << this->m_MaxIterationsConc << std::endl;
201 std::cout <<
"current distance / minimum distance: "<< distance <<
" / " << this->m_ModelMinDistance << std::endl;
202 std::cout <<
"Current model: " << std::endl;
203 this->PrintSolution(this->m_Alphas, this->m_GaussianModel);
204 std::cout << std::endl;
206 double ratio = this->m_ModelMinDistance / distance;
211 this->UpdateProgress(ratio);
212 std::cout << std::endl;
217 }
while((distance > this->m_ModelMinDistance) && emIter < this->m_MaxIterationsConc);
219 if( !this->concentration() )
221 this->m_Likelihood = 0.0;
226 }
while((distance > this->m_ModelMinDistance) && iter < this->m_MaxIterations);
228 this->m_Likelihood = this->expectation();
int PrintSolution(std::vector< double > alphas, std::vector< GaussianFunctionType::Pointer > model)
virtual bool concentration()
virtual void Update() ITK_OVERRIDE
std::map< double, std::vector< unsigned short > > ResidualMap