ITK  5.2.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_MOVE(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() = delete;
154 
155  ConstIterator(const ImageToListSampleAdaptor * adaptor) { *this = adaptor->Begin(); }
156 
158  : m_Iter(iter.m_Iter)
159  , m_InstanceIdentifier(iter.m_InstanceIdentifier)
160  {}
161 
162  ConstIterator &
163  operator=(const ConstIterator & iter)
164  {
165  m_Iter = iter.m_Iter;
166  m_InstanceIdentifier = iter.m_InstanceIdentifier;
167  return *this;
168  }
169 
171  GetFrequency() const
172  {
173  return 1;
174  }
175 
176  const MeasurementVectorType &
178  {
179  MeasurementVectorTraits::Assign(this->m_MeasurementVectorCache, m_Iter.Get());
180  return this->m_MeasurementVectorCache;
181  }
182 
185  {
186  return m_InstanceIdentifier;
187  }
188 
189  ConstIterator &
191  {
192  ++m_Iter;
193  ++m_InstanceIdentifier;
194  return *this;
195  }
196 
197  bool
198  operator!=(const ConstIterator & it) const
199  {
200  return (m_Iter != it.m_Iter);
201  }
202 
203  bool
204  operator==(const ConstIterator & it) const
205  {
206  return (m_Iter == it.m_Iter);
207  }
208 
209  protected:
210  // This method should only be available to the ListSample class
212  : m_Iter(std::move(iter))
213  , m_InstanceIdentifier(iid)
214  {}
215 
216  private:
220  };
221 
226  class Iterator : public ConstIterator
227  {
229 
230  public:
231  Iterator() = delete;
232  Iterator(const Self * adaptor) = delete;
233  Iterator(const ImageConstIteratorType & iter, InstanceIdentifier iid) = delete;
234  Iterator(const ConstIterator & it) = delete;
235  ConstIterator &
236  operator=(const ConstIterator & it) = delete;
237 
238  Iterator(Self * adaptor)
239  : ConstIterator(adaptor)
240  {}
241 
242  Iterator(const Iterator & iter)
243  : ConstIterator(iter)
244  {}
245 
246  Iterator &
247  operator=(const Iterator & iter)
248  {
249  this->ConstIterator::operator=(iter);
250  return *this;
251  }
252 
253  protected:
255  : ConstIterator(iter, iid)
256  {}
257  };
258 
260  Iterator
262  {
263  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
264  ImageIteratorType imageIterator(nonConstImage, nonConstImage->GetLargestPossibleRegion());
265  imageIterator.GoToBegin();
266  Iterator iter(imageIterator, 0);
267  return iter;
268  }
270 
272  Iterator
273  End()
274  {
275  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
276  const typename ImageType::RegionType & largestRegion = nonConstImage->GetLargestPossibleRegion();
277  ImageIteratorType imageIterator(nonConstImage, largestRegion);
278  imageIterator.GoToEnd();
279  Iterator iter(imageIterator, largestRegion.GetNumberOfPixels());
281 
282  return iter;
283  }
284 
286  ConstIterator
287  Begin() const
288  {
289  ImageConstIteratorType imageConstIterator(m_Image, m_Image->GetLargestPossibleRegion());
290  imageConstIterator.GoToBegin();
291  ConstIterator iter(imageConstIterator, 0);
293 
294  return iter;
295  }
296 
298  ConstIterator
299  End() const
300  {
301  const typename ImageType::RegionType & largestRegion = m_Image->GetLargestPossibleRegion();
302  ImageConstIteratorType imageConstIterator(m_Image, largestRegion);
303  imageConstIterator.GoToEnd();
304  ConstIterator iter(imageConstIterator, largestRegion.GetNumberOfPixels());
306 
307  return iter;
308  }
309 
310 protected:
312  ~ImageToListSampleAdaptor() override = default;
313  void
314  PrintSelf(std::ostream & os, Indent indent) const override;
315 
316 private:
319 
320 }; // end of class ImageToListSampleAdaptor
321 } // end of namespace Statistics
322 } // end of namespace itk
323 
324 #ifndef ITK_MANUAL_INSTANTIATION
325 # include "itkImageToListSampleAdaptor.hxx"
326 #endif
327 
328 #endif
itk::Statistics::ImageToListSampleAdaptor::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkImageToListSampleAdaptor.h:247
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkImageToListSampleAdaptor.h:177
itk::Statistics::ImageToListSampleAdaptor::IndexType
typename ImageType::IndexType IndexType
Definition: itkImageToListSampleAdaptor.h:79
itk::ImageRegionConstIterator::GoToBegin
void GoToBegin()
Definition: itkImageRegionConstIterator.h:196
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:287
itk::Statistics::ImageToListSampleAdaptor::MeasurementVectorSizeType
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkImageToListSampleAdaptor.h:99
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkImageToListSampleAdaptor.h:184
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:254
itk::Statistics::ImageToListSampleAdaptor::GetMeasurementVectorSize
MeasurementVectorSizeType GetMeasurementVectorSize() const override
Definition: itkImageToListSampleAdaptor.h:121
itk::ImageRegionConstIterator::GoToEnd
void GoToEnd()
Definition: itkImageRegionConstIterator.h:208
itk::Statistics::ImageToListSampleAdaptor::Iterator
Iterator.
Definition: itkImageToListSampleAdaptor.h:226
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:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_MeasurementVectorCache
MeasurementVectorType m_MeasurementVectorCache
Definition: itkImageToListSampleAdaptor.h:218
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkImageToListSampleAdaptor.h:171
itk::Statistics::ImageToListSampleAdaptor::ImageType
TImage ImageType
Definition: itkImageToListSampleAdaptor.h:76
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator!=
bool operator!=(const ConstIterator &it) const
Definition: itkImageToListSampleAdaptor.h:198
itk::Statistics::ImageToListSampleAdaptor::End
Iterator End()
Definition: itkImageToListSampleAdaptor.h:273
itk::Statistics::ImageToListSampleAdaptor::ImageConstPointer
typename ImageType::ConstPointer ImageConstPointer
Definition: itkImageToListSampleAdaptor.h:78
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(Self *adaptor)
Definition: itkImageToListSampleAdaptor.h:238
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkImageToListSampleAdaptor.h:190
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::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkImageToListSampleAdaptor.h:204
itk::Statistics::ImageToListSampleAdaptor::m_MeasurementVectorInternal
MeasurementVectorType m_MeasurementVectorInternal
Definition: itkImageToListSampleAdaptor.h:318
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkImageToListSampleAdaptor.h:163
itk::Statistics::ImageToListSampleAdaptor::End
ConstIterator End() const
Definition: itkImageToListSampleAdaptor.h:299
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_Iter
ImageConstIteratorType m_Iter
Definition: itkImageToListSampleAdaptor.h:217
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:242
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::ConstIterator
ConstIterator(const ImageToListSampleAdaptor *adaptor)
Definition: itkImageToListSampleAdaptor.h:155
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
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:157
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::ConstIterator
ConstIterator(ImageConstIteratorType iter, InstanceIdentifier iid)
Definition: itkImageToListSampleAdaptor.h:211
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:219
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itkSmartPointer.h
itk::ImageRegionConstIterator< ImageType >
itk::Statistics::ImageToListSampleAdaptor::m_Image
ImageConstPointer m_Image
Definition: itkImageToListSampleAdaptor.h:317
itk::Statistics::ImageToListSampleAdaptor::Begin
Iterator Begin()
Definition: itkImageToListSampleAdaptor.h:261
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293