Orfeo Toolbox  3.16
itkGaussianMixtureModelComponent.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: itkGaussianMixtureModelComponent.txx,v $
5 Language: C++
6 Date: $Date: 2009-04-16 15:27:01 $
7 Version: $Revision: 1.19 $
8 
9 Copyright (c) Insight Software Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __itkGaussianMixtureModelComponent_txx
19 #define __itkGaussianMixtureModelComponent_txx
20 
21 #include <iostream>
22 
24 
25 namespace itk {
26 namespace Statistics {
27 
28 template< class TSample >
31 {
32  m_MeanEstimator = MeanEstimatorType::New();
33  m_CovarianceEstimator = CovarianceEstimatorType::New();
34  m_GaussianDensityFunction = NativeMembershipFunctionType::New();
35  this->SetMembershipFunction((MembershipFunctionType*)
36  m_GaussianDensityFunction.GetPointer());
37  m_Mean.Fill(0.0);
38  m_Covariance.SetIdentity();
39 }
40 
41 template< class TSample >
42 void
44 ::PrintSelf(std::ostream& os, Indent indent) const
45 {
46  Superclass::PrintSelf(os, indent);
47 
48  os << indent << "Mean: " << m_Mean << std::endl;
49  os << indent << "Covariance: " << m_Covariance << std::endl;
50  os << indent << "Mean Estimator: " << m_MeanEstimator << std::endl;
51  os << indent << "Covariance Estimator: " << m_CovarianceEstimator << std::endl;
52  os << indent << "GaussianDensityFunction: " << m_GaussianDensityFunction << std::endl;
53 }
54 
55 template< class TSample >
56 void
58 ::SetSample(const TSample* sample)
59 {
60  Superclass::SetSample(sample);
61 
62  m_MeanEstimator->SetInputSample(sample);
63  m_CovarianceEstimator->SetInputSample(sample);
64 
65  WeightArrayType* weights = this->GetWeights();
66  m_MeanEstimator->SetWeights(weights);
67  m_CovarianceEstimator->SetWeights(weights);
68  const MeasurementVectorSizeType measurementVectorLength =
69  sample->GetMeasurementVectorSize();
70  m_GaussianDensityFunction->SetMeasurementVectorSize(
71  measurementVectorLength );
72  MeasurementVectorTraits::SetLength( m_Mean, measurementVectorLength );
73  m_Covariance.SetSize( measurementVectorLength, measurementVectorLength );
74  m_Mean.Fill(NumericTraits< double >::NonpositiveMin());
75  m_Covariance.Fill(NumericTraits< double >::NonpositiveMin());
76  m_CovarianceEstimator->SetMean(&m_Mean);
77  m_GaussianDensityFunction->SetMean(&m_Mean);
78 }
79 
80 template< class TSample >
81 void
83 ::SetParameters(const ParametersType &parameters)
84 {
85  Superclass::SetParameters(parameters);
86 
87  unsigned int paramIndex = 0;
88  unsigned int i, j;
89 
90  bool changed = false;
91 
92  MeasurementVectorSizeType measurementVectorSize =
93  this->GetSample()->GetMeasurementVectorSize();
94 
95  for ( i = 0; i < measurementVectorSize; i++)
96  {
97  if ( m_Mean[i] != parameters[paramIndex] )
98  {
99  m_Mean[i] = parameters[paramIndex];
100  changed = true;
101  }
102  ++paramIndex;
103  }
104 
105  for ( i = 0; i < measurementVectorSize; i++ )
106  {
107  for ( j = 0; j < measurementVectorSize; j++ )
108  {
109  if ( m_Covariance.GetVnlMatrix().get(i, j) !=
110  parameters[paramIndex] )
111  {
112  m_Covariance.GetVnlMatrix().put(i, j, parameters[paramIndex]);
113  changed = true;
114  }
115  ++paramIndex;
116  }
117  }
118  m_GaussianDensityFunction->SetCovariance(&m_Covariance);
119  this->AreParametersModified(changed);
120 }
121 
122 
123 template< class TSample >
124 double
127 {
128  unsigned int i, j;
129 
130  MeanType meanEstimate = *(m_MeanEstimator->GetOutput());
131  CovarianceType covEstimate = *(m_CovarianceEstimator->GetOutput());
132 
133  double temp;
134  double changes = 0.0;
135  MeasurementVectorSizeType measurementVectorSize =
136  this->GetSample()->GetMeasurementVectorSize();
137 
138  for ( i = 0; i < measurementVectorSize; i++)
139  {
140  temp = m_Mean[i] - meanEstimate[i];
141  changes += temp * temp;
142  }
143 
144  for ( i = 0; i < measurementVectorSize; i++ )
145  {
146  for ( j = 0; j < measurementVectorSize; j++ )
147  {
148  temp = m_Covariance.GetVnlMatrix().get(i, j) -
149  covEstimate.GetVnlMatrix().get(i, j);
150  changes += temp * temp;
151  }
152  }
153 
154  changes = vcl_sqrt(changes);
155  return changes;
156 }
157 
158 template< class TSample >
159 void
162 {
163  MeasurementVectorSizeType measurementVectorSize =
164  this->GetSample()->GetMeasurementVectorSize();
165 
166  this->AreParametersModified(false);
167 
168  m_MeanEstimator->Update();
169 
170  unsigned int i, j;
171  double temp;
172  double changes;
173  bool changed = false;
174  ParametersType parameters = this->GetFullParameters();
175  int paramIndex = 0;
176 
177  MeanType meanEstimate = *(m_MeanEstimator->GetOutput());
178  for ( i = 0; i < measurementVectorSize; i++)
179  {
180  temp = m_Mean[i] - meanEstimate[i];
181  changes = temp * temp;
182  changes = vcl_sqrt(changes);
183  if ( changes > this->GetMinimalParametersChange() )
184  {
185  changed = true;
186  }
187  }
188 
189 
190  if ( changed )
191  {
192  m_Mean = *(m_MeanEstimator->GetOutput());
193  for ( i = 0; i < measurementVectorSize; i++)
194  {
195  parameters[paramIndex] = meanEstimate[i];
196  ++paramIndex;
197  }
198  this->AreParametersModified(true);
199  }
200  else
201  {
202  paramIndex = measurementVectorSize;
203  }
204 
205  m_CovarianceEstimator->Update();
206  CovarianceType covEstimate = *(m_CovarianceEstimator->GetOutput());
207  changed = false;
208  for ( i = 0; i < measurementVectorSize; i++ )
209  {
210  for ( j = 0; j < measurementVectorSize; j++ )
211  {
212  temp = m_Covariance.GetVnlMatrix().get(i, j) -
213  covEstimate.GetVnlMatrix().get(i, j);
214  changes = temp * temp;
215  changes = vcl_sqrt(changes);
216  if ( changes > this->GetMinimalParametersChange() )
217  {
218  changed = true;
219  }
220  }
221  }
222 
223  if ( changed )
224  {
225  m_Covariance = *(m_CovarianceEstimator->GetOutput());
226  for ( i = 0; i < measurementVectorSize; i++ )
227  {
228  for ( j = 0; j < measurementVectorSize; j++ )
229  {
230  parameters[paramIndex] = covEstimate.GetVnlMatrix().get(i, j);
231  ++paramIndex;
232  }
233  }
234  this->AreParametersModified(true);
235  }
236 
237  Superclass::SetParameters(parameters);
238  //update covariance and its inverse of Gaussian mixture
239  m_GaussianDensityFunction->SetCovariance( &m_Covariance );
240 }
241 
242 } // end of namespace Statistics
243 } // end of namespace itk
244 
245 #endif

Generated at Sat Feb 2 2013 23:38:57 for Orfeo Toolbox with doxygen 1.8.1.1