File: itkGreyLevelCooccurrenceMatrixTextureCoefficientsCalculator.txx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,672 kB
  • ctags: 85,253
  • sloc: cpp: 458,133; ansic: 196,222; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 428; csh: 220; perl: 193; xml: 20
file content (212 lines) | stat: -rw-r--r-- 7,586 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkGreyLevelCooccurrenceMatrixTextureCoefficientsCalculator.txx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkGreyLevelCooccurrenceMatrixTextureCoefficientsCalculator_txx
#define __itkGreyLevelCooccurrenceMatrixTextureCoefficientsCalculator_txx

#include "itkGreyLevelCooccurrenceMatrixTextureCoefficientsCalculator.h"

#include "itkNumericTraits.h"
#include "vnl/vnl_math.h"

namespace itk {
namespace Statistics {
    
template< class THistogram >
void
GreyLevelCooccurrenceMatrixTextureCoefficientsCalculator< THistogram >::
Compute( void )
{
  typedef typename HistogramType::Iterator HistogramIterator;
      
  // First, normalize the histogram if it doesn't look normalized.
  // This is one pass through the histogram.
  FrequencyType totalFrequency = m_Histogram->GetTotalFrequency();
  if ( (totalFrequency - NumericTraits<MeasurementType>::One) > 0.0001 )
    {
    // Doesn't look normalized:
    this->NormalizeHistogram();
    }
      
  // Now get the various means and variances. This is takes two passes
  // through the histogram.
  double pixelMean, marginalMean, marginalDevSquared, pixelVariance;
  this->ComputeMeansAndVariances(pixelMean, marginalMean, marginalDevSquared,
                                 pixelVariance);
      
            
  // Finally compute the texture features. Another one pass.
  m_Energy = m_Entropy = m_Correlation = m_InverseDifferenceMoment =
    m_Inertia = m_ClusterShade = m_ClusterProminence = m_HaralickCorrelation = 0;
      
  double pixelVarianceSquared = pixelVariance * pixelVariance;
  double log2 = vcl_log(2.);
  for (HistogramIterator hit = m_Histogram->Begin();
       hit != m_Histogram->End(); ++hit)
    {
    MeasurementType frequency = hit.GetFrequency();
    if (frequency == 0)
      {
      continue; // no use doing these calculations if we're just multiplying by zero.
      }
        
    IndexType index = m_Histogram->GetIndex(hit.GetInstanceIdentifier());
    m_Energy += frequency * frequency;
    m_Entropy -= (frequency > 0.0001) ? frequency * vcl_log(frequency) / log2 : 0;
    m_Correlation += ( (index[0] - pixelMean) * (index[1] - pixelMean) * frequency)
      / pixelVarianceSquared;
    m_InverseDifferenceMoment += frequency /
      (1.0 + (index[0] - index[1]) * (index[0] - index[1]) );
    m_Inertia += (index[0] - index[1]) * (index[0] - index[1]) * frequency;
    m_ClusterShade += vcl_pow((index[0] - pixelMean) + (index[1] - pixelMean), 3) *
      frequency;
    m_ClusterProminence += vcl_pow((index[0] - pixelMean) + (index[1] - pixelMean), 4) *
      frequency;
    m_HaralickCorrelation += index[0] * index[1] * frequency;
    }
      
  m_HaralickCorrelation = (m_HaralickCorrelation - marginalMean * marginalMean) /
    marginalDevSquared;
}
  

template< class THistogram >
void
GreyLevelCooccurrenceMatrixTextureCoefficientsCalculator< THistogram >::
NormalizeHistogram( void )
{
  typename HistogramType::Iterator hit;
  typename HistogramType::FrequencyType totalFrequency = 
    m_Histogram->GetTotalFrequency();
        
  for (hit = m_Histogram->Begin(); hit != m_Histogram->End(); ++hit)
    {
    hit.SetFrequency(hit.GetFrequency() / totalFrequency);
    }
}
      
template< class THistogram >
void
GreyLevelCooccurrenceMatrixTextureCoefficientsCalculator< THistogram >::
ComputeMeansAndVariances( double &pixelMean, double &marginalMean, 
                          double &marginalDevSquared, double &pixelVariance )
{
  // This function takes two passes through the histogram and two passes through
  // an array of the same length as a histogram axis. This could probably be
  // cleverly compressed to one pass, but it's not clear that that's necessary.
      
  typedef typename HistogramType::Iterator HistogramIterator;
      
  // Initialize everything
  typename HistogramType::SizeValueType binsPerAxis = m_Histogram->GetSize(0);
  double *marginalSums = new double[binsPerAxis];
  for (double *ms_It = marginalSums; 
       ms_It < marginalSums + binsPerAxis; ms_It++)
    {
    *ms_It = 0;
    }
  pixelMean = 0;
      
  // Ok, now do the first pass through the histogram to get the marginal sums
  // and compute the pixel mean
  HistogramIterator hit;
  for (hit = m_Histogram->Begin(); hit != m_Histogram->End(); ++hit)
    {
    MeasurementType frequency = hit.GetFrequency();
    IndexType index = m_Histogram->GetIndex(hit.GetInstanceIdentifier());
    pixelMean += index[0] * frequency;
    marginalSums[index[0]] += frequency;
    }
      
  /*  Now get the mean and deviaton of the marginal sums.
          Compute incremental mean and SD, a la Knuth, "The  Art of Computer 
          Programming, Volume 2: Seminumerical Algorithms",  section 4.2.2. 
          Compute mean and standard deviation using the recurrence relation:
          M(1) = x(1), M(k) = M(k-1) + (x(k) - M(k-1) ) / k
          S(1) = 0, S(k) = S(k-1) + (x(k) - M(k-1)) * (x(k) - M(k))
          for 2 <= k <= n, then
          sigma = vcl_sqrt(S(n) / n) (or divide by n-1 for sample SD instead of
          population SD).
      */
  marginalMean = marginalSums[0];
  marginalDevSquared = 0;
  for (unsigned int arrayIndex = 1; arrayIndex < binsPerAxis; arrayIndex++)
    {
    int k = arrayIndex + 1;
    double M_k_minus_1 = marginalMean;
    double S_k_minus_1 = marginalDevSquared;
    double x_k = marginalSums[arrayIndex];
        
    double M_k = M_k_minus_1 + (x_k - M_k_minus_1) / k;
    double S_k = S_k_minus_1 + (x_k - M_k_minus_1) * (x_k - M_k);
        
    marginalMean = M_k;
    marginalDevSquared = S_k;
    }
  marginalDevSquared = marginalDevSquared / binsPerAxis;
      
  // OK, now compute the pixel variances.
  pixelVariance = 0;
  for (hit = m_Histogram->Begin(); hit != m_Histogram->End(); ++hit)
    {
    MeasurementType frequency = hit.GetFrequency();
    IndexType index = m_Histogram->GetIndex(hit.GetInstanceIdentifier());
    pixelVariance += (index[0] - pixelMean) * (index[0] - pixelMean) * frequency;
    }
  delete [] marginalSums;
}
    
template< class THistogram >
double
GreyLevelCooccurrenceMatrixTextureCoefficientsCalculator< THistogram >::
GetFeature(TextureFeatureName feature)
{
  switch(feature)
    {
    case Energy:
      return this->GetEnergy();
    case Entropy:
      return this->GetEntropy();
    case Correlation:
      return this->GetCorrelation();
    case InverseDifferenceMoment:
      return this->GetInverseDifferenceMoment();
    case Inertia:
      return this->GetInertia();
    case ClusterShade:
      return this->GetClusterShade();
    case ClusterProminence:
      return this->GetClusterProminence();
    case HaralickCorrelation:
      return this->GetHaralickCorrelation();
    default:
      return 0;
    }
}
      
template< class THistogram >
void
GreyLevelCooccurrenceMatrixTextureCoefficientsCalculator< THistogram >::
PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os,indent);
}
    
} // end of namespace Statistics 
} // end of namespace itk 


#endif