Orfeo Toolbox  3.16
itkKullbackLeiblerCompareHistogramImageToImageMetric.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkKullbackLeiblerCompareHistogramImageToImageMetric.txx,v $
5  Language: C++
6  Date: $Date: 2006-03-19 04:36:54 $
7  Version: $Revision: 1.4 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkKullbackLeiblerCompareHistogramImageToImageMetric_txx
18 #define __itkKullbackLeiblerCompareHistogramImageToImageMetric_txx
19 
21 #include "itkHistogram.h"
22 
23 // Todo: need to access Use_Padding in parent. Make in protected
24 // need to figure out what to do when "stuff" is not in histogram
25 // kernel function?
26 
27 namespace itk
28 {
29 template <class TFixedImage, class TMovingImage>
32 {
33  m_Epsilon = 1e-12; // should be smaller than 1/numBins^2
34 }
35 
36 template <class TFixedImage, class TMovingImage>
37 void
40 {
41  Superclass::Initialize();
42 }
43 
44 template <class TFixedImage, class TMovingImage>
46  TMovingImage>::MeasureType
48  TMovingImage>
49 ::EvaluateMeasure(HistogramType& histogram) const
50 {
51  // Two terms.
52  // First the term that measures the entropy of the term
53  // p(x,y) log p(x,y) - p(x,y) log q(x,y)
54 
55  MeasureType KullbackLeibler = NumericTraits<MeasureType>::Zero;
56 
57  HistogramIteratorType measured_it = histogram.Begin();
58  HistogramIteratorType measured_end = histogram.End();
59 
60  HistogramIteratorType training_it = this->GetTrainingHistogram()->Begin();
61  HistogramIteratorType training_end = this->GetTrainingHistogram()->End();
62 
63  while (measured_it != measured_end)
64  {
65  // Every bin gets epsilon added to it
66  double TrainingFreq = training_it.GetFrequency()+m_Epsilon;
67  double MeasuredFreq = measured_it.GetFrequency()+m_Epsilon;
68 
69  KullbackLeibler += MeasuredFreq*vcl_log(MeasuredFreq/TrainingFreq);
70 
71  ++measured_it;
72  ++training_it;
73  }
74 
75  if (training_it != training_end)
76  itkWarningMacro("The Measured and Training Histograms have different number of bins.");
77 
78  // Get the total frequency for each histogram.
79  HistogramFrequencyType totalTrainingFreq = this->GetTrainingHistogram()->GetTotalFrequency();
80  HistogramFrequencyType totalMeasuredFreq = histogram.GetTotalFrequency();
81 
82  // The actual number of total frequency is a bit larger
83  // than the number of counts because we add m_Epsilon to every bin
84  double AdjustedTotalTrainingFreq = totalTrainingFreq +
85  this->GetHistogramSize()[0]*this->GetHistogramSize()[1]*m_Epsilon;
86  double AdjustedTotalMeasuredFreq = totalMeasuredFreq +
87  this->GetHistogramSize()[0]*this->GetHistogramSize()[1]*m_Epsilon;
88 
89  KullbackLeibler = KullbackLeibler/static_cast<MeasureType>(AdjustedTotalMeasuredFreq)
90  - vcl_log(AdjustedTotalMeasuredFreq/AdjustedTotalTrainingFreq);
91 
92  return KullbackLeibler;
93 }
94 
95 template <class TFixedImage, class TMovingImage>
97 PrintSelf(std::ostream& os, Indent indent) const
98 {
99  Superclass::PrintSelf(os, indent);
100 
101  os << indent << "Epsilon: " << m_Epsilon << std::endl;
102 }
103 
104 
105 } // End namespace itk
106 
107 #endif // itkKullbackLeiblerCompareHistogramImageToImageMetric_txx

Generated at Sat Feb 2 2013 23:47:32 for Orfeo Toolbox with doxygen 1.8.1.1