File: ImageCollectionToImageFilter.h

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (372 lines) | stat: -rw-r--r-- 11,288 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
/*=========================================================================

  Program:   ITK-SNAP
  Module:    $RCSfile: ImageCollectionToImageFilter.h,v $
  Language:  C++
  Date:      $Date: 2009/01/24 01:50:21 $
  Version:   $Revision: 1.4 $
  Copyright (c) 2007 Paul A. Yushkevich

  This file is part of ITK-SNAP

  ITK-SNAP is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  -----

  Copyright (c) 2003 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 __ImageCollectionToImageFilter_h_
#define __ImageCollectionToImageFilter_h_

#include <itkImageSource.h>
#include <itkImageToImageFilter.h>

#include <itkImageRegionIteratorWithIndex.h>
#include <itkConstNeighborhoodIterator.h>


/**
 * This class is derived from the itk::iterator hierarchy and used to gain
 * access to the protected m_Offset property
 */
template <class TIterator>
class IteratorOffsetAccessor : public TIterator
{
public:
  typedef typename TIterator::ImageType ImageType;
  typedef typename TIterator::RegionType RegionType;
  typedef itk::OffsetValueType OffsetValueType;

  IteratorOffsetAccessor() { }
  IteratorOffsetAccessor(const IteratorOffsetAccessor<TIterator> &copy)
    : TIterator(copy) {}
  IteratorOffsetAccessor(ImageType *image, const RegionType &region)
    : TIterator(image, region) {}

  OffsetValueType GetOffset() const { return this->m_Offset; }
};

/**
 * An iterator that can be used to iterate over a collection of scalar and
 * vector images of the same type.
 *
 * Example usage:
 *
 * ImageCollectionConstRegionIteratorWithIndex it(out.GetBufferedRegion());
 * it.AddImage(img1);
 * it.AddImage(img2);
 * it.AddImage(img3);
 *
 * int nComp = it.GetTotalComponents();
 * for (; !it.IsAtEnd(); ++it)
 * {
 *   for(int j = 0; j < nComp; j++)
 *   {
 *     GreyType &val = it.Value(j);
 *   }
 * }
 */
template <class TImage, class TVectorImage>
class ImageCollectionConstRegionIteratorWithIndex
{
public:
  /** Standard class typedefs. */
  typedef ImageCollectionConstRegionIteratorWithIndex Self;

  /** Dimension of the image that the iterator walks.  This constant is needed so
   * functions that are templated over image iterator type (as opposed to
   * being templated over pixel type and dimension) can have compile time
   * access to the dimension of the image that the iterator walks. */
  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);

  /** Index typedef support. */
  typedef typename TImage::IndexType         IndexType;
  typedef typename IndexType::IndexValueType IndexValueType;

  /** Size typedef support. */
  typedef typename TImage::SizeType        SizeType;
  typedef typename SizeType::SizeValueType SizeValueType;

  /** Region typedef support. */
  typedef typename TImage::RegionType RegionType;

  /** Image typedef support. */
  typedef TImage ImageType;

  /** Image base type */
  typedef itk::ImageBase<ImageDimension> ImageBaseType;

  /** PixelContainer typedef support. Used to refer to the container for
   * the pixel data. While this was already typdef'ed in the superclass,
   * it needs to be redone here for this subclass to compile properly with gcc. */
  typedef typename TImage::PixelContainer  PixelContainer;
  typedef typename PixelContainer::Pointer PixelContainerPointer;

  typedef typename TVectorImage::PixelContainer VectorPixelContainer;
  typedef typename VectorPixelContainer::Pointer VectorPixelContainerPointer;

  /** Internal Pixel Type */
  typedef typename TImage::InternalPixelType InternalPixelType;
  typedef typename TVectorImage::InternalPixelType VectorInternalPixelType;

  /** Offsets */
  typedef typename TImage::OffsetType OffsetType;
  typedef typename TImage::OffsetValueType OffsetValueType;

  /** Constructor is passed a region */
  ImageCollectionConstRegionIteratorWithIndex(const RegionType &region);

  /** Add a scalar image to the collection */
  void AddScalarImage(TImage *image);

  /** Add a vector image to the collection */
  void AddVectorImage(TVectorImage *image);

  /** Add an image that must be dynamically castable to either TImage or TVectorImage */
  void AddImage(itk::DataObject *image);

  /** Optional neighborhood support (used by Value(comp, offset)) */
  void SetRadius(const SizeType &size);

  /** Get the number of components across the collection */
  itkGetMacro(TotalComponents, unsigned int)

  /** Get the optional neighborhood size */
  itkGetMacro(NeighborhoodSize, int)

  /** Standard iterator operations */
  bool IsAtEnd() const { return m_InternalIter.IsAtEnd(); }
  bool IsAtBegin() const { return m_InternalIter.IsAtBegin(); }
  Self & operator++() { ++m_InternalIter; return *this; }
  Self & operator--() { --m_InternalIter; return *this; }
  void GoToBegin() { m_InternalIter.GoToBegin(); }
  void GoToEnd() { m_InternalIter.GoToEnd(); }

  /** Get a pointer to a component */
  InternalPixelType &Value(unsigned int comp)
  {
    OffsetValueType offset = m_InternalIter.GetOffset();
    InternalPixelType *dataPtr = m_Start[comp] + offset * m_OffsetScaling[comp];
    return *(dataPtr);
  }

  /** Get a pointer to a component in the neighborhood of pointed voxel (no bounds check) */
  InternalPixelType &NeighborValue(unsigned int comp, unsigned int nbr_idx)
  {
    OffsetValueType offset = m_InternalIter.GetOffset();
    offset += m_NeighborhoodOffsetTable[nbr_idx];
    InternalPixelType *dataPtr = m_Start[comp] + offset * m_OffsetScaling[comp];
    return *(dataPtr);
  }

protected:

  // Collection of scalar images
  std::vector<const TImage *> m_ScalarImages;

  // Collection of vector images
  std::vector<const TVectorImage *> m_VectorImages;

  // An array of start pointers for each component
  std::vector<InternalPixelType *> m_Start;

  // The scaling of offsets for each component
  std::vector<int> m_OffsetScaling;

  // A dummy image used to create an internal iterator. Instead of replicating
  // all of the iterator mechanics code in this class, we defer to the internal
  // iterator for all of that.
  typename ImageType::Pointer m_DummyImage;

  // Internal iterator
  typedef itk::ImageRegionIterator<ImageType> WrappedIteratorType;
  typedef IteratorOffsetAccessor<WrappedIteratorType> InternalIteratorType;
  InternalIteratorType m_InternalIter;

  // Total number of components that have been added
  unsigned int m_TotalComponents;

  // The offset for current iteration
  RegionType m_Region;

  // This method initializes all the iteration mechanics
  void ComputeMechanics(ImageBaseType *image);

  // Compute neighborhood offsets
  void ComputeNeighborhoodOffsets();

  // Radius for the optional neighborhood support
  SizeType m_Radius;

  // Offset table for optional neighborhood support
  std::vector<int> m_NeighborhoodOffsetTable;

  // Size of the neighborhood
  int m_NeighborhoodSize;
};


template <class TImage, class TVectorImage>
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::ImageCollectionConstRegionIteratorWithIndex(const RegionType &region)
{
  m_Region = region;
  m_TotalComponents = 0;
  m_Radius.Fill(0);
  m_NeighborhoodSize = 1;
}

template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::AddScalarImage(TImage *image)
{
  // Initialize the iteration mechanics
  if(m_TotalComponents == 0)
    ComputeMechanics(image);
  else
    assert(m_DummyImage->GetBufferedRegion() == image->GetBufferedRegion());

  // Add the image to the list
  m_ScalarImages.push_back(image);
  m_TotalComponents++;

  m_Start.push_back(image->GetBufferPointer());
  m_OffsetScaling.push_back(1);
}

template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::AddVectorImage(TVectorImage *image)
{
  // Initialize the iteration mechanics
  if(m_TotalComponents == 0)
    ComputeMechanics(image);
  else
    assert(m_DummyImage->GetBufferedRegion() == image->GetBufferedRegion());

  m_VectorImages.push_back(image);
  m_TotalComponents += image->GetVectorLength();

  for(int i = 0; i < image->GetVectorLength(); i++)
    {
    m_Start.push_back(image->GetBufferPointer() + i);
    m_OffsetScaling.push_back(image->GetVectorLength());
    }
}

template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::AddImage(itk::DataObject *dobj)
{
  TImage *image = dynamic_cast<TImage *>(dobj);
  if(image)
    {
    this->AddScalarImage(image);
    }
  else
    {
    TVectorImage *vecImage = dynamic_cast<TVectorImage *>(dobj);
    if(vecImage)
      {
      this->AddVectorImage(vecImage);
      }
    else
      {
      itkAssertInDebugOrThrowInReleaseMacro(
            "Wrong input type to ImageCollectionConstRegionIteratorWithIndex");
      }
    }


}

template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::SetRadius(const SizeType &radius)
{
  // Set the radius of the neighborhood
  m_Radius = radius;

  // Compute offsets if an image has been set
  if(this->GetTotalComponents() > 0)
      this->ComputeNeighborhoodOffsets();
}

template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::ComputeMechanics(ImageBaseType *image)
{
  m_DummyImage = ImageType::New();
  m_DummyImage->SetRegions(image->GetBufferedRegion());

  m_InternalIter = InternalIteratorType(m_DummyImage, m_Region);

  this->ComputeNeighborhoodOffsets();
}


template <class TImage, class TVectorImage>
void
ImageCollectionConstRegionIteratorWithIndex<TImage, TVectorImage>
::ComputeNeighborhoodOffsets()
{
  typedef itk::ConstNeighborhoodIterator<ImageType> DummyIter;
  DummyIter diter(m_Radius, m_DummyImage, m_DummyImage->GetBufferedRegion());

  // Compute offsets in memory
  m_NeighborhoodOffsetTable.resize(diter.Size(), 0);
  for(int i = 0; i < diter.Size(); i++)
    {
    OffsetType offset = diter.GetOffset(i);
    IndexType index;
    for(int j = 0; j < TImage::ImageDimension; j++)
      index[j] = offset[j];
    m_NeighborhoodOffsetTable[i] = m_DummyImage->ComputeOffset(index);
    }

  m_NeighborhoodSize = diter.Size();
}



/* example usage:



  */

/*
template <class TScalarImage, class TVectorImage, class TOutputImage>
class ImageCollectionToImageFilter
: public itk::ImageSource<TOutputImage>
{
public:

protected:
};

*/

#endif