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