ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkAdaptiveEqualizationHistogram.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 "itksys/hash_map.hxx"
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 
44  using RealType = float;
45 
47 
48  {
49  }
50 
51  // ~AdaptiveEqualizationHistogram() {} default is ok
52 
53  void AddPixel(const TInputPixel & p)
54  {
55  m_Map[p]++;
56  }
57 
58  void RemovePixel(const TInputPixel & p)
59  {
60 
61  // insert new item if one doesn't exist
62  typename MapType::iterator it = m_Map.find( p );
63 
64  itkAssertInDebugAndIgnoreInReleaseMacro( it != m_Map.end() );
65 
66  if ( --(it->second) == 0 )
67  {
68  m_Map.erase( it );
69  }
70 
71  }
72 
73  TOutputPixel GetValue(const TInputPixel &pixel)
74  {
75 
76  // Normalize input pixels to [-0.5 0.5] gray level.
77  // AdaptiveHistogramEqualization compute kernel components with
78  // float, but use double for accumulate and temporaries.
79  const double iscale = (double)m_Maximum - m_Minimum;
80 
81  double sum = 0.0;
82  typename MapType::iterator itMap = m_Map.begin();
83  const RealType u = ( (double)pixel - m_Minimum ) / iscale - 0.5;
84  while ( itMap != m_Map.end() )
85  {
86  const RealType v = ( (double)itMap->first - m_Minimum ) / iscale - 0.5;
87  const double ikernel = m_KernelSize - m_BoundaryCount;
88  sum += itMap->second * CumulativeFunction(u,v) / ikernel;
89 
90  ++itMap;
91  }
92 
93  return (TOutputPixel)( iscale * ( sum + 0.5 ) + m_Minimum );
94  }
95 
97 
99 
100  void SetAlpha( RealType alpha ) {m_Alpha=alpha;}
101  void SetBeta( RealType beta ) {m_Beta=beta;}
102  void SetKernelSize( RealType kernelSize ) {m_KernelSize=kernelSize;}
103 
104  void SetMinimum( TInputPixel minimum ) {m_Minimum=minimum;}
105  void SetMaximum( TInputPixel maximum ) {m_Maximum=maximum;}
106 
107 private:
111 
112  TInputPixel m_Minimum;
113  TInputPixel m_Maximum;
114 
116  {
117  // Calculate cumulative function
118  const RealType s = itk::Math::sgn(u - v);
119  const RealType ad = itk::Math::abs( 2.0 * ( u - v ) );
120 
121  return 0.5 * s * std::pow(ad, m_Alpha) - m_Beta * 0.5 * s * ad + m_Beta * u;
122  }
123 
124 private:
125  using MapType = typename itksys::hash_map< TInputPixel,
126  size_t,
128 
129 
131  size_t m_BoundaryCount{0};
132 
133 };
134 
135 } // end namespace Function
136 } // end namespace itk
137 
138 #endif // itkAdaptiveHistogramHistogram_h
typename itksys::hash_map< TInputPixel, vcl_size_t, StructHashFunction< TInputPixel > > MapType
Generic hash function for an arbitrary struct (or class).