ITK  5.4.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 * https://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
26namespace itk
27{
28namespace Function
29{
30template <typename TInputPixel, typename TCompare>
32{
33public:
34 using MapType = typename std::map<TInputPixel, IdentifierType, TCompare>;
35
37
38 inline void
40 {
42 }
43
44 inline void
46 {
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
118template <typename TInputPixel, typename TCompare>
120{
121public:
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 {
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;
203 TInputPixel m_Boundary;
204};
205
207
208// now create MorphologyHistogram partial specializations using the VectorMorphologyHistogram
209// as base class
210
211template <typename TCompare>
212class MorphologyHistogram<unsigned char, TCompare> : public VectorMorphologyHistogram<unsigned char, TCompare>
213{};
214
215template <typename TCompare>
216class MorphologyHistogram<signed char, TCompare> : public VectorMorphologyHistogram<signed char, TCompare>
217{};
218
219template <typename TCompare>
220class MorphologyHistogram<bool, TCompare> : public VectorMorphologyHistogram<bool, TCompare>
221{};
222
224
225} // end namespace Function
226} // end namespace itk
227
228#endif
void SetBoundary(const TInputPixel &val)
void RemovePixel(const TInputPixel &p)
TInputPixel GetValue(const TInputPixel &)
typename std::map< TInputPixel, IdentifierType, TCompare > MapType
TInputPixel GetValue(const TInputPixel &)
Define additional traits for native types such as int or float.
static constexpr T NonpositiveMin()
static constexpr T max(const T &)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....