21 #ifndef otbCrossCorrelation_h
22 #define otbCrossCorrelation_h
46 template <
class TInput1,
class TInput2,
class TOutput>
56 inline TOutput
operator()(
const TInput1& itA,
const TInput2& itB)
59 TOutput meanA = itk::NumericTraits<TOutput>::Zero;
60 TOutput meanB = itk::NumericTraits<TOutput>::Zero;
62 for (
unsigned long pos = 0; pos < itA.Size(); ++pos)
65 meanA +=
static_cast<TOutput
>(itA.GetPixel(pos));
66 meanB +=
static_cast<TOutput
>(itB.GetPixel(pos));
72 TOutput varA = itk::NumericTraits<TOutput>::Zero;
73 TOutput varB = itk::NumericTraits<TOutput>::Zero;
75 for (
unsigned long pos = 0; pos < itA.Size(); ++pos)
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)));
85 TOutput crossCorrel = itk::NumericTraits<TOutput>::Zero;
87 if (varA != itk::NumericTraits<TOutput>::Zero && varB != itk::NumericTraits<TOutput>::Zero)
89 for (
unsigned long pos = 0; pos < itA.Size(); ++pos)
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)));
95 else if (varA == itk::NumericTraits<TOutput>::Zero && varB == itk::NumericTraits<TOutput>::Zero)
97 crossCorrel = itk::NumericTraits<TOutput>::One;
99 return static_cast<TOutput
>(itk::NumericTraits<TOutput>::One - crossCorrel);