ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkAdaptiveEqualizationHistogram.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 itkAdaptiveEqualizationHistogram_h
19#define itkAdaptiveEqualizationHistogram_h
20
21#include <unordered_map>
23#include "itkMath.h"
24#include <cmath>
25
26namespace itk::Function
27{
28
29/* \class AdaptiveEqualizationHistogram
30 *
31 * Implements the function class for a moving histogram algorithm for
32 * adaptive histogram equalization.
33 *
34 * \sa AdaptiveHistogramEqualizationImageFilter
35 * \sa MovingHistogramImageFilter
36 * \ingroup ITKImageStatistics
37 */
38template <class TInputPixel, class TOutputPixel>
40{
41public:
42 using RealType = float;
43
45
46 // ~AdaptiveEqualizationHistogram() {} default is ok
47
48 void
49 AddPixel(const TInputPixel & p)
50 {
51 m_Map[p]++;
52 }
53
54 void
55 RemovePixel(const TInputPixel & p)
56 {
57
58 // insert new item if one doesn't exist
59 auto it = m_Map.find(p);
60
61 itkAssertInDebugAndIgnoreInReleaseMacro(it != m_Map.end());
62
63 if (--(it->second) == 0)
64 {
65 m_Map.erase(it);
66 }
67 }
68
69 TOutputPixel
70 GetValue(const TInputPixel & pixel)
71 {
72
73 // Normalize input pixels to [-0.5 0.5] gray level.
74 // AdaptiveHistogramEqualization compute kernel components with
75 // float, but use double for accumulate and temporaries.
76 const double iscale = static_cast<double>(m_Maximum) - m_Minimum;
77
78 double sum = 0.0;
79 auto itMap = m_Map.begin();
80 const RealType u = (static_cast<double>(pixel) - m_Minimum) / iscale - 0.5;
81 while (itMap != m_Map.end())
82 {
83 const RealType v = (static_cast<double>(itMap->first) - m_Minimum) / iscale - 0.5;
84 const double ikernel = m_KernelSize - m_BoundaryCount;
85 sum += itMap->second * CumulativeFunction(u, v) / ikernel;
86
87 ++itMap;
88 }
89
90 return (TOutputPixel)(iscale * (sum + 0.5) + m_Minimum);
91 }
92
93 void
95 {
97 }
98
99 void
101 {
103 }
104
105 void
107 {
108 m_Alpha = alpha;
109 }
110 void
112 {
113 m_Beta = beta;
114 }
115 void
117 {
118 m_KernelSize = kernelSize;
119 }
120
121 void
122 SetMinimum(TInputPixel minimum)
123 {
124 m_Minimum = minimum;
125 }
126 void
127 SetMaximum(TInputPixel maximum)
128 {
129 m_Maximum = maximum;
130 }
131
132private:
136
137 TInputPixel m_Minimum;
138 TInputPixel m_Maximum;
139
142 {
143 // Calculate cumulative function
144 const RealType s = itk::Math::sgn(u - v);
145 const RealType ad = itk::Math::abs(2.0 * (u - v));
146
147 return 0.5 * s * std::pow(ad, m_Alpha) - m_Beta * 0.5 * s * ad + m_Beta * u;
148 }
149
150private:
151 using MapType = typename std::unordered_map<TInputPixel, size_t, StructHashFunction<TInputPixel>>;
152
153
155 size_t m_BoundaryCount{ 0 };
156};
157
158} // namespace itk::Function
159
160#endif // itkAdaptiveHistogramHistogram_h
typename std::unordered_map< TInputPixel, vcl_size_t, StructHashFunction< TInputPixel > > MapType
bool abs(bool x)
Definition itkMath.h:833