OTB  9.0.0
Orfeo Toolbox
otbGaussianModelComponent.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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>
38 {
39  m_CovarianceEstimator = nullptr;
40  m_GaussianMembershipFunction = nullptr;
41 }
42 
43 template <class TSample>
44 void GaussianModelComponent<TSample>::PrintSelf(std::ostream& os, itk::Indent indent) const
45 {
46  Superclass::PrintSelf(os, indent);
47 
48  os << indent << "Mean Estimator: " << m_CovarianceEstimator << std::endl;
49  os << indent << "Covariance Estimator: " << m_CovarianceEstimator << std::endl;
50  os << indent << "GaussianMembershipFunction: " << m_GaussianMembershipFunction << std::endl;
51 }
52 
53 template <class TSample>
54 void GaussianModelComponent<TSample>::ShowParameters(std::ostream& os, itk::Indent indent) const
55 {
56  unsigned int i, j;
57  os << indent << "Gaussian model component : \n";
58  os << indent << "Mean : ";
59  for (i = 0; i < m_Mean.Size(); ++i)
60  os << m_Mean[i] << "\t";
61  os << "\n" << indent << "Covariance : ";
62  for (i = 0; i < m_Mean.Size(); ++i)
63  {
64  for (j = 0; j < m_Mean.Size(); ++j)
65  os << m_Covariance(i, j) << "\t";
66  os << "\n" << indent << " ";
67  }
68  os << "\n";
69 }
70 
71 template <class TSample>
72 void GaussianModelComponent<TSample>::SetSample(const TSample* sample)
73 {
74  Superclass::SetSample(sample);
75  const MeasurementVectorSizeType measurementVectorLength = sample->GetMeasurementVectorSize();
76  this->m_Parameters.SetSize(measurementVectorLength * (1 + measurementVectorLength));
77 
78  // Set the size of the mean vector
79  m_Mean.SetSize(measurementVectorLength);
80 
81 
82  // Set the parameters of the mean (internally) and the covariance estimator
83  m_Covariance.SetSize(measurementVectorLength, measurementVectorLength);
84 
85  m_CovarianceEstimator = CovarianceEstimatorType::New();
86  m_CovarianceEstimator->SetInput(sample);
87  m_CovarianceEstimator->Update();
88 
89  m_GaussianMembershipFunction = NativeMembershipFunctionType::New();
90  this->m_PdfFunction = (MembershipFunctionType*)m_GaussianMembershipFunction;
91  m_GaussianMembershipFunction->SetMeasurementVectorSize(measurementVectorLength);
92  this->SetPdfMembershipFunction((MembershipFunctionType*)m_GaussianMembershipFunction.GetPointer());
93 }
94 
95 template <class TSample>
97 {
98  Superclass::SetParameters(parameters);
99 
100  unsigned int paramIndex = 0;
101  unsigned int i, j;
102 
103  MeasurementVectorSizeType measurementVectorSize = this->GetSample()->GetMeasurementVectorSize();
104 
105  m_Mean.SetSize(measurementVectorSize);
106  for (i = 0; i < measurementVectorSize; i++)
107  {
108  m_Mean[i] = parameters[paramIndex];
109  paramIndex++;
110  }
111 
112  m_Covariance.SetSize(measurementVectorSize, measurementVectorSize);
113  for (i = 0; i < measurementVectorSize; i++)
114  for (j = 0; j < measurementVectorSize; j++)
115  {
116  m_Covariance(i, j) = parameters[paramIndex];
117  paramIndex++;
118  }
119 
120  this->m_GaussianMembershipFunction->SetMean(m_Mean);
121  this->m_GaussianMembershipFunction->SetCovariance(&m_Covariance);
122 }
123 
124 template <class TSample>
126 {
127  if (this->IsSampleModified() == 0)
128  return;
129 
130  MeasurementVectorSizeType measurementVectorSize = this->GetSample()->GetMeasurementVectorSize();
131 
132  unsigned int i, j;
133  int paramIndex = 0;
134 
135  // Get the mean using the convariance estimator (computed internally)
136  typename CovarianceEstimatorType::MeasurementVectorType meanOutput = m_CovarianceEstimator->GetMean();
137 
138  for (i = 0; i < measurementVectorSize; i++)
139  {
140  m_Mean.SetElement(i, meanOutput.GetElement(i));
141  this->m_Parameters[paramIndex] = meanOutput.GetElement(i);
142  ++paramIndex;
143  }
144 
145  // Get the covariance matrix and fill the parameters vector
146  const typename CovarianceEstimatorType::MatrixType covariance = m_CovarianceEstimator->GetCovarianceMatrix();
147 
148  for (i = 0; i < measurementVectorSize; i++)
149  for (j = 0; j < measurementVectorSize; j++)
150  {
151  this->m_Parameters[paramIndex] = covariance.GetVnlMatrix().get(i, j);
152  m_Covariance(i, j) = covariance.GetVnlMatrix().get(i, j);
153  paramIndex++;
154  }
155 
156  this->m_GaussianMembershipFunction->SetMean(meanOutput);
157  this->m_GaussianMembershipFunction->SetCovariance(m_Covariance);
158 
159  Superclass::GenerateData();
160 }
161 
162 } // end of namespace Statistics
163 } // end of namesapce otb
164 
165 #endif
otb::Statistics::GaussianModelComponent::GaussianModelComponent
GaussianModelComponent()
Definition: otbGaussianModelComponent.hxx:37
otb::Statistics::ModelComponentBase::ParametersType
itk::Array< double > ParametersType
Definition: otbModelComponentBase.h:85
otb::Statistics::GaussianModelComponent::ShowParameters
void ShowParameters(std::ostream &os, itk::Indent indent) const override
Definition: otbGaussianModelComponent.hxx:54
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Statistics::ModelComponentBase::MeasurementVectorSizeType
TSample::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: otbModelComponentBase.h:80
otbMacro.h
otbGaussianModelComponent.h
otb::Statistics::GaussianModelComponent::SetSample
void SetSample(const TSample *sample) override
Definition: otbGaussianModelComponent.hxx:72
otb::Statistics::GaussianModelComponent::SetParameters
void SetParameters(const ParametersType &parameters)
Definition: otbGaussianModelComponent.hxx:96
otb::Statistics::ModelComponentBase::MembershipFunctionType
itk::Statistics::MembershipFunctionBase< MeasurementVectorType > MembershipFunctionType
Definition: otbModelComponentBase.h:83
otb::Statistics::GaussianModelComponent::GenerateData
void GenerateData() override
Definition: otbGaussianModelComponent.hxx:125
otb::Statistics::GaussianModelComponent::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbGaussianModelComponent.hxx:44