OTB  9.0.0
Orfeo Toolbox
otbHistogramStatisticsFunction.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
22 #ifndef otbHistogramStatisticsFunction_hxx
23 #define otbHistogramStatisticsFunction_hxx
24 
26 
27 namespace otb
28 {
29 
30 template <class TInputHistogram, class TOutput>
32 {
33  m_IsModified = true;
34 }
35 
36 template <class TInputHistogram, class TOutput>
38 {
39  if (m_IsModified == true)
40  {
41  this->Update();
42  }
43  return m_entropy;
44 }
45 
46 template <class TInputHistogram, class TOutput>
48 {
49  if (m_IsModified == true)
50  {
51  this->Update();
52  }
53  return m_mean;
54 }
55 
56 template <class TInputHistogram, class TOutput>
58 {
59  if (m_IsModified == true)
60  {
61  this->Update();
62  }
63  return m_covariance;
64 }
65 
66 template <class TInputHistogram, class TOutput>
68 {
69  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
70 
71  typename TInputHistogram::ConstIterator iter = histogram->Begin();
72  typename TInputHistogram::ConstIterator end = histogram->End();
73 
74  RealType entropy = itk::NumericTraits<RealType>::Zero;
75  FrequencyType globalFrequency = histogram->GetTotalFrequency();
76  if (globalFrequency == 0)
77  {
78  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
79  }
80  while (iter != end)
81  {
82  RealType Proba = static_cast<RealType>(iter.GetFrequency());
83  Proba /= static_cast<RealType>(globalFrequency);
84  if (Proba != 0.0)
85  {
86  entropy -= Proba * std::log(Proba);
87  }
88  ++iter;
89  }
90  m_entropy.resize(1);
91  m_entropy[0] = static_cast<TOutput>(entropy);
92 }
93 
94 template <class TInputHistogram, class TOutput>
96 {
97  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
98 
99  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
100  m_mean.resize(NumberOfDimension);
101 
102  if (histogram->GetTotalFrequency() == 0)
103  {
104  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
105  }
106 
107  if (NumberOfDimension > 2)
108  {
109  itkExceptionMacro(<< "Histogram must have 1 or 2 dimension.");
110  }
111 
112  for (unsigned int noDim = 0; noDim < NumberOfDimension; noDim++)
113  {
114  MeasurementType mean = itk::NumericTraits<MeasurementType>::Zero;
115  for (unsigned int i = 0; i < histogram->GetSize()[noDim]; ++i)
116  {
117  MeasurementType val = histogram->GetMeasurement(i, noDim);
118  FrequencyType freq = histogram->GetFrequency(i, noDim);
119  mean += val * freq;
120  }
121  mean /= histogram->GetTotalFrequency();
122  m_mean[noDim] = static_cast<TOutput>(mean);
123  }
124 }
125 
126 template <class TInputHistogram, class TOutput>
128 {
129  CalculateMean();
130 
131  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
132 
133  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
134  m_covariance.resize(NumberOfDimension * NumberOfDimension);
135 
136  if (histogram->GetTotalFrequency() == 0)
137  {
138  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
139  }
140 
141  for (unsigned int noDimX = 0; noDimX < NumberOfDimension; noDimX++)
142  for (unsigned int noDimY = 0; noDimY < NumberOfDimension; noDimY++)
143  {
144  MeasurementType covariance = itk::NumericTraits<MeasurementType>::Zero;
145  for (unsigned int i = 0; i < histogram->GetSize()[noDimX]; ++i)
146  for (unsigned int j = 0; j < histogram->GetSize()[noDimY]; ++j)
147  {
148  MeasurementType valX = histogram->GetMeasurement(i, noDimX);
149  MeasurementType valY = histogram->GetMeasurement(j, noDimY);
150  FrequencyType freqX = histogram->GetFrequency(i, noDimX);
151  FrequencyType freqY = histogram->GetFrequency(j, noDimY);
152 
153  valX -= static_cast<MeasurementType>(m_mean[noDimX]);
154  valY -= static_cast<MeasurementType>(m_mean[noDimY]);
155  covariance += ((valX * freqX) * (valY * freqY));
156  }
157  covariance /= histogram->GetTotalFrequency();
158  m_covariance[noDimX * NumberOfDimension + noDimY] = static_cast<TOutput>(covariance);
159  }
160 }
161 
162 template <class TInputHistogram, class TOutput>
164 {
165 
166  CalculateEntropy();
167  CalculateMean();
168  CalculateCovariance();
169  m_IsModified = false;
170 }
171 
172 template <class TInputHistogram, class TOutput>
173 void HistogramStatisticsFunction<TInputHistogram, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
174 {
175  Superclass::PrintSelf(os, indent);
176 }
177 
178 } // end namespace otb
179 
180 #endif
otb::HistogramStatisticsFunction::GetEntropy
OutputType GetEntropy()
Definition: otbHistogramStatisticsFunction.hxx:37
otb::mean
Definition: otbParserXPlugins.h:261
otb::HistogramStatisticsFunction::FrequencyType
TInputHistogram::AbsoluteFrequencyType FrequencyType
Definition: otbHistogramStatisticsFunction.h:54
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::HistogramStatisticsFunction::CalculateEntropy
void CalculateEntropy()
Definition: otbHistogramStatisticsFunction.hxx:67
otb::HistogramStatisticsFunction::MeasurementType
TInputHistogram::MeasurementType MeasurementType
Definition: otbHistogramStatisticsFunction.h:53
otb::HistogramStatisticsFunction::RealType
itk::NumericTraits< MeasurementType >::RealType RealType
Definition: otbHistogramStatisticsFunction.h:55
otb::HistogramStatisticsFunction::OutputType
std::vector< TOutput > OutputType
Definition: otbHistogramStatisticsFunction.h:59
otb::HistogramStatisticsFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbHistogramStatisticsFunction.hxx:173
otbHistogramStatisticsFunction.h
otb::HistogramStatisticsFunction::CalculateMean
void CalculateMean()
Definition: otbHistogramStatisticsFunction.hxx:95
otb::HistogramStatisticsFunction::CalculateCovariance
void CalculateCovariance()
Definition: otbHistogramStatisticsFunction.hxx:127
otb::HistogramStatisticsFunction::GenerateData
void GenerateData()
Definition: otbHistogramStatisticsFunction.hxx:163
otb::HistogramStatisticsFunction::HistogramStatisticsFunction
HistogramStatisticsFunction()
Definition: otbHistogramStatisticsFunction.hxx:31
otb::HistogramStatisticsFunction::GetMean
OutputType GetMean()
Definition: otbHistogramStatisticsFunction.hxx:47
otb::HistogramStatisticsFunction::GetCovariance
OutputType GetCovariance()
Definition: otbHistogramStatisticsFunction.hxx:57