File: itkMovingHistogramMorphologyImageFilter.h

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (311 lines) | stat: -rw-r--r-- 9,770 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkMovingHistogramMorphologyImageFilter.h
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkMovingHistogramMorphologyImageFilter_h
#define __itkMovingHistogramMorphologyImageFilter_h

#include "itkMovingHistogramImageFilter.h"
#include <list>
#include <map>

namespace itk {

namespace Function {
template <class TInputPixel, class TCompare>
class MorphologyHistogram
{
public:
  MorphologyHistogram()
    {
    m_UseVectorBasedAlgorithm = UseVectorBasedAlgorithm();
    if( m_UseVectorBasedAlgorithm )
      { initVector(); }
    }
  ~MorphologyHistogram(){}

  MorphologyHistogram * Clone() const
    {
    MorphologyHistogram * result = new MorphologyHistogram();
    result->m_Map = this->m_Map;
    result->m_Vector = this->m_Vector;
    result->m_CurrentValue = this->m_CurrentValue;
    result->m_Compare = this->m_Compare;
    result->m_Direction = this->m_Direction;
    result->m_Boundary = this->m_Boundary;
    return result;
    }

  // define the method required by the functor and dispatch to the specialized methods


  inline void AddBoundary()
    {
    if( m_UseVectorBasedAlgorithm )
      { AddBoundaryVector(); }
    else
      { AddBoundaryMap(); }
    }

  inline void RemoveBoundary()
    {
    if( m_UseVectorBasedAlgorithm )
      { RemoveBoundaryVector(); }
    else
      { RemoveBoundaryMap(); }
    }

  inline void AddPixel( const TInputPixel &p )
    {
    if( m_UseVectorBasedAlgorithm )
      { AddPixelVector( p ); }
    else
      { AddPixelMap( p ); }
    }

  inline void RemovePixel( const TInputPixel &p )
    {
    if( m_UseVectorBasedAlgorithm )
      { RemovePixelVector( p ); }
    else
      { RemovePixelMap( p ); }
    }

  inline TInputPixel GetValue( const TInputPixel & )
    {
    if( m_UseVectorBasedAlgorithm )
      { return GetValueVector(); }
    else
      { return GetValueMap(); }
    }


  inline static bool UseVectorBasedAlgorithm()
    {
    // bool, short and char are acceptable for vector based algorithm: they do not require
    // too much memory. Other types are not usable with that algorithm
    return typeid(TInputPixel) == typeid(unsigned char)
        || typeid(TInputPixel) == typeid(signed char)
        || typeid(TInputPixel) == typeid(unsigned short)
        || typeid(TInputPixel) == typeid(signed short)
        || typeid(TInputPixel) == typeid(bool);
    }

  bool m_UseVectorBasedAlgorithm;
  
  //
  // the map based algorithm
  //

  typedef typename std::map< TInputPixel, unsigned long, TCompare > MapType;

  inline void AddBoundaryMap()
    { m_Map[ m_Boundary ]++; }

  inline void RemoveBoundaryMap()
    { m_Map[ m_Boundary ]--; }

  inline void AddPixelMap( const TInputPixel &p )
    { m_Map[ p ]++; }

  inline void RemovePixelMap( const TInputPixel &p )
    { m_Map[ p ]--; }

  inline TInputPixel GetValueMap()
    {
    // clean the map
    typename MapType::iterator mapIt = m_Map.begin();
    while( mapIt != m_Map.end() )
      {
      if( mapIt->second == 0 )
        { 
        // this value must be removed from the histogram
        // The value must be stored and the iterator updated before removing the value
        // or the iterator is invalidated.
        TInputPixel toErase = mapIt->first;
        mapIt++;
        m_Map.erase( toErase );
        }
      else
        {
        mapIt++;
        // don't remove all the zero value found, just remove the one before the current maximum value
        // the histogram may become quite big on real type image, but it's an important increase of performances
        break;
        }
      }

    // and return the value
    return m_Map.begin()->first;
    }

  MapType m_Map;


  //
  // the vector based algorithm
  //

  inline void initVector()
    {
    // initialize members need for the vector based algorithm
    m_Vector.resize( static_cast<int>( NumericTraits< TInputPixel >::max() - NumericTraits< TInputPixel >::NonpositiveMin() + 1 ), 0 );
    if( m_Compare( NumericTraits< TInputPixel >::max(), NumericTraits< TInputPixel >::NonpositiveMin() ) )
      {
      m_CurrentValue = NumericTraits< TInputPixel >::NonpositiveMin();
      m_Direction = -1;
      }
    else
      {
      m_CurrentValue = NumericTraits< TInputPixel >::max();
      m_Direction = 1;
      }
    }
  

  inline void AddBoundaryVector()
    { AddPixelVector( m_Boundary ); }

  inline void RemoveBoundaryVector()
    { RemovePixelVector( m_Boundary ); }

  inline void AddPixelVector( const TInputPixel &p )
    {
    m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]++;
    if( m_Compare( p, m_CurrentValue ) )
      { m_CurrentValue = p; }
    }

  inline void RemovePixelVector( const TInputPixel &p )
    {
    m_Vector[ static_cast<int>( p - NumericTraits< TInputPixel >::NonpositiveMin() ) ]--;
    while( m_Vector[ static_cast<int>( m_CurrentValue - NumericTraits< TInputPixel >::NonpositiveMin() ) ] == 0 )
      { m_CurrentValue += m_Direction; }
    }

  inline TInputPixel GetValueVector()
    { return m_CurrentValue; }

  std::vector<unsigned long> m_Vector;
  TInputPixel                m_CurrentValue;
  TCompare                   m_Compare;
  signed int                 m_Direction;
  

  // accessor for boundary value

  void SetBoundary( const TInputPixel & val )
    { m_Boundary = val; }

  TInputPixel m_Boundary;
};
} // end namespace Function


/**
 * \class MovingHistogramMorphologyImageFilter
 * \brief base class for MovingHistogramDilateImageFilter and MovingHistogramErodeImageFilter
 *
 * This class is similar to MovingHistogramImageFilter but add support
 * for boundaries and don't fully update the histogram to enhance performances.
 * 
 * \sa MovingHistogramImageFilter, MovingHistogramDilateImageFilter, MovingHistogramErodeImageFilter
 * \ingroup ImageEnhancement  MathematicalMorphologyImageFilters
 */

template<class TInputImage, class TOutputImage, class TKernel, class THistogram>
class ITK_EXPORT MovingHistogramMorphologyImageFilter : 
    public MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel, THistogram>
{
public:
  /** Standard class typedefs. */
  typedef MovingHistogramMorphologyImageFilter             Self;
  typedef MovingHistogramImageFilter<TInputImage, TOutputImage, TKernel, THistogram>
                                                           Superclass;
  typedef SmartPointer<Self>                               Pointer;
  typedef SmartPointer<const Self>                         ConstPointer;
  
  /** Standard New method. */
  itkNewMacro(Self);  

  /** Runtime information support. */
  itkTypeMacro(MovingHistogramMorphologyImageFilter, 
               ImageToImageFilter);
  
  /** Image related typedefs. */
  typedef TInputImage                                      InputImageType;
  typedef TOutputImage                                     OutputImageType;
  typedef typename TInputImage::RegionType                 RegionType;
  typedef typename TInputImage::SizeType                   SizeType;
  typedef typename TInputImage::IndexType                  IndexType;
  typedef typename TInputImage::PixelType                  PixelType;
  typedef typename TInputImage::OffsetType                 OffsetType;
  typedef typename Superclass::OutputImageRegionType       OutputImageRegionType;
  typedef typename TOutputImage::PixelType                 OutputPixelType;
  
  /** Image related typedefs. */
  itkStaticConstMacro(ImageDimension, unsigned int,
                      TInputImage::ImageDimension);
                      
  /** Kernel typedef. */
  typedef TKernel                                          KernelType;
  
  /** Kernel (structuring element) iterator. */
  typedef typename KernelType::ConstIterator               KernelIteratorType;
  
  /** n-dimensional Kernel radius. */
  typedef typename KernelType::SizeType                    RadiusType;

  typedef typename std::list< OffsetType >                 OffsetListType;

  typedef typename std::map< OffsetType, OffsetListType, typename OffsetType::LexicographicCompare >          OffsetMapType;

  /** Set/Get the boundary value. */
  itkSetMacro(Boundary, PixelType);
  itkGetConstMacro(Boundary, PixelType);

  /** Return true if the vector based algorithm is used, and
   * false if the map based algorithm is used */
  static bool GetUseVectorBasedAlgorithm()
    { return THistogram::UseVectorBasedAlgorithm(); }
  
protected:
  MovingHistogramMorphologyImageFilter();
  ~MovingHistogramMorphologyImageFilter() {};
  void PrintSelf(std::ostream& os, Indent indent) const;
  
  /** Multi-thread version GenerateData. */
//   void  ThreadedGenerateData (const OutputImageRegionType& 
//                               outputRegionForThread,
//                               int threadId);

  /** needed to pass the boundary value to the histogram object */
  virtual THistogram * NewHistogram();

  PixelType m_Boundary;

private:
  MovingHistogramMorphologyImageFilter(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented

}; // end of class

} // end namespace itk
  
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkMovingHistogramMorphologyImageFilter.txx"
#endif

#endif