This file is indexed.

/usr/include/ITK-4.9/itkOtsuMultipleThresholdsCalculator.hxx is in libinsighttoolkit4-dev 4.9.0-4ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkOtsuMultipleThresholdsCalculator_hxx
#define itkOtsuMultipleThresholdsCalculator_hxx

#include "itkMath.h"
#include "itkOtsuMultipleThresholdsCalculator.h"

namespace itk
{
template< typename TInputHistogram >
OtsuMultipleThresholdsCalculator< TInputHistogram >
::OtsuMultipleThresholdsCalculator()
{
  m_NumberOfThresholds = 1;
  m_Output.resize(m_NumberOfThresholds);
  m_ValleyEmphasis = false;
  std::fill(m_Output.begin(), m_Output.end(), NumericTraits< MeasurementType >::ZeroValue());
}

template< typename TInputHistogram >
const typename OtsuMultipleThresholdsCalculator< TInputHistogram >::OutputType &
OtsuMultipleThresholdsCalculator< TInputHistogram >
::GetOutput()
{
  return m_Output;
}

/**
 * Increment the thresholds of one position along the histogram
 */
template< typename TInputHistogram >
bool
OtsuMultipleThresholdsCalculator< TInputHistogram >
::IncrementThresholds(InstanceIdentifierVectorType & thresholdIndexes,
                      MeanType globalMean,
                      MeanVectorType & classMean,
                      FrequencyVectorType & classFrequency)
{
  typename TInputHistogram::ConstPointer histogram = this->GetInputHistogram();

  const SizeValueType numberOfHistogramBins = histogram->Size();
  const SizeValueType numberOfClasses = classMean.size();

  unsigned int k;
  int          j;

  // from the upper threshold down
  for ( j = static_cast< int >( m_NumberOfThresholds - 1 ); j >= 0; j-- )
    {
    // if this threshold can be incremented (i.e. we're not at the end of the
    // histogram)
    if ( thresholdIndexes[j] < numberOfHistogramBins - 2 - ( m_NumberOfThresholds - 1 - j ) )
      {
      // increment it and update mean and frequency of the class bounded by the
      // threshold
      ++thresholdIndexes[j];

      const MeanType meanOld = classMean[j];
      const FrequencyType freqOld = classFrequency[j];

      classFrequency[j] += histogram->GetFrequency(thresholdIndexes[j]);

      if ( NumericTraits< FrequencyType >::IsPositive(classFrequency[j]) )
        {
        classMean[j] = ( meanOld * static_cast< MeanType >( freqOld )
                         + static_cast< MeanType >( histogram->GetMeasurementVector(thresholdIndexes[j])[0] )
                         * static_cast< MeanType >( histogram->GetFrequency(thresholdIndexes[j]) ) )
                       / static_cast< MeanType >( classFrequency[j] );
        }
      else
        {
        classMean[j] = NumericTraits< MeanType >::ZeroValue();
        }

      // set higher thresholds adjacent to their previous ones, and update mean
      // and frequency of the respective classes
      for ( k = j + 1; k < m_NumberOfThresholds; k++ )
        {
        thresholdIndexes[k] = thresholdIndexes[k - 1] + 1;
        classFrequency[k] = histogram->GetFrequency(thresholdIndexes[k]);
        if ( NumericTraits< FrequencyType >::IsPositive(classFrequency[k]) )
          {
          classMean[k] = static_cast< MeanType >( histogram->GetMeasurementVector(thresholdIndexes[k])[0] );
          }
        else
          {
          classMean[k] = NumericTraits< MeanType >::ZeroValue();
          }
        }

      // update mean and frequency of the highest class
      classFrequency[numberOfClasses - 1] = histogram->GetTotalFrequency();
      classMean[numberOfClasses - 1] = globalMean * histogram->GetTotalFrequency();

      for ( k = 0; k < numberOfClasses - 1; k++ )
        {
        classFrequency[numberOfClasses - 1] -= classFrequency[k];
        classMean[numberOfClasses - 1] -= classMean[k] * static_cast< MeanType >( classFrequency[k] );
        }

      if ( NumericTraits< FrequencyType >::IsPositive(classFrequency[numberOfClasses - 1]) )
        {
        classMean[numberOfClasses - 1] /= static_cast< MeanType >( classFrequency[numberOfClasses - 1] );
        }
      else
        {
        classMean[numberOfClasses - 1] = NumericTraits< MeanType >::ZeroValue();
        }

      // exit the for loop if a threshold has been incremented
      break;
      }
    else  // if this threshold can't be incremented
      {
      // if it's the lowest threshold
      if ( j == 0 )
        {
        // we couldn't increment because we're done
        return false;
        }
      }
    }
  // we incremented
  return true;
}

/**
 * Compute Otsu's thresholds
 */
template< typename TInputHistogram >
void
OtsuMultipleThresholdsCalculator< TInputHistogram >
::Compute()
{
  typename TInputHistogram::ConstPointer histogram = this->GetInputHistogram();

  // TODO: as an improvement, the class could accept multi-dimensional
  // histograms
  // and the user could specify the dimension to apply the algorithm to.
  if ( histogram->GetSize().Size() != 1 )
    {
    itkExceptionMacro(<< "Histogram must be 1-dimensional.");
    }

  // compute global mean
  typename TInputHistogram::ConstIterator iter = histogram->Begin();
  typename TInputHistogram::ConstIterator end = histogram->End();

  MeanType      globalMean = NumericTraits< MeanType >::ZeroValue();
  const FrequencyType globalFrequency = histogram->GetTotalFrequency();
  while ( iter != end )
    {
    globalMean += static_cast< MeanType >( iter.GetMeasurementVector()[0] )
                  * static_cast< MeanType >( iter.GetFrequency() );
    ++iter;
    }
  globalMean /= static_cast< MeanType >( globalFrequency );

  SizeValueType numberOfClasses = m_NumberOfThresholds + 1;

  // initialize thresholds
  InstanceIdentifierVectorType thresholdIndexes(m_NumberOfThresholds);

  SizeValueType j;
  for ( j = 0; j < m_NumberOfThresholds; j++ )
    {
    thresholdIndexes[j] = j;
    }

  InstanceIdentifierVectorType maxVarThresholdIndexes = thresholdIndexes;

  // compute frequency and mean of initial classes
  FrequencyType       freqSum = NumericTraits< FrequencyType >::ZeroValue();
  FrequencyVectorType classFrequency(numberOfClasses);
  for ( j = 0; j < numberOfClasses - 1; j++ )
    {
    classFrequency[j] = histogram->GetFrequency(thresholdIndexes[j]);
    freqSum += classFrequency[j];
    }
  classFrequency[numberOfClasses - 1] = globalFrequency - freqSum;

  // Convert the frequencies to probabilities (i.e. normalize the histogram).
  SizeValueType histSize = histogram->GetSize()[0];
  WeightVectorType imgPDF(histSize);
  for ( j = 0; j < histSize; j++ )
    {
      imgPDF[j] = (WeightType)histogram->GetFrequency(j) / (WeightType)globalFrequency;
    }

  MeanType       meanSum = NumericTraits< MeanType >::ZeroValue();
  MeanVectorType classMean(numberOfClasses);
  for ( j = 0; j < numberOfClasses - 1; j++ )
    {
    if ( NumericTraits< FrequencyType >::IsPositive(classFrequency[j]) )
      {
      classMean[j] = static_cast< MeanType >( histogram->GetMeasurementVector(j)[0] );
      }
    else
      {
      classMean[j] = NumericTraits< MeanType >::ZeroValue();
      }
    meanSum += classMean[j] * static_cast< MeanType >( classFrequency[j] );
    }

  if ( NumericTraits< FrequencyType >::IsPositive(classFrequency[numberOfClasses - 1]) )
    {
    classMean[numberOfClasses
              - 1] =
      ( globalMean * static_cast< MeanType >( globalFrequency )
       - meanSum ) / static_cast< MeanType >( classFrequency[numberOfClasses - 1] );
    }
  else
    {
    classMean[numberOfClasses - 1] = NumericTraits< MeanType >::ZeroValue();
    }

  //
  // The "volatile" modifier is used here for preventing the variable from
  // being kept in 80 bit FPU registers when using 32-bit x86 processors with
  // SSE instructions disabled. A case that arised in the Debian 32-bits
  // distribution.
  //
#ifndef ITK_COMPILER_SUPPORTS_SSE2_32
  volatile VarianceType maxVarBetween = NumericTraits< VarianceType >::ZeroValue();
#else
  VarianceType maxVarBetween = NumericTraits< VarianceType >::ZeroValue();
#endif
  //
  // The introduction of the "volatile" modifier forces the compiler to keep
  // the variable in memory and therefore store it in the IEEE float/double
  // format. In this way making numerical results consistent across platforms.
  //

  for ( j = 0; j < numberOfClasses; j++ )
    {
    maxVarBetween += (static_cast< VarianceType >( classFrequency[j] ))
      * static_cast< VarianceType >( ( classMean[j] ) * ( classMean[j] ) );
    }
  maxVarBetween /= static_cast< VarianceType >( globalFrequency );

  // Sum the relevant weights for valley emphasis
  WeightType valleyEmphasisFactor = NumericTraits< WeightType >::ZeroValue();
  if (m_ValleyEmphasis)
    {
    for ( j = 0; j < numberOfClasses - 1; j++ )
      {
      valleyEmphasisFactor = imgPDF[thresholdIndexes[j]];
      }
    valleyEmphasisFactor = 1.0 - valleyEmphasisFactor;
    maxVarBetween = maxVarBetween * valleyEmphasisFactor;
    }

  // explore all possible threshold configurations and choose the one that
  // yields maximum between-class variance
  while ( Self::IncrementThresholds(thresholdIndexes, globalMean, classMean, classFrequency) )
    {

    //
    // The "volatile" modifier is used here for preventing the variable from
    // being kept in 80 bit FPU registers when using 32-bit x86 processors with
    // SSE instructions disabled. A case that arised in the Debian 32-bits
    // distribution.
    //
#ifndef ITK_COMPILER_SUPPORTS_SSE2_32
    volatile VarianceType varBetween = NumericTraits< VarianceType >::ZeroValue();
#else
    VarianceType varBetween = NumericTraits< VarianceType >::ZeroValue();
#endif
    //
    // The introduction of the "volatile" modifier forces the compiler to keep
    // the variable in memory and therefore store it in the IEEE float/double
    // format. In this way making numerical results consistent across platforms.
    //

    for ( j = 0; j < numberOfClasses; j++ )
      {
      // The true between-class variance \sigma_B^2 for any number of classes is defined as:
      // \sigma_B^2 = \sum_{k=1}^{M} \omega_k (\mu_k - \mu_T)^2
      // where \omega_k = classFrequency[j]/globalFrequency is the probability of the class,
      // \mu_k = classMean[j] is the mean of the class,
      // \mu_T = globalMean is the overall mean,
      // and M is the number of classes.
      // However, in the paper "A Fast Algorithm for Multilevel Thresholding" by Liao, Chen, and Chung,
      // it was shown that this can be simplified to
      // (\sum_{k=1}^{M} \omega_k \mu_k^2) - \mu_T^2
      // Since we are looking for the argmax, the second term can be ignored because it is a constant, leading to the simpler
      // (\sum_{k=1}^{M} \omega_k \mu_k^2), which is what is implemented here.
      // Although this is no longer truly a "between class variance", we keep that name since it is only different by a constant.
      varBetween += (static_cast< VarianceType >( classFrequency[j] ))
              * static_cast< VarianceType >( ( classMean[j] ) * ( classMean[j] ) );
      }
    varBetween /= static_cast< VarianceType >( globalFrequency );

    if (m_ValleyEmphasis)
    {
      // Sum relevant weights to get valley emphasis factor
      valleyEmphasisFactor = NumericTraits< WeightType >::ZeroValue();
      for ( j = 0; j < numberOfClasses - 1; j++ )
      {
        valleyEmphasisFactor += imgPDF[thresholdIndexes[j]];
      }
      valleyEmphasisFactor = 1.0 - valleyEmphasisFactor;
      varBetween = varBetween * valleyEmphasisFactor;
    }

    const unsigned int maxUlps = 1;
    if ( varBetween > maxVarBetween &&
         !Math::FloatAlmostEqual( maxVarBetween, varBetween, maxUlps) )
      {
      maxVarBetween = varBetween;
      maxVarThresholdIndexes = thresholdIndexes;
      }
    }

  // copy corresponding bin max to threshold vector
  m_Output.resize(m_NumberOfThresholds);

  for ( j = 0; j < m_NumberOfThresholds; j++ )
    {
    m_Output[j] = histogram->GetMeasurement(maxVarThresholdIndexes[j],0);
    }
}

template< typename TInputHistogram >
void
OtsuMultipleThresholdsCalculator< TInputHistogram >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "NumberOfThresholds: " << m_NumberOfThresholds;

  os << indent << "Output: ";
  for ( SizeValueType j = 0; j < m_NumberOfThresholds; j++ )
    {
    os << m_Output[j] << " ";
    }
  os << std::endl;
}
} // end namespace itk

#endif