OTB  6.7.0
Orfeo Toolbox
otbHistogramStatisticsFunction.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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>
33 {
34  m_IsModified = true;
35 }
36 
37 template<class TInputHistogram, class TOutput>
41 {
42  if (m_IsModified == true)
43  {
44  this->Update();
45  }
46  return m_entropy;
47 }
48 
49 template<class TInputHistogram, class TOutput>
53 {
54  if (m_IsModified == true)
55  {
56  this->Update();
57  }
58  return m_mean;
59 }
60 
61 template<class TInputHistogram, class TOutput>
65 {
66  if (m_IsModified == true)
67  {
68  this->Update();
69  }
70  return m_covariance;
71 }
72 
73 template<class TInputHistogram, class TOutput>
74 void
77 {
78  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
79 
80  typename TInputHistogram::ConstIterator iter = histogram->Begin();
81  typename TInputHistogram::ConstIterator end = histogram->End();
82 
84  FrequencyType globalFrequency = histogram->GetTotalFrequency();
85  if (globalFrequency == 0)
86  {
87  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
88  }
89  while (iter != end)
90  {
91  RealType Proba = static_cast<RealType>(iter.GetFrequency());
92  Proba /= static_cast<RealType>(globalFrequency);
93  if (Proba != 0.0)
94  {
95  entropy -= Proba * std::log(Proba);
96  }
97  ++iter;
98  }
99  m_entropy.resize(1);
100  m_entropy[0] = static_cast<TOutput>(entropy);
101 }
102 
103 template<class TInputHistogram, class TOutput>
104 void
107 {
108  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
109 
110  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
111  m_mean.resize(NumberOfDimension);
112 
113  if (histogram->GetTotalFrequency() == 0)
114  {
115  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
116  }
117 
118  if (NumberOfDimension > 2)
119  {
120  itkExceptionMacro(<< "Histogram must have 1 or 2 dimension.");
121  }
122 
123  for (unsigned int noDim = 0; noDim < NumberOfDimension; noDim++)
124  {
126  for (unsigned int i = 0; i < histogram->GetSize()[noDim]; ++i)
127  {
128  MeasurementType val = histogram->GetMeasurement(i, noDim);
129  FrequencyType freq = histogram->GetFrequency(i, noDim);
130  mean += val * freq;
131  }
132  mean /= histogram->GetTotalFrequency();
133  m_mean[noDim] = static_cast<TOutput>(mean);
134  }
135 }
136 
137 template<class TInputHistogram, class TOutput>
138 void
141 {
142  CalculateMean();
143 
144  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
145 
146  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
147  m_covariance.resize(NumberOfDimension * NumberOfDimension);
148 
149  if (histogram->GetTotalFrequency() == 0)
150  {
151  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
152  }
153 
154  for (unsigned int noDimX = 0; noDimX < NumberOfDimension; noDimX++)
155  for (unsigned int noDimY = 0; noDimY < NumberOfDimension; noDimY++)
156  {
158  for (unsigned int i = 0; i < histogram->GetSize()[noDimX]; ++i)
159  for (unsigned int j = 0; j < histogram->GetSize()[noDimY]; ++j)
160  {
161  MeasurementType valX = histogram->GetMeasurement(i, noDimX);
162  MeasurementType valY = histogram->GetMeasurement(j, noDimY);
163  FrequencyType freqX = histogram->GetFrequency(i, noDimX);
164  FrequencyType freqY = histogram->GetFrequency(j, noDimY);
165 
166  valX -= static_cast<MeasurementType>(m_mean[noDimX]);
167  valY -= static_cast<MeasurementType>(m_mean[noDimY]);
168  covariance += ((valX * freqX) * (valY * freqY));
169  }
170  covariance /= histogram->GetTotalFrequency();
171  m_covariance[noDimX * NumberOfDimension + noDimY] = static_cast<TOutput>(covariance);
172  }
173 
174 }
175 
176 template<class TInputHistogram, class TOutput>
177 void
180 {
181 
182  CalculateEntropy();
183  CalculateMean();
184  CalculateCovariance();
185  m_IsModified = false;
186 }
187 
188 template<class TInputHistogram, class TOutput>
189 void
191 ::PrintSelf(std::ostream& os, itk::Indent indent) const
192 {
193  Superclass::PrintSelf(os, indent);
194 }
195 
196 } // end namespace otb
197 
198 #endif
itk::NumericTraits< MeasurementType >::RealType RealType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
TInputHistogram::AbsoluteFrequencyType FrequencyType
TInputHistogram::MeasurementType MeasurementType