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"
30
31namespace itk
32{
33namespace Statistics
34{
53template <typename TImage>
54class ITK_TEMPLATE_EXPORT ImageToListSampleAdaptor
55 : public ListSample<typename MeasurementVectorPixelTraits<typename TImage::PixelType>::MeasurementVectorType>
56{
57public:
58 ITK_DISALLOW_COPY_AND_MOVE(ImageToListSampleAdaptor);
59
63 using Superclass =
68
70 itkOverrideGetNameOfClassMacro(ImageToListSampleAdaptor);
71
73 itkNewMacro(Self);
74
76 using ImageType = TImage;
80 using PixelType = typename ImageType::PixelType;
81 using PixelContainerConstPointer = typename ImageType::PixelContainerConstPointer;
82
87
88
96
97 using typename Superclass::AbsoluteFrequencyType;
98 using typename Superclass::TotalAbsoluteFrequencyType;
99 using typename Superclass::MeasurementVectorSizeType;
100 using typename Superclass::InstanceIdentifier;
103
105 void
106 SetImage(const TImage * image);
107
109 const TImage *
110 GetImage() const;
111
114 Size() const override;
115
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
138 GetFrequency(InstanceIdentifier id) const override;
139
142 GetTotalFrequency() const override;
143
148 class ConstIterator
150 friend class ImageToListSampleAdaptor;
151
152 public:
153 ConstIterator() = delete;
155 ConstIterator(const ImageToListSampleAdaptor * adaptor) { *this = adaptor->Begin(); }
157 ConstIterator(const ConstIterator & iter)
158 : m_Iter(iter.m_Iter)
159 , m_InstanceIdentifier(iter.m_InstanceIdentifier)
160 {}
161
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
177 GetMeasurementVector() const
178 {
179 MeasurementVectorTraits::Assign(this->m_MeasurementVectorCache, m_Iter.Get());
180 return this->m_MeasurementVectorCache;
181 }
182
184 GetInstanceIdentifier() const
185 {
186 return m_InstanceIdentifier;
187 }
188
190 operator++()
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 }
203 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
204
205 protected:
206 // This method should only be available to the ListSample class
208 : m_Iter(std::move(iter))
209 , m_InstanceIdentifier(iid)
210 {}
211
212 private:
214 mutable MeasurementVectorType m_MeasurementVectorCache;
215 InstanceIdentifier m_InstanceIdentifier;
216 };
217
222 class Iterator : public ConstIterator
224 friend class ImageToListSampleAdaptor;
225
226 public:
227 Iterator() = delete;
228 Iterator(const Self * adaptor) = delete;
230 Iterator(const ConstIterator & it) = delete;
232 operator=(const ConstIterator & it) = delete;
234 Iterator(Self * adaptor)
235 : ConstIterator(adaptor)
236 {}
238 Iterator(const Iterator & iter)
239 : ConstIterator(iter)
240 {}
241
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
257 Begin()
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 }
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
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
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
306protected:
308 ~ImageToListSampleAdaptor() override = default;
309 void
310 PrintSelf(std::ostream & os, Indent indent) const override;
311
312private:
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
Base class for all data objects in ITK.
A multi-dimensional iterator templated over image type that walks a region of pixels.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Base class for most ITK classes.
Definition: itkObject.h:62
Traits for a pixel that define the dimension and component type.
Iterator(const ImageConstIteratorType &iter, InstanceIdentifier iid)=delete
This class provides ListSample interface to ITK Image.
typename MeasurementVectorTraitsType::ValueType MeasurementType
typename MeasurementPixelTraitsType::MeasurementVectorType MeasurementVectorType
typename ImageType::ConstPointer ImageConstPointer
typename ImageType::PixelContainerConstPointer PixelContainerConstPointer
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:52
typename TMeasurementVector::ValueType ValueType
static void Assign(TArrayType &m, const TArrayType &v)
SmartPointer< const Self > ConstPointer
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
STL namespace.
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:70