ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageToNeighborhoodSampleAdaptor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 itkImageToNeighborhoodSampleAdaptor_h
19 #define itkImageToNeighborhoodSampleAdaptor_h
20 
21 #include <typeinfo>
22 #include <vector>
23 #include <iostream>
24 
25 #include "itkImage.h"
26 #include "itkListSample.h"
27 #include "itkSmartPointer.h"
29 #include "itkMacro.h"
32 
33 namespace itk {
34 namespace Statistics {
35 
51  template < typename TImage, typename TBoundaryCondition >
52 class ITK_TEMPLATE_EXPORT ImageToNeighborhoodSampleAdaptor :
53  public ListSample< std::vector< ConstNeighborhoodIterator< TImage, TBoundaryCondition > > >
54 {
55 public:
56  ITK_DISALLOW_COPY_AND_ASSIGN(ImageToNeighborhoodSampleAdaptor);
57 
60 
61  using Superclass =
63 
66 
69 
71  itkNewMacro(Self);
72 
74  using ImageType = TImage;
75  using ImagePointer = typename ImageType::Pointer;
76  using ImageConstPointer = typename ImageType::ConstPointer;
77  using IndexType = typename ImageType::IndexType;
78  using OffsetType = typename ImageType::OffsetType;
80  using PixelType = typename ImageType::PixelType;
81  using PixelContainerConstPointer = typename ImageType::PixelContainerConstPointer;
83  using OffsetTableType = typename RegionType::OffsetTableType;
84  using SizeType = typename ImageType::SizeType;
86 
94 
97  using MeasurementVectorType =
98  typename std::vector< ConstNeighborhoodIterator< TImage, TBoundaryCondition > >;
99  using ValueType = typename MeasurementVectorType::value_type;
101 
102  using AbsoluteFrequencyType = typename Superclass::AbsoluteFrequencyType;
103  using TotalAbsoluteFrequencyType = typename Superclass::TotalAbsoluteFrequencyType;
104  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
105  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
106 
108  void SetImage(const TImage* image);
109 
111  const TImage* GetImage() const;
112 
114  void SetRadius(const NeighborhoodRadiusType& radius);
115 
117  NeighborhoodRadiusType GetRadius() const;
118 
120  void SetRegion(const RegionType& region);
121 
123  RegionType GetRegion() const;
124 
125  void SetUseImageRegion(const bool& flag);
126 
128  itkGetConstMacro( UseImageRegion, bool );
129 
131  itkBooleanMacro( UseImageRegion );
132 
133 
135  InstanceIdentifier Size() const override;
136 
138  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override;
139 
141  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override;
142 
144  TotalAbsoluteFrequencyType GetTotalFrequency() const override;
145 
151  {
153  public:
154 
156  {
157  *this = adaptor->Begin();
158  }
159 
161  {
162  m_MeasurementVectorCache = iter.m_MeasurementVectorCache;
163  m_InstanceIdentifier = iter.m_InstanceIdentifier;
164  }
165 
167  {
168  m_MeasurementVectorCache = iter.m_MeasurementVectorCache;
169  m_InstanceIdentifier = iter.m_InstanceIdentifier;
170  return *this;
171  }
172 
174  {
175  return 1;
176  }
177 
179  {
180  return this->m_MeasurementVectorCache;
181  }
182 
184  {
185  return m_InstanceIdentifier;
186  }
187 
189  {
190  ++(m_MeasurementVectorCache[0]);
191  ++m_InstanceIdentifier;
192  return *this;
193  }
194 
195  bool operator!=(const ConstIterator &it)
196  {
197  return (m_MeasurementVectorCache[0] != it.m_MeasurementVectorCache[0]);
198  }
199 
200  bool operator==(const ConstIterator &it)
201  {
202  return (m_MeasurementVectorCache[0] == it.m_MeasurementVectorCache[0]);
203  }
204 
205  protected:
206  // This method should only be available to the ListSample class
209  InstanceIdentifier iid)
210  {
211  this->m_MeasurementVectorCache.clear();
212  this->m_MeasurementVectorCache.push_back(iter);
213  m_InstanceIdentifier = iid;
214  }
215 
216  private:
217  ConstIterator() = delete;
220  };
221 
226  class Iterator : public ConstIterator
227  {
228 
230 
231  public:
232 
233  Iterator(Self * adaptor):ConstIterator(adaptor)
234  {
235  }
236 
237  Iterator(const Iterator &iter):ConstIterator( iter )
238  {
239  }
240 
241  Iterator& operator =(const Iterator & iter)
242  {
243  this->ConstIterator::operator=( iter );
244  return *this;
245  }
246 
247 #if !(defined(_MSC_VER) && (_MSC_VER <= 1200))
248  protected:
249 #endif
250  //This copy constructor is actually used in Iterator Begin()!
252  {
253  }
254 
255  private:
256  // To ensure const-correctness these method must not be in the public API.
257  // The are not implemented, since they should never be called.
258  Iterator() = delete;
259  Iterator(const Self * adaptor) = delete;
260  Iterator(const ConstIterator & it) = delete;
261  ConstIterator& operator=(const ConstIterator& it) = delete;
262  };
263 
266  {
267  NeighborhoodIteratorType nIterator( m_Radius, m_Image, m_Region);
268  nIterator.GoToBegin();
269  Iterator iter(nIterator, 0);
270  return iter;
271  }
273 
276  {
277  NeighborhoodIteratorType nIterator( m_Radius, m_Image, m_Region);
278  nIterator.GoToEnd();
279  Iterator iter(nIterator, m_Region.GetNumberOfPixels());
280  return iter;
281  }
283 
284 
287  {
288  NeighborhoodIteratorType nIterator( m_Radius, m_Image, m_Region);
289  nIterator.GoToBegin();
290  ConstIterator iter(nIterator, 0);
291  return iter;
292  }
294 
297  {
298  NeighborhoodIteratorType nIterator( m_Radius, m_Image, m_Region);
299  nIterator.GoToEnd();
300  ConstIterator iter(nIterator, m_Region.GetNumberOfPixels());
301  return iter;
302  }
304 
305 protected:
307  ~ImageToNeighborhoodSampleAdaptor() override = default;
308  void PrintSelf(std::ostream& os, Indent indent) const override;
309 
310 private:
317  bool m_UseImageRegion{true};
319 
320 }; // end of class ImageToNeighborhoodSampleAdaptor
321 
322 } // end of namespace Statistics
323 
324 template <typename TImage, typename TBoundaryCondition>
325  std::ostream & operator<<(std::ostream &os,
327 
328 } // end of namespace itk
329 
330 #ifndef ITK_MANUAL_INSTANTIATION
331 #include "itkImageToNeighborhoodSampleAdaptor.hxx"
332 #endif
333 
334 #endif
typename std::vector< ConstNeighborhoodIterator< TImage, TBoundaryCondition > > MeasurementVectorType
typename NeighborhoodIteratorType::NeighborhoodType NeighborhoodType
ConstIterator(NeighborhoodIteratorType iter, InstanceIdentifier iid)
ITK_ITERATOR_VIRTUAL void GoToBegin() ITK_ITERATOR_FINAL
A light-weight container object for storing an N-dimensional neighborhood of values.
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:188
typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType
typename NeighborhoodIteratorType::IndexType NeighborhoodIndexType
typename ImageType::PixelContainerConstPointer PixelContainerConstPointer
Const version of NeighborhoodIterator, defining iteration of a local N-dimensional neighborhood of pi...
ITK_ITERATOR_VIRTUAL void GoToEnd() ITK_ITERATOR_FINAL
A multi-dimensional iterator templated over image type that walks pixels within a region and is speci...
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:51
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Iterator(NeighborhoodIteratorType iter, InstanceIdentifier iid)
typename Superclass::RadiusType RadiusType
typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
This class provides ListSample interface to ITK Image.
Base class for most ITK classes.
Definition: itkObject.h:60
signed long OffsetValueType
Definition: itkIntTypes.h:94
Base class for all data objects in ITK.
Defines iteration of a local N-dimensional neighborhood of pixels across an itk::Image.
typename NeighborhoodIteratorType::SizeType NeighborhoodSizeType
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
typename NeighborhoodIteratorType::RadiusType NeighborhoodRadiusType