ITK  5.1.0
Insight Toolkit
itkImageToListSampleAdaptor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageToListSampleAdaptor_h
19 #define itkImageToListSampleAdaptor_h
20 
21 #include <typeinfo>
22 #include <utility>
23 
24 #include "itkImage.h"
25 #include "itkPixelTraits.h"
26 #include "itkListSample.h"
27 #include "itkSmartPointer.h"
28 #include "itkImageRegionIterator.h"
30 
31 namespace itk
32 {
33 namespace Statistics
34 {
53 template <typename TImage>
54 class ITK_TEMPLATE_EXPORT ImageToListSampleAdaptor
55  : public ListSample<typename MeasurementVectorPixelTraits<typename TImage::PixelType>::MeasurementVectorType>
56 {
57 public:
58  ITK_DISALLOW_COPY_AND_ASSIGN(ImageToListSampleAdaptor);
59 
62 
63  using Superclass =
65 
68 
70  itkTypeMacro(ImageToListSampleAdaptor, ListSample);
71 
73  itkNewMacro(Self);
74 
76  using ImageType = TImage;
77  using ImagePointer = typename ImageType::Pointer;
78  using ImageConstPointer = typename ImageType::ConstPointer;
79  using IndexType = typename ImageType::IndexType;
80  using PixelType = typename ImageType::PixelType;
81  using PixelContainerConstPointer = typename ImageType::PixelContainerConstPointer;
82 
87 
88 
93 
96 
97  using AbsoluteFrequencyType = typename Superclass::AbsoluteFrequencyType;
98  using TotalAbsoluteFrequencyType = typename Superclass::TotalAbsoluteFrequencyType;
99  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
100  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
101 
103 
105  void
106  SetImage(const TImage * image);
107 
109  const TImage *
110  GetImage() const;
111 
114  Size() const override;
115 
117  const MeasurementVectorType &
118  GetMeasurementVector(InstanceIdentifier id) const override;
119 
121  GetMeasurementVectorSize() const override
122  {
123  // some filter are expected that this method returns something even if the
124  // input is not set. This won't be the right value for a variable length vector
125  // but it's better than an exception.
126  if (m_Image.IsNull())
127  {
128  return Superclass::GetMeasurementVectorSize();
129  }
130  else
131  {
132  return m_Image->GetNumberOfComponentsPerPixel();
133  }
134  }
135 
137  AbsoluteFrequencyType
138  GetFrequency(InstanceIdentifier id) const override;
139 
141  TotalAbsoluteFrequencyType
142  GetTotalFrequency() const override;
143 
149  {
151 
152  public:
153  ConstIterator(const ImageToListSampleAdaptor * adaptor) { *this = adaptor->Begin(); }
154 
156  : m_Iter(iter.m_Iter)
157  , m_InstanceIdentifier(iter.m_InstanceIdentifier)
158  {}
159 
160  ConstIterator &
161  operator=(const ConstIterator & iter)
162  {
163  m_Iter = iter.m_Iter;
164  m_InstanceIdentifier = iter.m_InstanceIdentifier;
165  return *this;
166  }
167 
169  GetFrequency() const
170  {
171  return 1;
172  }
173 
174  const MeasurementVectorType &
176  {
177  MeasurementVectorTraits::Assign(this->m_MeasurementVectorCache, m_Iter.Get());
178  return this->m_MeasurementVectorCache;
179  }
180 
183  {
184  return m_InstanceIdentifier;
185  }
186 
187  ConstIterator &
189  {
190  ++m_Iter;
191  ++m_InstanceIdentifier;
192  return *this;
193  }
194 
195  bool
197  {
198  return (m_Iter != it.m_Iter);
199  }
200 
201  bool
203  {
204  return (m_Iter == it.m_Iter);
205  }
206 
207  protected:
208  // This method should only be available to the ListSample class
210  : m_Iter(std::move(iter))
211  , m_InstanceIdentifier(iid)
212  {}
213 
214  private:
215  ConstIterator() = delete;
219  };
220 
225  class Iterator : public ConstIterator
226  {
228 
229  public:
230  Iterator(Self * adaptor)
231  : ConstIterator(adaptor)
232  {}
233 
234  Iterator(const Iterator & iter)
235  : ConstIterator(iter)
236  {}
237 
238  Iterator &
239  operator=(const Iterator & iter)
240  {
241  this->ConstIterator::operator=(iter);
242  return *this;
243  }
244 
245  protected:
247  : ConstIterator(iter, iid)
248  {}
249 
250  private:
251  // To ensure const-correctness these method must not be in the public API.
252  // The are purposly not implemented, since they should never be called.
253  Iterator() = delete;
254  Iterator(const Self * adaptor) = delete;
255  Iterator(const ImageConstIteratorType & iter, InstanceIdentifier iid) = delete;
256  Iterator(const ConstIterator & it) = delete;
257  ConstIterator &
258  operator=(const ConstIterator & it) = delete;
259  };
260 
262  Iterator
264  {
265  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
266  ImageIteratorType imageIterator(nonConstImage, nonConstImage->GetLargestPossibleRegion());
267  imageIterator.GoToBegin();
268  Iterator iter(imageIterator, 0);
269  return iter;
270  }
272 
274  Iterator
275  End()
276  {
277  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
278  const typename ImageType::RegionType & largestRegion = nonConstImage->GetLargestPossibleRegion();
279  ImageIteratorType imageIterator(nonConstImage, largestRegion);
280  imageIterator.GoToEnd();
281  Iterator iter(imageIterator, largestRegion.GetNumberOfPixels());
283 
284  return iter;
285  }
286 
288  ConstIterator
289  Begin() const
290  {
291  ImageConstIteratorType imageConstIterator(m_Image, m_Image->GetLargestPossibleRegion());
292  imageConstIterator.GoToBegin();
293  ConstIterator iter(imageConstIterator, 0);
295 
296  return iter;
297  }
298 
300  ConstIterator
301  End() const
302  {
303  const typename ImageType::RegionType & largestRegion = m_Image->GetLargestPossibleRegion();
304  ImageConstIteratorType imageConstIterator(m_Image, largestRegion);
305  imageConstIterator.GoToEnd();
306  ConstIterator iter(imageConstIterator, largestRegion.GetNumberOfPixels());
308 
309  return iter;
310  }
311 
312 protected:
314  ~ImageToListSampleAdaptor() override = default;
315  void
316  PrintSelf(std::ostream & os, Indent indent) const override;
317 
318 private:
321 
322 }; // end of class ImageToListSampleAdaptor
323 } // end of namespace Statistics
324 } // end of namespace itk
325 
326 #ifndef ITK_MANUAL_INSTANTIATION
327 # include "itkImageToListSampleAdaptor.hxx"
328 #endif
329 
330 #endif
itk::Statistics::ImageToListSampleAdaptor::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkImageToListSampleAdaptor.h:239
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkImageToListSampleAdaptor.h:175
itk::Statistics::ImageToListSampleAdaptor::IndexType
typename ImageType::IndexType IndexType
Definition: itkImageToListSampleAdaptor.h:79
itk::ImageRegionConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageRegionConstIterator.h:194
itk::Statistics::ImageToListSampleAdaptor::ImagePointer
typename ImageType::Pointer ImagePointer
Definition: itkImageToListSampleAdaptor.h:77
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::Statistics::MeasurementVectorTraitsTypes
Definition: itkMeasurementVectorTraits.h:420
itk::Statistics::ImageToListSampleAdaptor::Begin
ConstIterator Begin() const
Definition: itkImageToListSampleAdaptor.h:289
itk::Statistics::ImageToListSampleAdaptor::MeasurementVectorSizeType
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkImageToListSampleAdaptor.h:99
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkImageToListSampleAdaptor.h:182
itk::Statistics::ImageToListSampleAdaptor
This class provides ListSample interface to ITK Image.
Definition: itkImageToListSampleAdaptor.h:54
itk::Statistics::ImageToListSampleAdaptor::AbsoluteFrequencyType
typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkImageToListSampleAdaptor.h:97
itk::Statistics::ListSample
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:51
itkImage.h
itk::Statistics::MeasurementVectorTraits::Assign
static void Assign(TArrayType &m, const TArrayType &v)
Definition: itkMeasurementVectorTraits.h:400
itkPixelTraits.h
itk::SmartPointer< Self >
itkImageRegionIterator.h
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(const ImageIteratorType &iter, InstanceIdentifier iid)
Definition: itkImageToListSampleAdaptor.h:246
itk::Statistics::ImageToListSampleAdaptor::GetMeasurementVectorSize
MeasurementVectorSizeType GetMeasurementVectorSize() const override
Definition: itkImageToListSampleAdaptor.h:121
itk::ImageRegionConstIterator::GoToEnd
void GoToEnd()
Definition: itkImageRegionConstIterator.h:206
itk::Statistics::ImageToListSampleAdaptor::Iterator
Iterator.
Definition: itkImageToListSampleAdaptor.h:225
itk::PixelTraits
Traits for a pixel that define the dimension and component type.
Definition: itkPixelTraits.h:45
itk::Statistics::ImageToListSampleAdaptor::MeasurementVectorType
typename MeasurementPixelTraitsType::MeasurementVectorType MeasurementVectorType
Definition: itkImageToListSampleAdaptor.h:92
itk::Statistics::MeasurementVectorPixelTraits::MeasurementVectorType
TPixelType MeasurementVectorType
Definition: itkMeasurementVectorTraits.h:441
itk::Statistics::ImageToListSampleAdaptor::TotalAbsoluteFrequencyType
typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkImageToListSampleAdaptor.h:98
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:78
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_MeasurementVectorCache
MeasurementVectorType m_MeasurementVectorCache
Definition: itkImageToListSampleAdaptor.h:217
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkImageToListSampleAdaptor.h:169
itk::Statistics::ImageToListSampleAdaptor::ImageType
TImage ImageType
Definition: itkImageToListSampleAdaptor.h:76
itk::Statistics::ImageToListSampleAdaptor::End
Iterator End()
Definition: itkImageToListSampleAdaptor.h:275
itk::Statistics::ImageToListSampleAdaptor::ImageConstPointer
typename ImageType::ConstPointer ImageConstPointer
Definition: itkImageToListSampleAdaptor.h:78
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(Self *adaptor)
Definition: itkImageToListSampleAdaptor.h:230
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator!=
bool operator!=(const ConstIterator &it)
Definition: itkImageToListSampleAdaptor.h:196
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkImageToListSampleAdaptor.h:188
itk::Statistics::ImageToListSampleAdaptor::ValueType
MeasurementVectorType ValueType
Definition: itkImageToListSampleAdaptor.h:102
itkListSample.h
itk::Statistics::ImageToListSampleAdaptor::ConstIterator
Const Iterator.
Definition: itkImageToListSampleAdaptor.h:148
itk::Statistics::ImageToListSampleAdaptor::m_MeasurementVectorInternal
MeasurementVectorType m_MeasurementVectorInternal
Definition: itkImageToListSampleAdaptor.h:320
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkImageToListSampleAdaptor.h:161
itk::Statistics::ImageToListSampleAdaptor::End
ConstIterator End() const
Definition: itkImageToListSampleAdaptor.h:301
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_Iter
ImageConstIteratorType m_Iter
Definition: itkImageToListSampleAdaptor.h:216
itkMeasurementVectorTraits.h
itk::Statistics::ImageToListSampleAdaptor::PixelContainerConstPointer
typename ImageType::PixelContainerConstPointer PixelContainerConstPointer
Definition: itkImageToListSampleAdaptor.h:81
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkImageToListSampleAdaptor.h:234
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::ConstIterator
ConstIterator(const ImageToListSampleAdaptor *adaptor)
Definition: itkImageToListSampleAdaptor.h:153
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::Statistics::ImageToListSampleAdaptor::PixelType
typename ImageType::PixelType PixelType
Definition: itkImageToListSampleAdaptor.h:80
itk::Statistics::ImageToListSampleAdaptor::InstanceIdentifier
typename Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkImageToListSampleAdaptor.h:100
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkImageToListSampleAdaptor.h:155
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::ConstIterator
ConstIterator(ImageConstIteratorType iter, InstanceIdentifier iid)
Definition: itkImageToListSampleAdaptor.h:209
itk::Statistics::MeasurementVectorPixelTraits
Definition: itkMeasurementVectorTraits.h:437
itk::Statistics::ImageToListSampleAdaptor::MeasurementType
typename MeasurementVectorTraitsType::ValueType MeasurementType
Definition: itkImageToListSampleAdaptor.h:95
itk::Statistics::MeasurementVectorTraitsTypes::ValueType
typename TMeasurementVector::ValueType ValueType
Definition: itkMeasurementVectorTraits.h:423
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_InstanceIdentifier
InstanceIdentifier m_InstanceIdentifier
Definition: itkImageToListSampleAdaptor.h:218
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:60
itkSmartPointer.h
itk::ImageRegionConstIterator< ImageType >
itk::Statistics::ImageToListSampleAdaptor::m_Image
ImageConstPointer m_Image
Definition: itkImageToListSampleAdaptor.h:319
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator==
bool operator==(const ConstIterator &it)
Definition: itkImageToListSampleAdaptor.h:202
itk::Statistics::ImageToListSampleAdaptor::Begin
Iterator Begin()
Definition: itkImageToListSampleAdaptor.h:263
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293