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>
22 #include "itkStructHashFunction.h"
23 #include "itkMath.h"
24 #include <cmath>
25 namespace itk
26 {
27 namespace 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  */
39 template <class TInputPixel, class TOutputPixel>
41 {
42 public:
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  {
103  --m_BoundaryCount;
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 
133 private:
137 
138  TInputPixel m_Minimum;
139  TInputPixel m_Maximum;
140 
141  RealType
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 
151 private:
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
itk::Function::AdaptiveEqualizationHistogram::RemoveBoundary
void RemoveBoundary()
Definition: itkAdaptiveEqualizationHistogram.h:101
itk::Function::AdaptiveEqualizationHistogram::AddPixel
void AddPixel(const TInputPixel &p)
Definition: itkAdaptiveEqualizationHistogram.h:50
itk::Function::AdaptiveEqualizationHistogram::m_Maximum
TInputPixel m_Maximum
Definition: itkAdaptiveEqualizationHistogram.h:139
itk::Function::AdaptiveEqualizationHistogram::AddBoundary
void AddBoundary()
Definition: itkAdaptiveEqualizationHistogram.h:95
itk::Function::AdaptiveEqualizationHistogram::AdaptiveEqualizationHistogram
AdaptiveEqualizationHistogram()=default
itk::Function::AdaptiveEqualizationHistogram::SetAlpha
void SetAlpha(RealType alpha)
Definition: itkAdaptiveEqualizationHistogram.h:107
itk::Function::AdaptiveEqualizationHistogram
Definition: itkAdaptiveEqualizationHistogram.h:40
itk::Function::AdaptiveEqualizationHistogram::CumulativeFunction
RealType CumulativeFunction(RealType u, RealType v)
Definition: itkAdaptiveEqualizationHistogram.h:142
itk::Function::AdaptiveEqualizationHistogram::m_Alpha
RealType m_Alpha
Definition: itkAdaptiveEqualizationHistogram.h:134
itk::Function::AdaptiveEqualizationHistogram::m_Minimum
TInputPixel m_Minimum
Definition: itkAdaptiveEqualizationHistogram.h:138
itk::Math::abs
bool abs(bool x)
Definition: itkMath.h:844
itk::Function::AdaptiveEqualizationHistogram::m_KernelSize
RealType m_KernelSize
Definition: itkAdaptiveEqualizationHistogram.h:136
itk::Function::AdaptiveEqualizationHistogram::SetMinimum
void SetMinimum(TInputPixel minimum)
Definition: itkAdaptiveEqualizationHistogram.h:123
itk::Function::AdaptiveEqualizationHistogram::m_Beta
RealType m_Beta
Definition: itkAdaptiveEqualizationHistogram.h:135
itk::Function::AdaptiveEqualizationHistogram::GetValue
TOutputPixel GetValue(const TInputPixel &pixel)
Definition: itkAdaptiveEqualizationHistogram.h:71
itk::Function::AdaptiveEqualizationHistogram::RemovePixel
void RemovePixel(const TInputPixel &p)
Definition: itkAdaptiveEqualizationHistogram.h:56
itk::Function::AdaptiveEqualizationHistogram::RealType
float RealType
Definition: itkAdaptiveEqualizationHistogram.h:43
itk::Function::AdaptiveEqualizationHistogram::m_Map
MapType m_Map
Definition: itkAdaptiveEqualizationHistogram.h:155
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkStructHashFunction.h
itk::Function::AdaptiveEqualizationHistogram::m_BoundaryCount
vcl_size_t m_BoundaryCount
Definition: itkAdaptiveEqualizationHistogram.h:156
itk::Function::AdaptiveEqualizationHistogram::SetBeta
void SetBeta(RealType beta)
Definition: itkAdaptiveEqualizationHistogram.h:112
itk::Function::AdaptiveEqualizationHistogram::SetKernelSize
void SetKernelSize(RealType kernelSize)
Definition: itkAdaptiveEqualizationHistogram.h:117
itk::Function::AdaptiveEqualizationHistogram::SetMaximum
void SetMaximum(TInputPixel maximum)
Definition: itkAdaptiveEqualizationHistogram.h:128
itk::Function::AdaptiveEqualizationHistogram::MapType
typename std::unordered_map< TInputPixel, vcl_size_t, StructHashFunction< TInputPixel > > MapType
Definition: itkAdaptiveEqualizationHistogram.h:152
itkMath.h