26 #include "itkHistogram.h"
47 template <
class TInput1,
class TInput2,
class TOutput>
57 typedef typename itk::Statistics::Histogram<HistogramFrequencyType, itk::Statistics::DenseFrequencyContainer2>
HistogramType;
68 inline TOutput
operator()(
const TInput1& itA,
const TInput2& itB)
70 HistogramType::Pointer histogram;
81 double upperBoundIncreaseFactor = 0.001;
83 histogramSize.Fill(256);
85 TOutput maxA = itA.GetPixel(0);
86 TOutput minA = itA.GetPixel(0);
87 TOutput maxB = itB.GetPixel(0);
88 TOutput minB = itB.GetPixel(0);
90 for (
unsigned long pos = 0; pos < itA.Size(); ++pos)
93 TOutput value =
static_cast<TOutput
>(itA.GetPixel(pos));
97 else if (value < minA)
100 value =
static_cast<TOutput
>(itB.GetPixel(pos));
104 else if (value < minB)
109 lowerBound[0] = minA;
110 lowerBound[1] = minB;
111 upperBound[0] = maxA + (maxA - minA) * upperBoundIncreaseFactor;
112 upperBound[1] = maxB + (maxB - minB) * upperBoundIncreaseFactor;
114 histogram = HistogramType::New();
116 histogram->SetMeasurementVectorSize(2);
117 histogram->Initialize(histogramSize, lowerBound, upperBound);
119 for (
unsigned long pos = 0; pos < itA.Size(); ++pos)
122 typename HistogramType::IndexType sample(2);
123 sample[0] = itA.GetPixel(pos);
124 sample[1] = itB.GetPixel(pos);
127 histogram->IncreaseFrequencyOfIndex(sample, 1);
130 TOutput entropyX = itk::NumericTraits<TOutput>::Zero;
131 TOutput entropyY = itk::NumericTraits<TOutput>::Zero;
132 TOutput jointEntropy = itk::NumericTraits<TOutput>::Zero;
135 for (
unsigned int i = 0; i < histogram->GetSize()[0]; ++i)
140 entropyX += freq * std::log(freq);
144 entropyX = -entropyX /
static_cast<TOutput
>(totalFreq) + std::log(totalFreq);
146 for (
unsigned int i = 0; i < histogram->GetSize()[1]; ++i)
151 entropyY += freq * std::log(freq);
155 entropyY = -entropyY /
static_cast<TOutput
>(totalFreq) + std::log(totalFreq);
164 jointEntropy += freq * std::log(freq);
169 jointEntropy = -jointEntropy /
static_cast<TOutput
>(totalFreq) + std::log(totalFreq);
171 return static_cast<TOutput
>(jointEntropy / (entropyX + entropyY));