OTB  9.0.0
Orfeo Toolbox
otbLHMI.h
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 otbLHMI_h
23 #define otbLHMI_h
24 
25 #include <vector>
26 #include "itkHistogram.h"
27 
28 namespace otb
29 {
30 
31 namespace Functor
32 {
33 
47 template <class TInput1, class TInput2, class TOutput>
48 class LHMI
49 {
50 
51 public:
52  typedef typename std::vector<TOutput> VectorType;
53  typedef typename VectorType::iterator IteratorType;
54  typedef typename std::vector<VectorType> VectorOfVectorType;
55  typedef typename VectorOfVectorType::iterator VecOfVecIteratorType;
56  typedef double HistogramFrequencyType;
57  typedef typename itk::Statistics::Histogram<HistogramFrequencyType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
58  typedef typename HistogramType::MeasurementVectorType MeasurementVectorType;
59  typedef typename HistogramType::SizeType HistogramSizeType;
60  typedef typename HistogramType::Iterator HistogramIteratorType;
61 
62  LHMI()
63  {
64  }
65  virtual ~LHMI()
66  {
67  }
68  inline TOutput operator()(const TInput1& itA, const TInput2& itB)
69  {
70  HistogramType::Pointer histogram;
71 
73  HistogramSizeType histogramSize(2);
74 
76  MeasurementVectorType lowerBound(2);
77 
79  MeasurementVectorType upperBound(2);
80 
81  double upperBoundIncreaseFactor = 0.001;
82 
83  histogramSize.Fill(256);
84 
85  TOutput maxA = itA.GetPixel(0);
86  TOutput minA = itA.GetPixel(0);
87  TOutput maxB = itB.GetPixel(0);
88  TOutput minB = itB.GetPixel(0);
89 
90  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
91  {
92 
93  TOutput value = static_cast<TOutput>(itA.GetPixel(pos));
94 
95  if (value > maxA)
96  maxA = value;
97  else if (value < minA)
98  minA = value;
99 
100  value = static_cast<TOutput>(itB.GetPixel(pos));
101 
102  if (value > maxB)
103  maxB = value;
104  else if (value < minB)
105  minB = value;
106  }
107 
108  // Initialize the upper and lower bounds of the histogram.
109  lowerBound[0] = minA;
110  lowerBound[1] = minB;
111  upperBound[0] = maxA + (maxA - minA) * upperBoundIncreaseFactor;
112  upperBound[1] = maxB + (maxB - minB) * upperBoundIncreaseFactor;
113 
114  histogram = HistogramType::New();
115 
116  histogram->SetMeasurementVectorSize(2);
117  histogram->Initialize(histogramSize, lowerBound, upperBound);
118 
119  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
120  {
121 
122  typename HistogramType::IndexType sample(2);
123  sample[0] = itA.GetPixel(pos);
124  sample[1] = itB.GetPixel(pos);
125  /*if(sample[0]!=NumericTraits<TOutput>::Zero &&
126  sample[1]!=NumericTraits<TOutput>::Zero)*/
127  histogram->IncreaseFrequencyOfIndex(sample, 1);
128  }
129 
130  TOutput entropyX = itk::NumericTraits<TOutput>::Zero;
131  TOutput entropyY = itk::NumericTraits<TOutput>::Zero;
132  TOutput jointEntropy = itk::NumericTraits<TOutput>::Zero;
133  HistogramFrequencyType totalFreq = histogram->GetTotalFrequency();
134 
135  for (unsigned int i = 0; i < histogram->GetSize()[0]; ++i)
136  {
137  HistogramFrequencyType freq = histogram->GetFrequency(i, 0);
138  if (freq > 0)
139  {
140  entropyX += freq * std::log(freq);
141  }
142  }
143 
144  entropyX = -entropyX / static_cast<TOutput>(totalFreq) + std::log(totalFreq);
145 
146  for (unsigned int i = 0; i < histogram->GetSize()[1]; ++i)
147  {
148  HistogramFrequencyType freq = histogram->GetFrequency(i, 1);
149  if (freq > 0)
150  {
151  entropyY += freq * std::log(freq);
152  }
153  }
154 
155  entropyY = -entropyY / static_cast<TOutput>(totalFreq) + std::log(totalFreq);
156 
157  HistogramIteratorType it = histogram->Begin();
158  HistogramIteratorType end = histogram->End();
159  while (it != end)
160  {
161  HistogramFrequencyType freq = it.GetFrequency();
162  if (freq > 0)
163  {
164  jointEntropy += freq * std::log(freq);
165  }
166  ++it;
167  }
168 
169  jointEntropy = -jointEntropy / static_cast<TOutput>(totalFreq) + std::log(totalFreq);
170 
171  return static_cast<TOutput>(jointEntropy / (entropyX + entropyY));
172  }
173 };
174 }
175 }
176 #endif
otb::Functor::LHMI
TODO.
Definition: otbLHMI.h:48
otb::Functor::LHMI::IteratorType
VectorType::iterator IteratorType
Definition: otbLHMI.h:53
otb::Functor::LHMI::HistogramType
itk::Statistics::Histogram< HistogramFrequencyType, itk::Statistics::DenseFrequencyContainer2 > HistogramType
Definition: otbLHMI.h:57
otb::Functor::LHMI::HistogramSizeType
HistogramType::SizeType HistogramSizeType
Definition: otbLHMI.h:59
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::LHMI::MeasurementVectorType
HistogramType::MeasurementVectorType MeasurementVectorType
Definition: otbLHMI.h:58
otb::Functor::LHMI::VectorType
std::vector< TOutput > VectorType
Definition: otbLHMI.h:52
otb::Functor::LHMI::HistogramFrequencyType
double HistogramFrequencyType
Definition: otbLHMI.h:56
otb::Functor::LHMI::operator()
TOutput operator()(const TInput1 &itA, const TInput2 &itB)
Definition: otbLHMI.h:68
otb::Functor::LHMI::VectorOfVectorType
std::vector< VectorType > VectorOfVectorType
Definition: otbLHMI.h:54
otb::Functor::LHMI::LHMI
LHMI()
Definition: otbLHMI.h:62
otb::Functor::LHMI::VecOfVecIteratorType
VectorOfVectorType::iterator VecOfVecIteratorType
Definition: otbLHMI.h:55
otb::Functor::LHMI::HistogramIteratorType
HistogramType::Iterator HistogramIteratorType
Definition: otbLHMI.h:60
otb::Functor::LHMI::~LHMI
virtual ~LHMI()
Definition: otbLHMI.h:65