OTB  9.0.0
Orfeo Toolbox
otbCrossCorrelation.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 otbCrossCorrelation_h
22 #define otbCrossCorrelation_h
23 
24 #include "itkMath.h"
25 
26 namespace otb
27 {
28 
29 namespace Functor
30 {
31 
46 template <class TInput1, class TInput2, class TOutput>
48 {
49 public:
51  {
52  }
54  {
55  }
56  inline TOutput operator()(const TInput1& itA, const TInput2& itB)
57  {
58 
59  TOutput meanA = itk::NumericTraits<TOutput>::Zero;
60  TOutput meanB = itk::NumericTraits<TOutput>::Zero;
61 
62  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
63  {
64 
65  meanA += static_cast<TOutput>(itA.GetPixel(pos));
66  meanB += static_cast<TOutput>(itB.GetPixel(pos));
67  }
68 
69  meanA /= itA.Size();
70  meanB /= itB.Size();
71 
72  TOutput varA = itk::NumericTraits<TOutput>::Zero;
73  TOutput varB = itk::NumericTraits<TOutput>::Zero;
74 
75  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
76  {
77 
78  varA += static_cast<TOutput>(std::pow(static_cast<double>(itA.GetPixel(pos)) - static_cast<double>(meanA), static_cast<double>(2.0)));
79  varB += static_cast<TOutput>(std::pow(static_cast<double>(itB.GetPixel(pos)) - static_cast<double>(meanB), static_cast<double>(2.0)));
80  }
81 
82  varA /= itA.Size();
83  varB /= itB.Size();
84 
85  TOutput crossCorrel = itk::NumericTraits<TOutput>::Zero;
86 
87  if (varA != itk::NumericTraits<TOutput>::Zero && varB != itk::NumericTraits<TOutput>::Zero)
88  {
89  for (unsigned long pos = 0; pos < itA.Size(); ++pos)
90  {
91  crossCorrel += (static_cast<TOutput>(itA.GetPixel(pos)) - meanA) * (static_cast<TOutput>(itB.GetPixel(pos)) - meanB) /
92  (itA.Size() * std::sqrt(static_cast<double>(varA * varB)));
93  }
94  }
95  else if (varA == itk::NumericTraits<TOutput>::Zero && varB == itk::NumericTraits<TOutput>::Zero)
96  {
97  crossCorrel = itk::NumericTraits<TOutput>::One;
98  }
99  return static_cast<TOutput>(itk::NumericTraits<TOutput>::One - crossCorrel);
100  }
101 };
102 }
103 
104 } // end namespace otb
105 
106 #endif
otb::Functor::CrossCorrelation
Functor to compute the cross correlation.
Definition: otbCrossCorrelation.h:47
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::CrossCorrelation::operator()
TOutput operator()(const TInput1 &itA, const TInput2 &itB)
Definition: otbCrossCorrelation.h:56
otb::Functor::CrossCorrelation::~CrossCorrelation
virtual ~CrossCorrelation()
Definition: otbCrossCorrelation.h:53
otb::Functor::CrossCorrelation::CrossCorrelation
CrossCorrelation()
Definition: otbCrossCorrelation.h:50