ANIMA  4.0
animaRandomInitializer.cxx
Go to the documentation of this file.
2 
3 namespace anima
4 {
5 
7 {
8  if(this->minValues.size() != this->maxValues.size())
9  {
10  std::cerr << " --error: minValues and maxValues don't have the same size" <<std::endl;
11  return;
12  }
13  if(this->minValues.size()==0)
14  {
15  std::cerr << " --error: Initialization vector was empty" <<std::endl;
16  return;
17  }
18  if(m_NbGaussian==0)
19  {
20  std::cerr << " --error: number of gaussian is null" <<std::endl;
21  return;
22  }
23 
24  m_Alphas.clear();
25  m_GaussianModel.clear();
26  m_DimensionGaussian = this->maxValues.size();
27 
28  //Alpha's initialization
29  double *alphas = new double[m_NbGaussian];
30  double norm = 0.0;
31  unsigned int i = 0;
32 
33  while( i < m_NbGaussian)
34  {
35  double f = (static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX));
36  if(f!=0)
37  {
38  alphas[i] = f;
39  norm += f;
40  i++;
41  }
42  }
43 
44  for( i = 0; i < m_NbGaussian; i++)
45  {
46  m_Alphas.push_back(alphas[i]/norm);
47  GaussianFunctionType::Pointer distribution = this->randomDistribution();
48  m_GaussianModel.push_back(distribution);
49  }
50 
51  delete[] alphas;
52  alphas = NULL;
53 }
54 
55 itk::Statistics::GaussianMembershipFunction<itk::VariableLengthVector<double> >::Pointer RandomInitializer::randomDistribution()
56 {
57  GaussianFunctionType::Pointer distribution = GaussianFunctionType::New();
58  GaussianFunctionType::MeanVectorType mean( m_DimensionGaussian );
59  mean.Fill( 0.0 );
60  GaussianFunctionType::CovarianceMatrixType cov;
62  cov.Fill( 0.0 );
63 
64  for(unsigned int j = 0; j < m_DimensionGaussian; j++)
65  {
66  mean[j] = randUniform( this->minValues[j],this->maxValues[j]);
67  cov[j][j] = randUniform(this->minValues[j],this->maxValues[j])+0.001; //we don't want a 0.0 variance
68  }
69 
70  distribution->SetMean( mean );
71  distribution->SetCovariance( cov );
72 
73  return distribution;
74 }
75 
76 double RandomInitializer::randUniform(double min,double max)
77 {
78  double value = ( static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX) ); // uniform variable between 0.0 et 1.000
79  return value*(max-min) + min;
80 }
81 
82 
83 }
std::vector< double > maxValues
max values
std::vector< double > minValues
min values
std::vector< double > m_Alphas
itk::SmartPointer< Self > Pointer
itk::Statistics::GaussianMembershipFunction< itk::VariableLengthVector< double > >::Pointer randomDistribution()
initilize a Gaussian distribution
double randUniform(double min, double max)
std::vector< GaussianFunctionType::Pointer > m_GaussianModel