File: itkSpatialObjectToImageStatisticsCalculator.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 (259 lines) | stat: -rw-r--r-- 8,562 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
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
/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    itkSpatialObjectToImageStatisticsCalculator.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 __itkSpatialObjectToImageStatisticsCalculator_txx
#define __itkSpatialObjectToImageStatisticsCalculator_txx

#include "itkSpatialObjectToImageStatisticsCalculator.h"
#include "itkImageRegionConstIteratorWithIndex.h"

#ifdef ITK_USE_REVIEW_STATISTICS
#include "itkMeanSampleFilter.h"
#include "itkCovarianceSampleFilter.h"
#else
#include "itkMeanCalculator.h"
#include "itkCovarianceCalculator.h"
#endif

#include "itkImageMaskSpatialObject.h"

namespace itk
{ 
    
/** Constructor */
template<class TInputImage, class TInputSpatialObject, unsigned int TSampleDimension>
SpatialObjectToImageStatisticsCalculator<TInputImage,TInputSpatialObject,TSampleDimension>
::SpatialObjectToImageStatisticsCalculator()
{
  m_Image = NULL;
  m_SpatialObject = NULL;
  m_Mean.Fill(0);
  m_CovarianceMatrix.SetIdentity();
  m_SampleDirection = TSampleDimension-1;
  m_InternalImageTime = 0;
  m_InternalSpatialObjectTime = 0;
  m_Sum = 0;
  m_NumberOfPixels = 0;
  m_Sample = SampleType::New();
}


/** Compute Statistics from the Sample vector */
template<class TInputImage,class TInputSpatialObject, unsigned int TSampleDimension>
bool 
SpatialObjectToImageStatisticsCalculator<TInputImage,TInputSpatialObject,TSampleDimension>
::ComputeStatistics()
{
#ifdef ITK_USE_REVIEW_STATISTICS
  typedef itk::Statistics::MeanSampleFilter< SampleType > MeanAlgorithmType;
#else
  typedef itk::Statistics::MeanCalculator< SampleType > MeanAlgorithmType;
#endif
  
  typename MeanAlgorithmType::Pointer meanAlgorithm = MeanAlgorithmType::New();

#ifdef ITK_USE_REVIEW_STATISTICS
  meanAlgorithm->SetInput( m_Sample );
#else
  meanAlgorithm->SetInputSample( m_Sample );
#endif

  meanAlgorithm->Update();

#ifdef ITK_USE_REVIEW_STATISTICS
  typename MeanAlgorithmType::MeasurementVectorType mean = meanAlgorithm->GetMean();
#else
  typename MeanAlgorithmType::OutputType mean = 
                                *(meanAlgorithm->GetOutput());
#endif

  for( unsigned int i=0; i< SampleDimension; i++ )
    {
    m_Mean[i] = mean[i];
    }

#ifdef ITK_USE_REVIEW_STATISTICS
  typedef itk::Statistics::CovarianceSampleFilter< SampleType > CovarianceAlgorithmType;
#else
  typedef itk::Statistics::CovarianceCalculator< SampleType >   CovarianceAlgorithmType;
#endif
  
  typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = 
    CovarianceAlgorithmType::New();

#ifdef ITK_USE_REVIEW_STATISTICS
  covarianceAlgorithm->SetInput( m_Sample );
#else
  covarianceAlgorithm->SetInputSample( m_Sample );
  covarianceAlgorithm->SetMean( meanAlgorithm->GetOutput() );
#endif

  covarianceAlgorithm->Update();
  
#ifdef ITK_USE_REVIEW_STATISTICS
  typename CovarianceAlgorithmType::MatrixType covarianceMatrix = covarianceAlgorithm->GetCovarianceMatrix();
#else
  typename CovarianceAlgorithmType::OutputType covarianceMatrix
      = *(covarianceAlgorithm->GetOutput());
#endif

  for( unsigned int i=0; i< covarianceMatrix.Rows(); i++ )
    {
    for( unsigned int j=0; j< covarianceMatrix.Rows(); j++ )
      {
      m_CovarianceMatrix(i,j) = covarianceMatrix(i,j);
      }
    }

  return true;
}


/** */
template<class TInputImage,class TInputSpatialObject, unsigned int TSampleDimension>
void
SpatialObjectToImageStatisticsCalculator<TInputImage,TInputSpatialObject,TSampleDimension>
::Update(void)
{
  if(!m_Image || !m_SpatialObject)
    {
    itkExceptionMacro("SpatialObjectToImageStatisticsCalculator: Please set the image AND the spatial object first.");
    }

  // Update only if the image or the spatial object has been modified
  if((m_Image->GetMTime() == m_InternalImageTime)
     &&
     (m_SpatialObject->GetMTime() == m_InternalSpatialObjectTime) 
    )
    {
    return; // No need to update
    }

  m_InternalImageTime = m_Image->GetMTime();
  m_InternalSpatialObjectTime = m_SpatialObject->GetMTime();
  
  m_Sample = SampleType::New();
  m_Sample->SetMeasurementVectorSize( SampleDimension );
   
  m_NumberOfPixels = 0;
  m_Sum = 0;

  // If this is an ImageMaskSpatialObject we cannot use the flood filled iterator
  if(!strcmp(m_SpatialObject->GetTypeName(),"ImageMaskSpatialObject"))
    {
    typedef Image<unsigned char,itkGetStaticConstMacro(ObjectDimension)> MaskImageType;
    typedef ImageMaskSpatialObject<itkGetStaticConstMacro(ObjectDimension)> MaskSOType;

    // This apparently redundant construction is added here in order to 
    // force the SGI CC compiler to instantiate the symbols of the 
    // ImageMaskSpatialObject class. Otherwise, there is no readon for calling New().
    typename MaskSOType::Pointer maskSpatialObject = MaskSOType::New();

    maskSpatialObject = dynamic_cast< MaskSOType * >( m_SpatialObject.GetPointer() );

    typename MaskImageType::ConstPointer maskImage =  maskSpatialObject->GetImage();

    typedef ImageRegionConstIterator<MaskImageType> MaskIteratorType;
    MaskIteratorType it(maskImage,maskImage->GetLargestPossibleRegion());
    it.GoToBegin();
    while(!it.IsAtEnd())
      {
      if(it.Get()>0) // if inside the mask
        {
        IndexType ind = it.GetIndex();
        VectorType mv;
        mv[0] = m_Image->GetPixel(ind);
        m_Sum += static_cast< AccumulateType >(mv[0]);
        for(unsigned int i=1;i<itkGetStaticConstMacro(SampleDimension);i++)
          {
          ind[m_SampleDirection] += 1;
          mv[i]= m_Image->GetPixel(ind);
          m_Sum += static_cast< AccumulateType >(mv[i]);
          }
        m_Sample->PushBack( mv );
        m_NumberOfPixels++;
        }
      ++it;
      }
    }
  else
    {
    // Get the bounding box
    typename SpatialObjectType::BoundingBoxType::Pointer boundingBox;
    m_SpatialObject->ComputeBoundingBox();
    boundingBox = m_SpatialObject->GetBoundingBox();
    
    Point<double,itkGetStaticConstMacro(ObjectDimension)> pt;
    for(unsigned int i=0;i<itkGetStaticConstMacro(ObjectDimension);i++)
      {
      pt[i]=boundingBox->GetBounds()[i*2]+(boundingBox->GetBounds()[i*2+1]-boundingBox->GetBounds()[i*2])/2;
      }

    IndexType index;
    
    // We should remove the spacing and the origin of the image since the FloodFill iterator is
    // considering them.
    for(unsigned int i=0;i<itkGetStaticConstMacro(ObjectDimension);i++)
      {
      index[i]=(long int)((pt[i]-m_Image->GetOrigin()[i])/m_Image->GetSpacing()[i]);
      }

    IteratorType it = IteratorType(m_Image,m_SpatialObject,index);
    it.SetOriginInclusionStrategy();
    it.GoToBegin();

    while(!it.IsAtEnd())
      {
      IndexType ind = it.GetIndex();
      VectorType mv;
      mv[0] = it.Get();
      m_Sum += static_cast< AccumulateType >(mv[0]);
      for(unsigned int i=1;i<itkGetStaticConstMacro(SampleDimension);i++)
        {
        ind[m_SampleDirection] += 1;
        mv[i]= m_Image->GetPixel(ind);
        m_Sum += static_cast< AccumulateType >(mv[i]);
        }
      m_Sample->PushBack( mv );
      m_NumberOfPixels++;
      ++it;
      }
    }

  this->ComputeStatistics();
}

template<class TInputImage,class TInputSpatialObject, unsigned int TSampleDimension>
void
SpatialObjectToImageStatisticsCalculator<TInputImage,TInputSpatialObject,TSampleDimension>
::PrintSelf( std::ostream& os, Indent indent ) const
{
  Superclass::PrintSelf(os,indent);
  os << indent << "Image: " << m_Image << std::endl;
  os << indent << "SpatialObject: " << m_SpatialObject << std::endl;
  os << indent << "Mean: " << m_Mean << std::endl;
  os << indent << "Covariance Matrix: " << m_CovarianceMatrix << std::endl;
  os << indent << "Sum: " << m_Sum << std::endl;
  os << indent << "m_NumberOfPixels: " << m_NumberOfPixels << std::endl;
  os << indent << "Internal Image Time: " << m_InternalImageTime << std::endl;
  os << indent << "Internal Spatial Object Time: " << m_InternalSpatialObjectTime << std::endl;
  os << indent << "SampleDirection: " << m_SampleDirection << std::endl;
  os << indent << "Sample: " << m_Sample << std::endl;
}

} // end namespace itk

#endif