File: itkImageToListAdaptor.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 (351 lines) | stat: -rw-r--r-- 10,724 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
/*=========================================================================

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

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

#include <typeinfo>

#include "itkImage.h"
#include "itkPixelTraits.h"
#include "itkListSampleBase.h"
#include "itkSmartPointer.h"
#include "itkImageRegionIterator.h"
#include "itkFixedArray.h"
#include "itkMacro.h"

namespace itk { 
namespace Statistics {

/** \class ImageToListAdaptor
 *  \brief This class provides ListSampleBase interfaces to ITK Image
 *
 * After calling SetImage( const Image * ) method to plug in the image object,
 * users can use Sample interfaces to access Image data.
 * However, the resulting data are a list of measurement vectors. The type of
 * data is measurement vector. For example, if the pixel type of Image object 
 * is STL vector< float > and each pixel has two different types of 
 * measurements, intensity and gradient magnitude, this adaptor has
 * measurement vector of type ITK Point< float, 2>, and one element of the Point
 * is intensity and the other is gradient magnitude.
 *
 * There are two concepts of dimensions for this container. One is for Image 
 * object, and the other is for measurement vector dimension.
 * Only when using ITK Index to access data, the former concept is applicable
 * Otherwise, dimensions means dimensions of measurement vectors. 
 *
 * From the above example, there were two elements in a pixel and each pixel
 * provides [] operator for accessing its elements. However, in many cases,
 * The pixel might be a scalar value such as int or float. In this case,
 * The pixel doesn't support [] operator. To deal with this problem,
 * This class has two companion classes, ScalarAccessor and VectorAccessor.
 * If the pixel type is a scalar type, then you don't have change the third
 * template argument. If you have pixel type is vector one and supports
 * [] operator, then replace third argument with VectorAccessor
 *
 * \sa Sample, ListSampleBase
 */

template < class TImage,
           class TMeasurementVector = 
           ITK_TYPENAME TImage::PixelType >
class ITK_EXPORT ImageToListAdaptor : 
    public ListSampleBase< TMeasurementVector >
{
public:
  /** Standard class typedefs */
  typedef ImageToListAdaptor                   Self;
  typedef ListSampleBase< TMeasurementVector > Superclass;
  typedef SmartPointer< Self >                 Pointer;
  typedef SmartPointer<const Self>             ConstPointer;
  
  /** Run-time type information (and related methods). */
  itkTypeMacro(ImageToListAdaptor, ListSampleBase);
  
  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  
  /** Image typedefs */
  typedef TImage                                         ImageType;
  typedef typename ImageType::Pointer                    ImagePointer;
  typedef typename ImageType::ConstPointer               ImageConstPointer;
  typedef typename ImageType::IndexType                  IndexType;
  typedef typename ImageType::PixelType                  PixelType;
  typedef typename ImageType::PixelContainerConstPointer PixelContainerConstPointer;
  typedef typename ImageType::PixelContainer::ElementIdentifier 
                                                         InstanceIdentifier;
  
  /** Image Iterator typedef support */
  typedef ImageRegionIterator< ImageType > IteratorType; 
  typedef PixelTraits< typename TImage::PixelType > PixelTraitsType;

  /** Superclass typedefs for Measurement vector, measurement, 
   * Instance Identifier, frequency, size, size element value */
  typedef typename PixelTraitsType::ValueType            MeasurementType;
  typedef typename Superclass::FrequencyType             FrequencyType;
  typedef typename Superclass::TotalFrequencyType        TotalFrequencyType;
  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;

  /** the number of components in a measurement vector */
  itkStaticConstMacro(MeasurementVectorSize, unsigned int,
                      PixelTraitsType::Dimension);
  
  virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
    {
    // Measurement vector size for this class is fixed as the pixel's 
    // dimension. This method should throw an exception if the user tries to 
    // set the dimension to a different value. 
    if( s != MeasurementVectorSize )
      {
      itkExceptionMacro( << "Measurement vector size for the image adaptor obtained"
          << " from the pixel dimension is: " << MeasurementVectorSize << " but you "
          << "are setting it to " << s);
      }
    }

 unsigned int GetMeasurementVectorSize() const 
   {
   return MeasurementVectorSize;
   } 
    
  

  typedef TMeasurementVector    MeasurementVectorType;
  typedef MeasurementVectorType ValueType;

  /** Method to set the image */
  void SetImage(const TImage* image);

  /** Method to get the image */
  const TImage* GetImage() const;

  /** returns the number of measurement vectors in this container*/
  unsigned int Size() const;

  inline virtual const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;

  inline FrequencyType GetFrequency(const InstanceIdentifier &id) const;

  TotalFrequencyType GetTotalFrequency() const;
  
 
  class Iterator
  {
  public:
    
    Iterator(){}
    
    Iterator(InstanceIdentifier id, Pointer pContainer)
      :m_Id(id),m_Container(pContainer) {}
    
    FrequencyType GetFrequency() const
      { return 1;}

    const MeasurementVectorType & GetMeasurementVector() const
      { return m_Container->GetMeasurementVector(m_Id);} 

    InstanceIdentifier GetInstanceIdentifier() const
      { return m_Id;}

    Iterator& operator++()
      { ++m_Id; return *this;}
    
    Iterator& operator+(int n)
      { m_Id += n; return *this;}
    
    Iterator& operator-(int n)
      { m_Id -= n; return *this;}

    bool operator!=(const Iterator &it)
      {
      if (m_Id != it.m_Id)
        {return true;}

      if (m_Container != it.m_Container)
        { return true;}

      return false;
      } 
    
    bool operator==(const Iterator &it)
      { return !(*this != it);}
    
    Iterator& operator = (const Iterator &iter)
      { 
      m_Id = iter.m_Id; 
      m_Container = iter.m_Container; 
      return *this;
      }

    Iterator(const Iterator &iter)
      {   
      m_Id = iter.m_Id; 
      m_Container = iter.m_Container; 
      }
    
  private:
    InstanceIdentifier m_Id;  // Current id 
    Pointer m_Container;
  };


  class ConstIterator
    {
    public:
    
    ConstIterator(){}
    
    ConstIterator(InstanceIdentifier id, ConstPointer pContainer)
      :m_Id(id),m_Container(pContainer) {}
    
    FrequencyType GetFrequency() const
      { return 1;}

    const MeasurementVectorType & GetMeasurementVector() const
      { return m_Container->GetMeasurementVector(m_Id);} 

    InstanceIdentifier GetInstanceIdentifier() const
      { return m_Id;}

    ConstIterator& operator++()
      { ++m_Id; return *this;}
    
    ConstIterator& operator+(int n)
      { m_Id += n; return *this;}
    
    ConstIterator& operator-(int n)
      { m_Id -= n; return *this;}

    bool operator!=(const ConstIterator &it)
      {
      if (m_Id != it.m_Id)
        {return true;}

      if (m_Container != it.m_Container)
        { return true;}

      return false;
      }
    
    bool operator==(const ConstIterator &it)
      { return !(*this != it);}
    
    ConstIterator& operator = (const ConstIterator &iter)
      {
      m_Id = iter.m_Id; 
      m_Container = iter.m_Container; 
      return *this;
      }

    ConstIterator(const ConstIterator &iter)
      { 
      m_Id = iter.m_Id; 
      m_Container = iter.m_Container; 
      }
    
  private:
    InstanceIdentifier m_Id;  // Current id 
    ConstPointer m_Container;
  };

  Iterator Begin()
    { 
    Iterator iter(0, this);
    return iter; 
    }
  
  Iterator End()
    {
    Iterator iter(this->Size(), this); 
    return iter; 
    }

  ConstIterator Begin() const
    { 
    ConstIterator iter(0, this);
    return iter; 
    }
  
  ConstIterator End() const
    {
    ConstIterator iter(this->Size(), this); 
    return iter; 
    }
 
protected:
  ImageToListAdaptor();
  virtual ~ImageToListAdaptor() {}
  void PrintSelf(std::ostream& os, Indent indent) const;  

  itkGetConstReferenceMacro(PixelContainer,PixelContainerConstPointer);
  itkGetConstReferenceMacro(UseBuffer,bool);
  itkGetConstReferenceMacro(ImageBeginIndex,IndexType);
  itkGetConstReferenceMacro(ImageEndIndex,IndexType);


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

  PixelContainerConstPointer m_PixelContainer;
  bool                       m_UseBuffer;
  IndexType                  m_ImageBeginIndex;
  IndexType                  m_ImageEndIndex;

  ImageConstPointer m_Image;
}; // end of class ImageToListAdaptor

template < class TImage, class TMeasurementVector >
inline const TMeasurementVector &
ImageToListAdaptor< TImage, TMeasurementVector >
::GetMeasurementVector(const InstanceIdentifier &id) const
{
  if ( m_UseBuffer )
    {
    return *( reinterpret_cast< const MeasurementVectorType* >(&(*m_PixelContainer)[id]) );
    }
  else
    {
    return *(reinterpret_cast< const MeasurementVectorType* >
             ( &(m_Image->GetPixel( m_Image->ComputeIndex( id ) ) ) ) );
    }
}

/** returns the number of measurement vectors in this container*/
template < class TImage, class TMeasurementVector >
inline unsigned int
ImageToListAdaptor< TImage, TMeasurementVector >
::Size() const
{
  return m_PixelContainer->Size();
}

template < class TImage, class TMeasurementVector >
inline typename ImageToListAdaptor< TImage, TMeasurementVector >::FrequencyType
ImageToListAdaptor< TImage, TMeasurementVector >
::GetFrequency(const InstanceIdentifier &) const 
{
  return NumericTraits< FrequencyType >::One;
}

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

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

#endif