ITK  6.0.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  * https://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  itkOverrideGetNameOfClassMacro(ImageToListSampleAdaptor);
71 
73  itkNewMacro(Self);
74 
76  using ImageType = TImage;
77  using ImagePointer = typename ImageType::Pointer;
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 typename Superclass::AbsoluteFrequencyType;
98  using typename Superclass::TotalAbsoluteFrequencyType;
99  using typename Superclass::MeasurementVectorSizeType;
100  using 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  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
204 
205  protected:
206  // This method should only be available to the ListSample class
207  ConstIterator(ImageConstIteratorType iter, InstanceIdentifier iid)
208  : m_Iter(std::move(iter))
209  , m_InstanceIdentifier(iid)
210  {}
211 
212  private:
215  InstanceIdentifier m_InstanceIdentifier;
216  };
217 
222  class Iterator : public ConstIterator
223  {
225 
226  public:
227  Iterator() = delete;
228  Iterator(const Self * adaptor) = delete;
229  Iterator(const ImageConstIteratorType & iter, InstanceIdentifier iid) = delete;
230  Iterator(const ConstIterator & it) = delete;
231  ConstIterator &
232  operator=(const ConstIterator & it) = delete;
233 
234  Iterator(Self * adaptor)
235  : ConstIterator(adaptor)
236  {}
237 
238  Iterator(const Iterator & iter)
239  : ConstIterator(iter)
240  {}
241 
242  Iterator &
243  operator=(const Iterator & iter)
244  {
245  this->ConstIterator::operator=(iter);
246  return *this;
247  }
248 
249  protected:
251  : ConstIterator(iter, iid)
252  {}
253  };
254 
256  Iterator
258  {
259  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
260  ImageIteratorType imageIterator(nonConstImage, nonConstImage->GetLargestPossibleRegion());
261  imageIterator.GoToBegin();
262  Iterator iter(imageIterator, 0);
263  return iter;
264  }
268  Iterator
269  End()
270  {
271  ImagePointer nonConstImage = const_cast<ImageType *>(m_Image.GetPointer());
272  const typename ImageType::RegionType & largestRegion = nonConstImage->GetLargestPossibleRegion();
273  ImageIteratorType imageIterator(nonConstImage, largestRegion);
274  imageIterator.GoToEnd();
275  Iterator iter(imageIterator, largestRegion.GetNumberOfPixels());
278  return iter;
279  }
280 
282  ConstIterator
283  Begin() const
284  {
285  ImageConstIteratorType imageConstIterator(m_Image, m_Image->GetLargestPossibleRegion());
286  imageConstIterator.GoToBegin();
287  ConstIterator iter(imageConstIterator, 0);
290  return iter;
291  }
292 
294  ConstIterator
295  End() const
296  {
297  const typename ImageType::RegionType & largestRegion = m_Image->GetLargestPossibleRegion();
298  ImageConstIteratorType imageConstIterator(m_Image, largestRegion);
299  imageConstIterator.GoToEnd();
300  ConstIterator iter(imageConstIterator, largestRegion.GetNumberOfPixels());
303  return iter;
304  }
305 
306 protected:
308  ~ImageToListSampleAdaptor() override = default;
309  void
310  PrintSelf(std::ostream & os, Indent indent) const override;
311 
312 private:
313  ImageConstPointer m_Image{};
314  mutable MeasurementVectorType m_MeasurementVectorInternal{};
315 
316 }; // end of class ImageToListSampleAdaptor
317 } // end of namespace Statistics
318 } // end of namespace itk
319 
320 #ifndef ITK_MANUAL_INSTANTIATION
321 # include "itkImageToListSampleAdaptor.hxx"
322 #endif
323 
324 #endif
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
ConstPointer
SmartPointer< const Self > ConstPointer
Definition: itkAddImageFilter.h:94
itk::Statistics::ImageToListSampleAdaptor::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkImageToListSampleAdaptor.h:243
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:417
itk::Statistics::ImageToListSampleAdaptor::Begin
ConstIterator Begin() const
Definition: itkImageToListSampleAdaptor.h:283
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::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:398
itkPixelTraits.h
itk::SmartPointer< Self >
itkImageRegionIterator.h
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::Sample< MeasurementVectorPixelTraits< TImage::PixelType >::MeasurementVectorType >::InstanceIdentifier
typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier
Definition: itkSample.h:91
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(const ImageIteratorType &iter, InstanceIdentifier iid)
Definition: itkImageToListSampleAdaptor.h:250
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:222
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:438
itk::Statistics::Sample< MeasurementVectorPixelTraits< TImage::PixelType >::MeasurementVectorType >::MeasurementVectorSizeType
unsigned int MeasurementVectorSizeType
Definition: itkSample.h:94
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:214
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::End
Iterator End()
Definition: itkImageToListSampleAdaptor.h:269
itk::Statistics::ImageToListSampleAdaptor::ImageConstPointer
typename ImageType::ConstPointer ImageConstPointer
Definition: itkImageToListSampleAdaptor.h:78
itk::Statistics::ImageToListSampleAdaptor::Iterator::Iterator
Iterator(Self *adaptor)
Definition: itkImageToListSampleAdaptor.h:234
itk::Statistics::Sample< MeasurementVectorPixelTraits< TImage::PixelType >::MeasurementVectorType >::AbsoluteFrequencyType
MeasurementVectorTraits::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSample.h:84
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:198
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkImageToListSampleAdaptor.h:163
itk::Statistics::ImageToListSampleAdaptor::End
ConstIterator End() const
Definition: itkImageToListSampleAdaptor.h:295
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_Iter
ImageConstIteratorType m_Iter
Definition: itkImageToListSampleAdaptor.h:213
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:238
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: itkAnatomicalOrientation.h:29
itk::Statistics::ImageToListSampleAdaptor::PixelType
typename ImageType::PixelType PixelType
Definition: itkImageToListSampleAdaptor.h:80
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:207
itk::Statistics::MeasurementVectorPixelTraits
Definition: itkMeasurementVectorTraits.h:434
itk::Statistics::ImageToListSampleAdaptor::MeasurementType
typename MeasurementVectorTraitsType::ValueType MeasurementType
Definition: itkImageToListSampleAdaptor.h:95
itk::Statistics::MeasurementVectorTraitsTypes::ValueType
typename TMeasurementVector::ValueType ValueType
Definition: itkMeasurementVectorTraits.h:420
itk::Statistics::ImageToListSampleAdaptor::ConstIterator::m_InstanceIdentifier
InstanceIdentifier m_InstanceIdentifier
Definition: itkImageToListSampleAdaptor.h:215
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itkSmartPointer.h
itk::ImageRegionConstIterator< ImageType >
itk::Statistics::ImageToListSampleAdaptor::Begin
Iterator Begin()
Definition: itkImageToListSampleAdaptor.h:257
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293