ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkMorphologyHistogram.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 itkMorphologyHistogram_h
19 #define itkMorphologyHistogram_h
20 
21 #include <map>
22 #include <vector>
23 #include "itkIntTypes.h"
24 #include "itkNumericTraits.h"
25 
26 namespace itk
27 {
28 namespace Function
29 {
30 template< typename TInputPixel, typename TCompare >
32 {
33 public:
34 
35  typedef typename std::map< TInputPixel, IdentifierType, TCompare > MapType;
36 
38 
39  inline void AddBoundary()
40  {
41  m_Map[m_Boundary]++;
42  }
43 
44  inline void RemoveBoundary()
45  {
46  m_Map[m_Boundary]--;
47  }
48 
49  inline void AddPixel(const TInputPixel & p)
50  {
51  m_Map[p]++;
52  }
53 
54  inline void RemovePixel(const TInputPixel & p)
55  {
56  m_Map[p]--;
57  }
58 
59  inline TInputPixel GetValue()
60  {
61  itkAssertInDebugAndIgnoreInReleaseMacro(!m_Map.empty());
62  // clean the map
63  typename MapType::iterator mapIt = m_Map.begin();
64  while ( mapIt != m_Map.end() )
65  {
66  if ( mapIt->second == 0 )
67  {
68  // this value must be removed from the histogram
69  // The value must be stored and the iterator updated before removing the
70  // value
71  // or the iterator is invalidated.
72  TInputPixel toErase = mapIt->first;
73  mapIt++;
74  m_Map.erase(toErase);
75  }
76  else
77  {
78  mapIt++;
79  // don't remove all the zero value found, just remove the one before the
80  // current maximum value
81  // the histogram may become quite big on real type image, but it's an
82  // important increase of performances
83  break;
84  }
85  }
86 
87  // and return the value
88  itkAssertInDebugAndIgnoreInReleaseMacro(!m_Map.empty());
89  return m_Map.begin()->first;
90  }
91 
92  inline TInputPixel GetValue(const TInputPixel &)
93  {
94  return GetValue();
95  }
96 
97  void SetBoundary(const TInputPixel & val)
98  {
99  m_Boundary = val;
100  }
101 
103  {
104  return false;
105  }
106 
108  TInputPixel m_Boundary;
109 
110 };
111 
112 template< typename TInputPixel, typename TCompare >
114 {
115 public:
116 
118  {
119  // initialize members need for the vector based algorithm
122  {
125  m_Direction = -1;
126  }
127  else
128  {
131  m_Direction = 1;
132  }
133  m_Boundary = 0;
134  }
135 
136  inline void AddBoundary()
137  {
139  }
140 
141  inline void RemoveBoundary()
142  {
144  }
145 
146  inline void AddPixel(const TInputPixel & p)
147  {
148  m_Vector[p - NumericTraits < TInputPixel > ::NonpositiveMin()]++;
149  if ( m_Compare(p, m_CurrentValue) )
150  {
151  m_CurrentValue = p;
152  }
153  }
154 
155  inline void RemovePixel(const TInputPixel & p)
156  {
157  m_Vector[p - NumericTraits < TInputPixel > ::NonpositiveMin()]--;
160  {
162  }
163  }
164 
165  inline TInputPixel GetValue()
166  {
167  return m_CurrentValue;
168  }
169 
170  inline TInputPixel GetValue(const TInputPixel &)
171  {
172  return GetValue();
173  }
174 
175  void SetBoundary(const TInputPixel & val)
176  {
177  m_Boundary = val;
178  }
179 
181  {
182  return true;
183  }
184 
185  std::vector< IdentifierType > m_Vector;
186  TInputPixel m_InitValue;
187  TInputPixel m_CurrentValue;
188  TCompare m_Compare;
189  signed int m_Direction;
190  TInputPixel m_Boundary;
191 };
192 
194 
195 // now create MorphologyHistogram partial specilizations using the VectorMorphologyHistogram
196 // as base class
197 
198 template< typename TCompare >
199 class MorphologyHistogram<unsigned char, TCompare>:
200  public VectorMorphologyHistogram<unsigned char, TCompare>
201 {
202 };
203 
204 template< typename TCompare >
205 class MorphologyHistogram<signed char, TCompare>:
206  public VectorMorphologyHistogram<signed char, TCompare>
207 {
208 };
209 
210 template< typename TCompare >
211 class MorphologyHistogram<bool, TCompare>:
212  public VectorMorphologyHistogram<bool, TCompare>
213 {
214 };
215 
217 
218 } // end namespace Function
219 } // end namespace itk
220 
221 #endif
std::map< TInputPixel, IdentifierType, TCompare > MapType
TInputPixel GetValue(const TInputPixel &)
static ITK_CONSTEXPR_FUNC T max(const T &)
void SetBoundary(const TInputPixel &val)
static ITK_CONSTEXPR_FUNC T NonpositiveMin()
void RemovePixel(const TInputPixel &p)
TInputPixel GetValue(const TInputPixel &)
Define additional traits for native types such as int or float.