Orfeo Toolbox  4.0
otbHistogramStatisticsFunction.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
19 #ifndef __otbHistogramStatisticsFunction_txx
20 #define __otbHistogramStatisticsFunction_txx
21 
23 
24 namespace otb
25 {
26 
27 template<class TInputHistogram, class TOutput>
30 {
31  m_IsModified = true;
32 }
33 
34 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>
50 {
51  if (m_IsModified == true)
52  {
53  this->Update();
54  }
55  return m_mean;
56 }
57 
58 template<class TInputHistogram, class TOutput>
62 {
63  if (m_IsModified == true)
64  {
65  this->Update();
66  }
67  return m_covariance;
68 }
69 
70 template<class TInputHistogram, class TOutput>
71 void
74 {
75  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
76 
77  typename TInputHistogram::ConstIterator iter = histogram->Begin();
78  typename TInputHistogram::ConstIterator end = histogram->End();
79 
81  FrequencyType globalFrequency = histogram->GetTotalFrequency();
82  if (globalFrequency == 0)
83  {
84  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
85  }
86  while (iter != end)
87  {
88  RealType Proba = static_cast<RealType>(iter.GetFrequency());
89  Proba /= static_cast<RealType>(globalFrequency);
90  if (Proba != 0.0)
91  {
92  entropy -= Proba * vcl_log(Proba);
93  }
94  ++iter;
95  }
96  m_entropy.resize(1);
97  m_entropy[0] = static_cast<TOutput>(entropy);
98 }
99 
100 template<class TInputHistogram, class TOutput>
101 void
104 {
105  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
106 
107  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
108  m_mean.resize(NumberOfDimension);
109 
110  if (histogram->GetTotalFrequency() == 0)
111  {
112  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
113  }
114 
115  if (NumberOfDimension > 2)
116  {
117  itkExceptionMacro(<< "Histogram must have 1 or 2 dimension.");
118  }
119 
120  for (unsigned int noDim = 0; noDim < NumberOfDimension; noDim++)
121  {
123  for (unsigned int i = 0; i < histogram->GetSize()[noDim]; ++i)
124  {
125  MeasurementType val = histogram->GetMeasurement(i, noDim);
126  FrequencyType freq = histogram->GetFrequency(i, noDim);
127  mean += val * freq;
128  }
129  mean /= histogram->GetTotalFrequency();
130  m_mean[noDim] = static_cast<TOutput>(mean);
131  }
132 }
133 
134 template<class TInputHistogram, class TOutput>
135 void
138 {
139  CalculateMean();
140 
141  typename TInputHistogram::ConstPointer histogram = m_InputHistogram;
142 
143  unsigned int NumberOfDimension = histogram->GetSize().GetSize();
144  m_covariance.resize(NumberOfDimension * NumberOfDimension);
145 
146  if (histogram->GetTotalFrequency() == 0)
147  {
148  itkExceptionMacro(<< "Histogram must contain at least 1 element.");
149  }
150 
151  for (unsigned int noDimX = 0; noDimX < NumberOfDimension; noDimX++)
152  for (unsigned int noDimY = 0; noDimY < NumberOfDimension; noDimY++)
153  {
155  for (unsigned int i = 0; i < histogram->GetSize()[noDimX]; ++i)
156  for (unsigned int j = 0; j < histogram->GetSize()[noDimY]; ++j)
157  {
158  MeasurementType valX = histogram->GetMeasurement(i, noDimX);
159  MeasurementType valY = histogram->GetMeasurement(j, noDimY);
160  FrequencyType freqX = histogram->GetFrequency(i, noDimX);
161  FrequencyType freqY = histogram->GetFrequency(j, noDimY);
162 
163  valX -= static_cast<MeasurementType>(m_mean[noDimX]);
164  valY -= static_cast<MeasurementType>(m_mean[noDimY]);
165  covariance += ((valX * freqX) * (valY * freqY));
166  }
167  covariance /= histogram->GetTotalFrequency();
168  m_covariance[noDimX * NumberOfDimension + noDimY] = static_cast<TOutput>(covariance);
169  }
170 
171 }
172 
173 template<class TInputHistogram, class TOutput>
174 void
177 {
178 
179  CalculateEntropy();
180  CalculateMean();
181  CalculateCovariance();
182  m_IsModified = false;
183 }
184 
185 template<class TInputHistogram, class TOutput>
186 void
188 ::PrintSelf(std::ostream& os, itk::Indent indent) const
189 {
190  Superclass::PrintSelf(os, indent);
191 }
192 
193 } // end namespace otb
194 
195 #endif

Generated at Sat Mar 8 2014 15:57:48 for Orfeo Toolbox with doxygen 1.8.3.1