File: itkSampleToHistogramProjectionFilter.txx

package info (click to toggle)
insighttoolkit 3.18.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 110,432 kB
  • ctags: 74,559
  • sloc: cpp: 412,627; ansic: 196,210; fortran: 28,000; python: 3,852; tcl: 2,005; sh: 1,186; java: 583; makefile: 458; csh: 220; perl: 193; xml: 20
file content (405 lines) | stat: -rwxr-xr-x 11,202 bytes parent folder | download
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    $RCSfile: itkSampleToHistogramProjectionFilter.txx,v $
Language:  C++
Date:      $Date: 2009-03-04 19:29:54 $
Version:   $Revision: 1.16 $

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 __itkSampleToHistogramProjectionFilter_txx
#define __itkSampleToHistogramProjectionFilter_txx

#include "itkSampleToHistogramProjectionFilter.h"

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

namespace itk { 
namespace Statistics {

template< class TInputSample, class THistogramMeasurement >
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SampleToHistogramProjectionFilter()
{
  m_Mean = 0;
  m_StandardDeviation = 0;
  m_Histogram = 0;
  m_ProjectionAxis = 0;
  m_HistogramBinOverlap = 0.0;
  m_MinimumFrequency = NumericTraits< FrequencyType >::Zero;
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::PrintSelf(std::ostream& os, Indent indent) const 
{
  Superclass::PrintSelf(os,indent);

  os << indent << "Histogram: ";
  if ( m_Histogram != 0 )
    {
    os << m_Histogram << std::endl;
    }
  else
    {
    os << "not set." << std::endl;
    }

  os << indent << "Mean: ";
  if ( m_Mean != 0 )
    {
    os << (*m_Mean) << std::endl;
    }
  else
    {
    os << "not set." << std::endl;
    }

  os << indent << "Standard Deviation: ";
  if ( m_StandardDeviation != 0 )
    {
    os << (*m_StandardDeviation) << std::endl;
    }
  else
    {
    os << "not set" << std::endl;
    }

  os << indent << "ProjectionAxis: ";
  if ( m_ProjectionAxis != 0 )
    { 
    os << (*m_ProjectionAxis) << std::endl;
    }
  else
    {
    os << "not set." << std::endl;
    }

  os << indent << "HistogramBinOverlap: "  << m_HistogramBinOverlap << std::endl;
  os << indent << "Minimum Frequency   " << m_MinimumFrequency << std::endl;
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SetHistogram(HistogramType* histogram) 
{
  if ( m_Histogram != histogram )
    {
    m_Histogram = histogram;
    this->Modified();
    }
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SetMean(MeanType* mean)
{
  if( this->GetMeasurementVectorSize() )
    {
    if( mean->size() != this->GetMeasurementVectorSize() )
      {
      itkExceptionMacro( << "Size of measurement vectors in the sample must be"
         << " the same as the size of the mean." );
      }
    }
  else
    {
    this->SetMeasurementVectorSize( mean->size() );
    }

  
  if ( m_Mean != mean )
    {
    m_Mean = mean;
    this->Modified();
    }
}

template< class TInputSample, class THistogramMeasurement >
typename SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >::MeanType*
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::GetMean()
{
  return m_Mean; 
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SetStandardDeviation(double* value)
{
  if ( m_StandardDeviation != value )
    {
    m_StandardDeviation = value;
    this->Modified();
    }
}

template< class TInputSample, class THistogramMeasurement >
double*
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::GetStandardDeviation()
{ 
  return m_StandardDeviation; 
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SetProjectionAxis(ArrayType* axis)
{ 
  if( this->GetMeasurementVectorSize() )
    {
    if( axis->GetSize() != this->GetMeasurementVectorSize() )
      {
      itkExceptionMacro( << "Size of measurement vectors in the sample must be"
         << " the same as the size of the projection axis." );
      }
    }
  else
    {
    this->SetMeasurementVectorSize( axis->GetSize() );
    }
  
  if ( m_ProjectionAxis != axis )
    {
    m_ProjectionAxis = axis;
    this->Modified();
    }
}

template< class TInputSample, class THistogramMeasurement >
typename SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >::ArrayType*
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::GetProjectionAxis()
{
  return m_ProjectionAxis; 
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::SetHistogramBinOverlap(double overlap)
{ 
  if ( m_HistogramBinOverlap != overlap )
    {
    m_HistogramBinOverlap = overlap;
    this->Modified();
    }
}

template< class TInputSample, class THistogramMeasurement >
inline float
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::CalculateOverlap(int binId,
                   float dotProduct, float scale,
                   float marginalDistance,
                   bool firstHalf)
{
  float minWeight;
  float maxWeight;

  float binMin = m_Histogram->GetBinMin(0, binId);
  float binMax = m_Histogram->GetBinMax(0, binId);
  
  if (firstHalf)
    {
    minWeight = (dotProduct - binMin) / ((binMax - binMin) / 2.0);
    }
  else
    {
    minWeight = (dotProduct - binMin) / 
      ((float(m_Histogram->GetBinMax(0, binId - 1)) - 
        float(m_Histogram->GetBinMin(0, binId - 1))) / 2.0);
    }

  if (minWeight > -1.0)
    {
    if (firstHalf)
      {
      maxWeight = (binMax - dotProduct) /
        ((float(m_Histogram->GetBinMax(0, binId + 1)) - 
          float(m_Histogram->GetBinMin(0, binId + 1))) / 2.0);
      }
    else
      {
      maxWeight = 
        (binMax - dotProduct) / ((binMax - binMin) / 2.0);
      }
      
    if (maxWeight > -1.0)
      {
      if (minWeight < 1.0)
        {
        minWeight = 1.0 / (1.0 + vcl_exp(-minWeight / scale));
        }
      else
        {
        minWeight = 1.0;
        }
  
      if (maxWeight < 1.0)
        {
        maxWeight = 1.0 / (1.0 + vcl_exp(-maxWeight / scale));
        }
      else
        {
        maxWeight = 1.0;
        }
  
      return minWeight * maxWeight * marginalDistance;
      }
    }

  return m_MinimumFrequency;
}

template< class TInputSample, class THistogramMeasurement >
void
SampleToHistogramProjectionFilter< TInputSample, THistogramMeasurement >
::GenerateData()
{
  // Assert at run time that the given mean and ProjectionAxis have the same length as 
  // measurement vectors in the sample and that the size is non-zero.
  const MeasurementVectorSizeType measurementVectorSize = 
                            this->GetMeasurementVectorSize();
  if( !measurementVectorSize || 
      ( MeasurementVectorTraits::GetLength( *m_Mean ) != measurementVectorSize ) ||
      ( m_ProjectionAxis->Size() != measurementVectorSize ) )
    {
    itkExceptionMacro( << "Size of measurement vectors in the sample must be "
        << "the same as the size of the mean and the ProjectionAxis.");
    }
  
  
  typename HistogramType::Iterator h_iter = m_Histogram->Begin();
  typename HistogramType::Iterator h_last = m_Histogram->End();
  while (h_iter != h_last)
    {
    h_iter.SetFrequency(0.0);
    ++h_iter;
    }

  float scale = 1;

  if (m_HistogramBinOverlap)
    {
    scale = vcl_log(1.0 + m_HistogramBinOverlap / 10.0 );
    }

  typename HistogramType::InstanceIdentifier binId;
  unsigned int dimension;
  float coordinateDistance;
  float squaredDistance;
  float marginalDistance;
  float dotProduct;
  FrequencyType tempFrequency;
  FrequencyType frequency;

  MeasurementVectorType tempMeasurementVector;

  typename TInputSample::ConstIterator s_iter = this->GetInputSample()->Begin();
  typename TInputSample::ConstIterator s_last = this->GetInputSample()->End();

  unsigned long numberOfBins = (unsigned long) m_Histogram->Size();
  double extent = 
    vnl_math_abs( m_Histogram->GetBinMax(0, numberOfBins - 1UL) - 
                  m_Histogram->GetBinMin(0, 0) ) / 2.0;

  while (s_iter != s_last)
    {
    tempMeasurementVector = s_iter.GetMeasurementVector();
    squaredDistance = 0.0;
    dotProduct = 0.0;
    frequency = s_iter.GetFrequency();
    for (dimension = 0; dimension < measurementVectorSize; dimension++)
      {
      coordinateDistance = 
        tempMeasurementVector[dimension] - (*m_Mean)[dimension];
      squaredDistance += coordinateDistance * coordinateDistance;
      dotProduct += coordinateDistance * (*m_ProjectionAxis)[dimension];
      }

    marginalDistance = 
      vcl_sqrt(vnl_math_abs(squaredDistance - dotProduct * dotProduct)) /
      ((*m_StandardDeviation) * extent);

    dotProduct /= (*m_StandardDeviation);

    if (m_HistogramBinOverlap < 0.001)
      {
      if ( (marginalDistance <= 1) && 
           (dotProduct >= m_Histogram->GetBinMin(0, 0)) &&
           (dotProduct <= 
            m_Histogram->
            GetBinMax(0, numberOfBins - 1UL)) )
        {
        binId = 0;
        while ( (dotProduct > m_Histogram->GetBinMax(0, binId)) &&
                (binId < (numberOfBins - 1UL)) )
          {
          binId++;
          }
        m_Histogram->IncreaseFrequency(binId, frequency);
        }
      }
    else
      {
      marginalDistance = 1.0 - marginalDistance;
      if ( marginalDistance > -1.0 )
        {
        marginalDistance = 
          1.0 / (1.0 + vcl_exp(-marginalDistance / scale));
              
        for (binId = 0; binId <= (numberOfBins / 2UL); 
             binId++)
          {
          tempFrequency = 
            this->CalculateOverlap(binId, 
                                   dotProduct,
                                   scale,
                                   marginalDistance,
                                   true);
          if ( tempFrequency > m_MinimumFrequency )
            {
            m_Histogram->IncreaseFrequency(binId, tempFrequency * frequency);
            }
          } // end of for

        for (binId = numberOfBins / 2UL + 1UL; 
             binId < numberOfBins;
             binId++)
          {
          tempFrequency = 
            this->CalculateOverlap(binId, 
                                   dotProduct,
                                   scale,
                                   marginalDistance,
                                   false);

          if ( tempFrequency > m_MinimumFrequency )
            {
            m_Histogram->IncreaseFrequency(binId, tempFrequency * frequency);
            }
          } // end of for
        } // end of if (marginalDistance ...
      } // end of if (m_HistogramBinOverlap  ...
    ++s_iter;
    } // end of while
}

} // end of namespace Statistics 
} // end of namespace itk

#endif