Orfeo Toolbox  3.16
itkMovingHistogramMorphologyImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMovingHistogramMorphologyImageFilter.h,v $
5  Language: C++
6  Date: $Date: 2009-09-19 19:10:37 $
7  Version: $Revision: 1.6 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMovingHistogramMorphologyImageFilter_h
18 #define __itkMovingHistogramMorphologyImageFilter_h
19 
21 #include <list>
22 #include <map>
23 
24 namespace itk {
25 
26 namespace Function {
27 template <class TInputPixel, class TCompare>
29 {
30 public:
32  {
35  { initVector(); }
36  }
38 
40  {
42  result->m_Map = this->m_Map;
43  result->m_Vector = this->m_Vector;
44  result->m_CurrentValue = this->m_CurrentValue;
45  result->m_Compare = this->m_Compare;
46  result->m_Direction = this->m_Direction;
47  result->m_Boundary = this->m_Boundary;
48  return result;
49  }
50 
51  // define the method required by the functor and dispatch to the specialized methods
52 
53 
54  inline void AddBoundary()
55  {
57  { AddBoundaryVector(); }
58  else
59  { AddBoundaryMap(); }
60  }
61 
62  inline void RemoveBoundary()
63  {
66  else
67  { RemoveBoundaryMap(); }
68  }
69 
70  inline void AddPixel( const TInputPixel &p )
71  {
73  { AddPixelVector( p ); }
74  else
75  { AddPixelMap( p ); }
76  }
77 
78  inline void RemovePixel( const TInputPixel &p )
79  {
81  { RemovePixelVector( p ); }
82  else
83  { RemovePixelMap( p ); }
84  }
85 
86  inline TInputPixel GetValue( const TInputPixel & )
87  {
89  { return GetValueVector(); }
90  else
91  { return GetValueMap(); }
92  }
93 
94 
95  inline static bool UseVectorBasedAlgorithm()
96  {
97  // bool, short and char are acceptable for vector based algorithm: they do not require
98  // too much memory. Other types are not usable with that algorithm
99  return typeid(TInputPixel) == typeid(unsigned char)
100  || typeid(TInputPixel) == typeid(signed char)
101  || typeid(TInputPixel) == typeid(unsigned short)
102  || typeid(TInputPixel) == typeid(signed short)
103  || typeid(TInputPixel) == typeid(bool);
104  }
105 
107 
108  //
109  // the map based algorithm
110  //
111 
112  typedef typename std::map< TInputPixel, unsigned long, TCompare > MapType;
113 
114  inline void AddBoundaryMap()
115  { m_Map[ m_Boundary ]++; }
116 
117  inline void RemoveBoundaryMap()
118  { m_Map[ m_Boundary ]--; }
119 
120  inline void AddPixelMap( const TInputPixel &p )
121  { m_Map[ p ]++; }
122 
123  inline void RemovePixelMap( const TInputPixel &p )
124  { m_Map[ p ]--; }
125 
126  inline TInputPixel GetValueMap()
127  {
128  // clean the map
129  typename MapType::iterator mapIt = m_Map.begin();
130  while( mapIt != m_Map.end() )
131  {
132  if( mapIt->second == 0 )
133  {
134  // this value must be removed from the histogram
135  // The value must be stored and the iterator updated before removing the value
136  // or the iterator is invalidated.
137  TInputPixel toErase = mapIt->first;
138  mapIt++;
139  m_Map.erase( toErase );
140  }
141  else
142  {
143  mapIt++;
144  // don't remove all the zero value found, just remove the one before the current maximum value
145  // the histogram may become quite big on real type image, but it's an important increase of performances
146  break;
147  }
148  }
149 
150  // and return the value
151  return m_Map.begin()->first;
152  }
153 
155 
156 
157  //
158  // the vector based algorithm
159  //
160 
161  inline void initVector()
162  {
163  // initialize members need for the vector based algorithm
164  m_Vector.resize( static_cast<int>( NumericTraits< TInputPixel >::max() - NumericTraits< TInputPixel >::NonpositiveMin() + 1 ), 0 );
165  if( m_Compare( NumericTraits< TInputPixel >::max(), NumericTraits< TInputPixel >::NonpositiveMin() ) )
166  {
167  m_CurrentValue = NumericTraits< TInputPixel >::NonpositiveMin();
168  m_Direction = -1;
169  }
170  else
171  {
172  m_CurrentValue = NumericTraits< TInputPixel >::max();
173  m_Direction = 1;
174  }
175  }
176 
177 
178  inline void AddBoundaryVector()
179  { AddPixelVector( m_Boundary ); }
180 
181  inline void RemoveBoundaryVector()
183 
184  inline void AddPixelVector( const TInputPixel &p )
185  {
186  m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]++;
187  if( m_Compare( p, m_CurrentValue ) )
188  { m_CurrentValue = p; }
189  }
190 
191  inline void RemovePixelVector( const TInputPixel &p )
192  {
193  m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]--;
194  while( m_Vector[ static_cast<int>( m_CurrentValue - NumericTraits< TInputPixel >::NonpositiveMin() ) ] == 0 )
196  }
197 
198  inline TInputPixel GetValueVector()
199  { return m_CurrentValue; }
200 
201  std::vector<unsigned long> m_Vector;
202  TInputPixel m_CurrentValue;
203  TCompare m_Compare;
204  signed int m_Direction;
205 
206 
207  // accessor for boundary value
208 
209  void SetBoundary( const TInputPixel & val )
210  { m_Boundary = val; }
211 
212  TInputPixel m_Boundary;
213 };
214 } // end namespace Function
215 
216 
228 template<class TInputImage, class TOutputImage, class TKernel, class THistogram>
230  public MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel, THistogram>
231 {
232 public:
239 
241  itkNewMacro(Self);
242 
246 
248  typedef TInputImage InputImageType;
249  typedef TOutputImage OutputImageType;
250  typedef typename TInputImage::RegionType RegionType;
251  typedef typename TInputImage::SizeType SizeType;
252  typedef typename TInputImage::IndexType IndexType;
253  typedef typename TInputImage::PixelType PixelType;
254  typedef typename TInputImage::OffsetType OffsetType;
255  typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
256  typedef typename TOutputImage::PixelType OutputPixelType;
257 
259  itkStaticConstMacro(ImageDimension, unsigned int,
260  TInputImage::ImageDimension);
261 
263  typedef TKernel KernelType;
264 
266  typedef typename KernelType::ConstIterator KernelIteratorType;
267 
269  typedef typename KernelType::SizeType RadiusType;
270 
271  typedef typename std::list< OffsetType > OffsetListType;
272 
273  typedef typename std::map< OffsetType, OffsetListType, typename OffsetType::LexicographicCompare > OffsetMapType;
274 
276  itkSetMacro(Boundary, PixelType);
277  itkGetConstMacro(Boundary, PixelType);
278 
281  static bool GetUseVectorBasedAlgorithm()
282  { return THistogram::UseVectorBasedAlgorithm(); }
283 
284 protected:
287  void PrintSelf(std::ostream& os, Indent indent) const;
288 
290 // void ThreadedGenerateData (const OutputImageRegionType&
291 // outputRegionForThread,
292 // int threadId);
293 
295  virtual THistogram * NewHistogram();
296 
298 
299 private:
300  MovingHistogramMorphologyImageFilter(const Self&); //purposely not implemented
301  void operator=(const Self&); //purposely not implemented
302 
303 }; // end of class
304 
305 } // end namespace itk
306 
307 #ifndef ITK_MANUAL_INSTANTIATION
309 #endif
310 
311 #endif

Generated at Sat Feb 2 2013 23:53:48 for Orfeo Toolbox with doxygen 1.8.1.1