OTB  9.0.0
Orfeo Toolbox
otbJoinHistogramMI.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 #ifndef otbJoinHistogramMI_h
22 #define otbJoinHistogramMI_h
23 
24 #include "itkHistogram.h"
25 
26 namespace otb
27 {
28 
29 namespace Functor
30 {
31 
32 template <class TInput1, class TInput2, class TOutput>
34 {
35 public:
36  typedef double HistogramFrequencyType;
37  typedef typename itk::Statistics::Histogram<HistogramFrequencyType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
39  {
40  }
41  virtual ~JoinHistogramMI()
42  {
43  }
44  inline TOutput operator()(const TInput1& itA, const TInput2& itB, const HistogramType* histogram)
45  {
46  TOutput jointEntropy = itk::NumericTraits<TOutput>::Zero;
47  HistogramFrequencyType totalFreq = histogram->GetTotalFrequency();
48 
49  typename HistogramType::MeasurementVectorType sample(2);
50  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
51  {
52  double valueA = static_cast<double>(itA.GetPixel(pos));
53  double valueB = static_cast<double>(itB.GetPixel(pos));
54 
55  sample[0] = valueA;
56  sample[1] = valueB;
57 
58  typename HistogramType::IndexType index;
59  histogram->GetIndex(sample, index);
60  HistogramFrequencyType freq = histogram->GetFrequency(index);
61  if (freq > 0)
62  {
63  jointEntropy += freq * std::log(freq);
64  }
65  }
66 
67  jointEntropy = -jointEntropy / static_cast<TOutput>(totalFreq) + std::log(totalFreq);
68 
69  return jointEntropy;
70  }
71 };
72 }
73 } // end namespace otb
74 
75 #endif
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::JoinHistogramMI::HistogramFrequencyType
double HistogramFrequencyType
Definition: otbJoinHistogramMI.h:36
otb::Functor::JoinHistogramMI::operator()
TOutput operator()(const TInput1 &itA, const TInput2 &itB, const HistogramType *histogram)
Definition: otbJoinHistogramMI.h:44
otb::Functor::JoinHistogramMI
Definition: otbJoinHistogramMI.h:33
otb::Functor::JoinHistogramMI::~JoinHistogramMI
virtual ~JoinHistogramMI()
Definition: otbJoinHistogramMI.h:41
otb::Functor::JoinHistogramMI::HistogramType
itk::Statistics::Histogram< HistogramFrequencyType, itk::Statistics::DenseFrequencyContainer2 > HistogramType
Definition: otbJoinHistogramMI.h:37
otb::Functor::JoinHistogramMI::JoinHistogramMI
JoinHistogramMI()
Definition: otbJoinHistogramMI.h:38