OTB  6.7.0
Orfeo Toolbox
otbGaussianModelComponent.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbGaussianModelComponent_hxx
23 #define otbGaussianModelComponent_hxx
24 
25 #include <iostream>
26 
27 #include "itkNumericTraits.h"
28 #include "otbMacro.h"
30 
31 namespace otb
32 {
33 namespace Statistics
34 {
35 
36 template<class TSample>
39 {
40  m_CovarianceEstimator = nullptr;
41  m_GaussianMembershipFunction = nullptr;
42 }
43 
44 template<class TSample>
45 void
47 ::PrintSelf(std::ostream& os, itk::Indent indent) const
48 {
49  Superclass::PrintSelf(os, indent);
50 
51  os << indent << "Mean Estimator: " << m_CovarianceEstimator << std::endl;
52  os << indent << "Covariance Estimator: " << m_CovarianceEstimator << std::endl;
53  os << indent << "GaussianMembershipFunction: " << m_GaussianMembershipFunction << std::endl;
54 }
55 
56 template <class TSample>
57 void
59 ::ShowParameters(std::ostream& os, itk::Indent indent) const
60 {
61  unsigned int i, j;
62  os << indent << "Gaussian model component : \n";
63  os << indent << "Mean : ";
64  for (i = 0; i < m_Mean.Size(); ++i)
65  os << m_Mean[i] << "\t";
66  os << "\n" << indent << "Covariance : ";
67  for (i = 0; i < m_Mean.Size(); ++i)
68  {
69  for (j = 0; j < m_Mean.Size(); ++j)
70  os << m_Covariance(i, j) << "\t";
71  os << "\n" << indent << " ";
72  }
73  os << "\n";
74 }
75 
76 template<class TSample>
77 void
79 ::SetSample(const TSample* sample)
80 {
81  Superclass::SetSample(sample);
82  const MeasurementVectorSizeType measurementVectorLength = sample->GetMeasurementVectorSize();
83  this->m_Parameters.SetSize(measurementVectorLength * (1 + measurementVectorLength));
84 
85  // Set the size of the mean vector
86  m_Mean.SetSize(measurementVectorLength);
87 
88 
89  // Set the parameters of the mean (internally) and the covariance estimator
90  m_Covariance.SetSize(measurementVectorLength,
91  measurementVectorLength);
92 
93  m_CovarianceEstimator = CovarianceEstimatorType::New();
94  m_CovarianceEstimator->SetInput(sample);
95  m_CovarianceEstimator->Update();
96 
97  m_GaussianMembershipFunction = NativeMembershipFunctionType::New();
98  this->m_PdfFunction = (MembershipFunctionType *) m_GaussianMembershipFunction;
99  m_GaussianMembershipFunction->SetMeasurementVectorSize(
100  measurementVectorLength);
101  this->SetPdfMembershipFunction((MembershipFunctionType *)
102  m_GaussianMembershipFunction.GetPointer());
103 }
104 
105 template<class TSample>
106 void
108 ::SetParameters(const ParametersType& parameters)
109 {
110  Superclass::SetParameters(parameters);
111 
112  unsigned int paramIndex = 0;
113  unsigned int i, j;
114 
115  MeasurementVectorSizeType measurementVectorSize
116  = this->GetSample()->GetMeasurementVectorSize();
117 
118  m_Mean.SetSize (measurementVectorSize);
119  for (i = 0; i < measurementVectorSize; i++)
120  {
121  m_Mean[i] = parameters[paramIndex];
122  paramIndex++;
123  }
124 
125  m_Covariance.SetSize(measurementVectorSize, measurementVectorSize);
126  for (i = 0; i < measurementVectorSize; i++)
127  for (j = 0; j < measurementVectorSize; j++)
128  {
129  m_Covariance(i, j) = parameters[paramIndex];
130  paramIndex++;
131  }
132 
133  this->m_GaussianMembershipFunction->SetMean(m_Mean);
134  this->m_GaussianMembershipFunction->SetCovariance(&m_Covariance);
135 
136 }
137 
138 template<class TSample>
139 void
142 {
143  if (this->IsSampleModified() == 0) return;
144 
145  MeasurementVectorSizeType measurementVectorSize = this->GetSample()->GetMeasurementVectorSize();
146 
147  unsigned int i, j;
148  int paramIndex = 0;
149 
150  // Get the mean using the convariance estimator (computed internally)
151  typename CovarianceEstimatorType::MeasurementVectorType meanOutput = m_CovarianceEstimator->GetMean();
152 
153  for (i = 0; i < measurementVectorSize; i++)
154  {
155  m_Mean.SetElement(i,meanOutput.GetElement(i));
156  this->m_Parameters[paramIndex] = meanOutput.GetElement(i);
157  ++paramIndex;
158  }
159 
160  // Get the covariance matrix and fill the parameters vector
161  const typename CovarianceEstimatorType::MatrixType covariance = m_CovarianceEstimator->GetCovarianceMatrix();
162 
163  for (i = 0; i < measurementVectorSize; i++)
164  for (j = 0; j < measurementVectorSize; j++)
165  {
166  this->m_Parameters[paramIndex] = covariance.GetVnlMatrix().get(i, j);
167  m_Covariance(i, j) = covariance.GetVnlMatrix().get(i, j);
168  paramIndex++;
169  }
170 
171  this->m_GaussianMembershipFunction->SetMean(meanOutput);
172  this->m_GaussianMembershipFunction->SetCovariance(m_Covariance);
173 
174  Superclass::GenerateData();
175 }
176 
177 } // end of namespace Statistics
178 } // end of namesapce otb
179 
180 #endif
void SetSample(const TSample *sample) override
void SetParameters(const ParametersType &parameters)
SampleType::MeasurementVectorType MeasurementVectorType
void ShowParameters(std::ostream &os, itk::Indent indent) const override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
void SetSize(SizeValueType sz)
TSample::MeasurementVectorSizeType MeasurementVectorSizeType
InternalMatrixType & GetVnlMatrix(void)