File: itkJointDomainImageToListAdaptor.h

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 (330 lines) | stat: -rw-r--r-- 12,586 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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkJointDomainImageToListAdaptor.h,v $
  Language:  C++
  Date:      $Date: 2009-03-04 15:23:50 $
  Version:   $Revision: 1.15 $

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

#include "itkMacro.h"
#include "itkFixedArray.h"
#include "itkPoint.h"
#include "itkPixelTraits.h"
#include "itkImageToListAdaptor.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkEuclideanDistance.h"
#include "itkListSample.h"

namespace itk { 
namespace Statistics {

/** \class ImageJointDomainTraits
 *  \brief This class provides the type defintion for the measurement
 *  vector in the joint domain (range domain -- pixel values + spatial
 *  domain -- pixel's physical coordinates).
 *
 * \sa JointDomainImageToListAdaptor
 */
template< class TImage >
struct ImageJointDomainTraits
{
  typedef ImageJointDomainTraits                     Self;
  typedef PixelTraits< typename TImage::PixelType >  PixelTraitsType;
  typedef typename PixelTraitsType::ValueType        RangeDomainMeasurementType;
  typedef typename TImage::IndexType::IndexValueType IndexValueType;

  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
  itkStaticConstMacro(Dimension, 
                      unsigned int, 
                      TImage::ImageDimension +
                      PixelTraitsType::Dimension );

  typedef float                              CoordinateRepType;
  typedef Point< CoordinateRepType, itkGetStaticConstMacro(ImageDimension) > 
                                             PointType;
  typedef JoinTraits< RangeDomainMeasurementType, CoordinateRepType > 
                                             JoinTraitsType;
  typedef typename JoinTraitsType::ValueType MeasurementType;
  typedef FixedArray< MeasurementType, itkGetStaticConstMacro(Dimension) >
                                             MeasurementVectorType;
}; // end of ImageJointDomainTraits

/** \class JointDomainImageToListAdaptor
 *  \brief This adaptor returns measurement vectors composed of an
 *  image pixel's range domain value (pixel value) and spatial domain
 *  value (pixel's physical coordiantes).
 *
 * This class is a derived class of the ImageToListAdaptor. This class
 * overrides the GetMeasurementVector method. The GetMeasurementVector
 * returns a measurement vector that consist of a pixel's physical
 * coordinates and intensity value. For example, if the image
 * dimension is 3, and the pixel value is two component vector, the
 * measurement vector is a 5 component vector. The first three
 * component will be x, y, z physical coordinates (not index) and the
 * rest two component is the pixel values. The type of component is
 * float or which is determined by the ImageJointDomainTraits
 * class. When the pixel value type is double, the component value
 * type of a measurement vector is double. In other case, the
 * component value type is float becase the physical coordinate value
 * type is float. Since the measurment vector is a composition of
 * spatial domain and range domain, for many statistical analysis, we
 * want to normalize the values from both domains. For this purpose,
 * there is the SetNormalizationFactors method. With the above example
 * (5 component measurement vector), you can specify a 5 component
 * normalization factor array. With such factors, the
 * GetMeasurementVector method returns a measurement vector whose each
 * component is divided by the corresponding component of the factor array. 
 *
 * \sa Sample, ListSampleBase, ImageToListAdaptor
 */

template < class TImage >
class ITK_EXPORT JointDomainImageToListAdaptor 
  : public ImageToListAdaptor< 
  TImage, 
  typename ImageJointDomainTraits< TImage >::MeasurementVectorType >
{
public:
  typedef ImageJointDomainTraits< TImage >    ImageJointDomainTraitsType;
  typedef typename ImageJointDomainTraitsType::MeasurementVectorType
                                              MeasurementVectorType;
  typedef typename ImageJointDomainTraitsType::MeasurementType
                                              MeasurementType;
  typedef typename ImageJointDomainTraitsType::RangeDomainMeasurementType
                                              RangeDomainMeasurementType;
  typedef typename ImageJointDomainTraitsType::PointType
                                              PointType;
  typedef typename ImageJointDomainTraitsType::CoordinateRepType 
                                              CoordinateRepType;
  /** Standard class typedefs */
  typedef JointDomainImageToListAdaptor       Self;
  typedef ImageToListAdaptor< TImage, MeasurementVectorType > 
                                              Superclass;
  typedef SmartPointer< Self >                Pointer;
  typedef SmartPointer<const Self>            ConstPointer;
  
  /** Run-time type information (and related methods). */
  itkTypeMacro(JointDomainImageToListAdaptor, ImageToListAdaptor);
  
  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  
  /** the number of components in a measurement vector */
  itkStaticConstMacro(MeasurementVectorSize, 
                      unsigned int, 
                      ImageJointDomainTraitsType::Dimension);
  
  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;

  virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s ) 
    {
    // Measurement vector size for this class is fixed as the pixel's 
    // dimension. This method should have no effect
    if( s != MeasurementVectorSize )
      {
      itkExceptionMacro( << "Cannot set measurement vector size of "
          << " JointDomainImageToListAdaptor to " << s );
      }
    }

  MeasurementVectorSizeType GetMeasurementVectorSize() const
    {
    return MeasurementVectorSize;
    } 
 
  /** typedefs for Measurement vector, measurement, 
   * Instance Identifier, frequency, size, size element value */
  typedef typename Superclass::FrequencyType      FrequencyType;
  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;

  typedef typename TImage::IndexType                  ImageIndexType;
  typedef typename TImage::IndexType::IndexValueType  ImageIndexValueType;
  typedef typename TImage::SizeType                   ImageSizeType;
  typedef typename TImage::RegionType                 ImageRegionType;
  typedef ImageRegionConstIteratorWithIndex< TImage > ImageIteratorType;

  typedef MeasurementVectorType ValueType;
 
  itkStaticConstMacro(RangeDomainDimension, 
                      unsigned int, 
                      itk::PixelTraits< 
                      typename TImage::PixelType >::Dimension);

  typedef FixedArray< RangeDomainMeasurementType, 
                      itkGetStaticConstMacro( RangeDomainDimension ) > 
  RangeDomainMeasurementVectorType;

  typedef std::vector< InstanceIdentifier > InstanceIdentifierVectorType; 

  typedef FixedArray< float, itkGetStaticConstMacro(MeasurementVectorSize) >
  NormalizationFactorsType;

  /** Sets the normalization factors */
  void SetNormalizationFactors(NormalizationFactorsType& factors);

  /** Gets the measurement vector specified by the instance
   * identifier. This method overrides superclass method. */
  inline const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;

  /** Computes the image region (rectangular) that enclose the ball
   * defined by the mv (center) and the radius. */
  inline void ComputeRegion(const MeasurementVectorType& mv, 
                            const double radius,
                            ImageRegionType& region) const;

  /** Fills he result id vectors with instances that fall within a
   * ball specified by the mv (center) and radius. This method utilizes
   * the ComputRegion */
  inline void Search(const MeasurementVectorType& mv, 
                     const double radius, 
                     InstanceIdentifierVectorType& result) const;

protected:
  JointDomainImageToListAdaptor();
  virtual ~JointDomainImageToListAdaptor() {}
  void PrintSelf(std::ostream& os, Indent indent) const;  

private:
  JointDomainImageToListAdaptor(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented

  NormalizationFactorsType m_NormalizationFactors;

  mutable MeasurementVectorType            m_TempVector;
  mutable PointType                        m_TempPoint;
  mutable ImageIndexType                   m_TempIndex;
  mutable RangeDomainMeasurementVectorType m_TempRangeVector;
}; // end of class JointDomainImageToListAdaptor

template < class TImage >
inline const typename JointDomainImageToListAdaptor< TImage >::MeasurementVectorType &
JointDomainImageToListAdaptor< TImage >
::GetMeasurementVector(const InstanceIdentifier &id) const
{
  m_TempIndex = this->GetImage()->ComputeIndex( id );
  
  this->GetImage()->TransformIndexToPhysicalPoint( m_TempIndex, m_TempPoint );
  
  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
    {
    m_TempVector[i] = m_TempPoint[i] / m_NormalizationFactors[i];
    }
  
  if( this->GetUseBuffer() )
    {
    m_TempRangeVector =  
      *(reinterpret_cast<const RangeDomainMeasurementVectorType* >
        (&(*this->GetPixelContainer())[id]));
    }
  else
    {
    m_TempRangeVector = 
      *(reinterpret_cast< const RangeDomainMeasurementVectorType* >
        (&(this->GetImage()->GetPixel( m_TempIndex ) ) ) );
    }
  
  for ( unsigned int i = TImage::ImageDimension; i < MeasurementVectorType::Length; ++i )
    {
    m_TempVector[i] = m_TempRangeVector[i - TImage::ImageDimension] 
      / m_NormalizationFactors[i];
    }
  
  return m_TempVector;
}

template < class TImage >
inline void
JointDomainImageToListAdaptor< TImage >
::ComputeRegion(const MeasurementVectorType& mv, 
                const double radius,
                ImageRegionType& region) const
{
  ImageIndexType beginIndex;
  ImageSizeType size;

  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
    {
    m_TempPoint[i] = m_NormalizationFactors[i] * (mv[i] - radius);
    size[i] = (unsigned long)(2.0 * m_NormalizationFactors[i] * radius 
                              / this->GetImage()->GetSpacing()[i]);
    }

  this->GetImage()->TransformPhysicalPointToIndex(m_TempPoint , beginIndex );

  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
    {
    if ( beginIndex[i] < this->GetImageBeginIndex()[i] )
      {
      beginIndex[i] = this->GetImageBeginIndex()[i];
      size[i] -= (this->GetImageBeginIndex()[i] - beginIndex[i]);
      }

    if ( static_cast<typename ImageIndexType::IndexValueType>(beginIndex[i] + size[i] - 1) > this->GetImageEndIndex()[i] )
      {
      size[i] = this->GetImageEndIndex()[i] - beginIndex[i] + 1;
      }
    }
  
  region.SetIndex( beginIndex );
  region.SetSize( size );
}

template < class TImage >
inline void
JointDomainImageToListAdaptor< TImage >
::Search(const MeasurementVectorType& mv,
         const double radius,
         InstanceIdentifierVectorType& result) const
{
  ImageRegionType region;
  this->ComputeRegion( mv, radius, region );

  result.clear();
  ImageIteratorType iter( this->GetImage(), region );
  iter.GoToBegin();
  double squaredRadius = radius * radius;
  InstanceIdentifier instanceID;
  while ( !iter.IsAtEnd() )
    {
    instanceID  = this->GetImage()->ComputeOffset(iter.GetIndex());
    m_TempVector = this->GetMeasurementVector( instanceID );
    bool isWithinRange = true;
    double sum = 0.0;
    for ( unsigned int i = 0; i < MeasurementVectorSize; ++i )
      {
      const double temp = (m_TempVector[i] - mv[i]);
      sum += temp * temp;
      if ( sum > squaredRadius )
        {
        isWithinRange = false;
        break;
        }
      }

    if ( isWithinRange )
      {
      result.push_back(instanceID);
      }
    ++iter;
    }
}

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

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkJointDomainImageToListAdaptor.txx"
#endif

#endif