ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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::Function
27{
28template <typename TInputPixel, typename TCompare>
30{
31public:
32 using MapType = typename std::map<TInputPixel, IdentifierType, TCompare>;
33
35
36 inline void
38 {
40 }
41
42 inline void
44 {
46 }
47
48 inline void
49 AddPixel(const TInputPixel & p)
50 {
51 m_Map[p]++;
52 }
53
54 inline void
55 RemovePixel(const TInputPixel & p)
56 {
57 m_Map[p]--;
58 }
59
60 inline TInputPixel
62 {
63 itkAssertInDebugAndIgnoreInReleaseMacro(!m_Map.empty());
64 // clean the map
65 auto mapIt = m_Map.begin();
66 while (mapIt != m_Map.end())
67 {
68 if (mapIt->second == 0)
69 {
70 // this value must be removed from the histogram
71 // The value must be stored and the iterator updated before removing the
72 // value
73 // or the iterator is invalidated.
74 TInputPixel toErase = mapIt->first;
75 ++mapIt;
76 m_Map.erase(toErase);
77 }
78 else
79 {
80 ++mapIt;
81 // don't remove all the zero value found, just remove the one before the
82 // current maximum value
83 // the histogram may become quite big on real type image, but it's an
84 // important increase of performances
85 break;
86 }
87 }
88
89 // and return the value
90 itkAssertInDebugAndIgnoreInReleaseMacro(!m_Map.empty());
91 return m_Map.begin()->first;
92 }
93
94 inline TInputPixel
95 GetValue(const TInputPixel &)
96 {
97 return GetValue();
98 }
99
100 void
101 SetBoundary(const TInputPixel & val)
102 {
103 m_Boundary = val;
104 }
105
106 static bool
108 {
109 return false;
110 }
111
113 TInputPixel m_Boundary;
114};
115
116template <typename TInputPixel, typename TCompare>
118{
119public:
138
139 inline void
141 {
143 }
144
145 inline void
150
151 inline void
152 AddPixel(const TInputPixel & p)
153 {
156 {
157 m_CurrentValue = p;
158 }
159 }
160
161 inline void
171
172 inline TInputPixel
174 {
175 return m_CurrentValue;
176 }
177
178 inline TInputPixel
179 GetValue(const TInputPixel &)
180 {
181 return GetValue();
182 }
183
184 void
185 SetBoundary(const TInputPixel & val)
186 {
187 m_Boundary = val;
188 }
189
190 static bool
192 {
193 return true;
194 }
195
196 std::vector<IdentifierType> m_Vector;
197 TInputPixel m_InitValue;
198 TInputPixel m_CurrentValue;
199 TCompare m_Compare;
201 TInputPixel m_Boundary;
202};
203
205
206// now create MorphologyHistogram partial specializations using the VectorMorphologyHistogram
207// as base class
208
209template <typename TCompare>
210class MorphologyHistogram<unsigned char, TCompare> : public VectorMorphologyHistogram<unsigned char, TCompare>
211{};
212
213template <typename TCompare>
214class MorphologyHistogram<signed char, TCompare> : public VectorMorphologyHistogram<signed char, TCompare>
215{};
216
217template <typename TCompare>
218class MorphologyHistogram<bool, TCompare> : public VectorMorphologyHistogram<bool, TCompare>
219{};
220
222
223} // namespace itk::Function
224
225#endif
void SetBoundary(const TInputPixel &val)
TInputPixel GetValue(const TInputPixel &)
typename std::map< InputImagePixelType, IdentifierType, TCompare > MapType
TInputPixel GetValue(const TInputPixel &)
static constexpr T NonpositiveMin()
static constexpr T max(const T &)