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