File: itkAdaptiveHistogramEqualizationImageFilter.hxx

package info (click to toggle)
insighttoolkit4 4.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408,860 kB
  • ctags: 151,204
  • sloc: cpp: 633,356; ansic: 403,038; xml: 51,513; fortran: 34,250; python: 15,831; sh: 2,501; lisp: 2,070; tcl: 1,035; java: 710; makefile: 605; yacc: 323; perl: 200; csh: 195; lex: 177; pascal: 69; cs: 35; ruby: 10
file content (288 lines) | stat: -rw-r--r-- 9,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef __itkAdaptiveHistogramEqualizationImageFilter_hxx
#define __itkAdaptiveHistogramEqualizationImageFilter_hxx

#include <map>
#include <set>
#include "vnl/vnl_math.h"

#include "itkAdaptiveHistogramEqualizationImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkProgressReporter.h"

namespace itk
{
template< typename TImageType >
float
AdaptiveHistogramEqualizationImageFilter< TImageType >
::CumulativeFunction(float u, float v)
{
  // Calculate cumulative function
  float s, ad;

  s = vnl_math_sgn(u - v);
  ad = vnl_math_abs( 2.0 * ( u - v ) );

  return 0.5 *s *std::pow(ad, m_Alpha) - m_Beta * 0.5 * s * ad + m_Beta * u;
}

template< typename TImageType >
void
AdaptiveHistogramEqualizationImageFilter< TImageType >
::GenerateData()
{
  typename ImageType::ConstPointer input = this->GetInput();
  typename ImageType::Pointer output = this->GetOutput();

  // Allocate the output
  this->AllocateOutputs();

  unsigned int i;

  //Set the kernel value of PLAHE algorithm
  float kernel = 1;
  for ( i = 0; i < ImageDimension; i++ )
    {
    kernel = kernel * ( 2 * this->GetRadius()[i] + 1 );
    }
  kernel = 1 / kernel;

  // Iterator which traverse the input
  ImageRegionConstIterator< ImageType > itInput( input,
                                                 input->GetRequestedRegion() );

  // Calculate min and max gray level of an input image
  double min = static_cast< double >( itInput.Get() );
  double max = min;
  double value;
  while ( !itInput.IsAtEnd() )
    {
    value = static_cast< double >( itInput.Get() );
    if ( min > value )
      {
      min = value;
      }
    if ( max < value )
      {
      max = value;
      }
    ++itInput;
    }

  // Allocate a float type image which has the same size with an input image.
  // This image store normalized pixel values [-0.5 0.5] of the input image.
  typedef Image< float, ImageDimension > ImageFloatType;
  typename ImageFloatType::Pointer inputFloat = ImageFloatType::New();
  inputFloat->SetRegions( input->GetRequestedRegion() );
  inputFloat->Allocate();

  // Scale factors to convert back and forth to the [-0.5, 0.5] and
  // original gray level range
  float iscale = max - min;
  float scale = (float)1 / iscale;

  // Normalize input image to [-0.5 0.5] gray level and store in
  // inputFloat. AdaptiveHistogramEqualization only use float type
  // image which has gray range [-0.5 0.5]
  ImageRegionIterator< ImageFloatType > itFloat( inputFloat,
                                                 input->GetRequestedRegion() );

  itInput.GoToBegin(); // rewind the previous input iterator
  while ( !itInput.IsAtEnd() )
    {
    itFloat.Set(scale * ( itInput.Get() - min ) - 0.5);
    ++itFloat;
    ++itInput;
    }

  // Calculate cumulative array which will store the value of
  // cumulative function. During the AdaptiveHistogramEqualization
  // process, cumulative function will not be calculated, instead the
  // cumulative function will be pre-calculated and result will be
  // stored in cumulative array.  The cumulative array will be
  // referenced during AdaptiveHistogramEqualization processing.  This
  // pre-calculation can reduce computation time even though this
  // method uses a huge array.  If the cumulative array can not be
  // assigned, the cumulative function will be calculated each time.
  //
  //
  bool cachedCumulative = false;

  typedef std::set< float > FloatSetType;
  FloatSetType row;

  typedef std::map< std::pair< float, float >, float > ArrayMapType;
  ArrayMapType CumulativeArray;

  if ( m_UseLookupTable )
    {
    // determine what intensities are used on the input
    itFloat.GoToBegin(); // rewind the iterator for the normalized image
    while ( !itFloat.IsAtEnd() )
      {
      row.insert( itFloat.Get() );
      ++itFloat;
      }
    // only cache the array if it can be done without taking too much space
    if ( row.size() < ( input->GetRequestedRegion().GetNumberOfPixels() / 10 ) )
      {
      cachedCumulative = true;
      }
    else
      {
      cachedCumulative = false;
      row.clear();
      }

    if ( cachedCumulative )
      {
      // calculate cumulative function for each possible pairing of
      // intensities and store result in cumulative array
      FloatSetType::iterator itU, itV;
      ArrayMapType::key_type key;
      for ( itU = row.begin(); itU != row.end(); ++itU )
        {
        key.first = *itU;
        for ( itV = row.begin(); itV != row.end(); ++itV )
          {
          key.second = *itV;
          CumulativeArray.insert( ArrayMapType::value_type( key,
                                                            this->CumulativeFunction(*itU, *itV) ) );
          }
        }
      }
    }

  // Setup for processing the image
  //
  ZeroFluxNeumannBoundaryCondition< ImageFloatType > nbc;
  ConstNeighborhoodIterator< ImageFloatType >        bit;

  // Find the data-set boundary "faces"
  typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< ImageFloatType >::FaceListType faceList;
  NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< ImageFloatType > bC;
  faceList = bC( inputFloat, output->GetRequestedRegion(), this->GetRadius() );

  typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< ImageFloatType >::FaceListType::iterator fit;

  // Map stores (number of pixel)/(window size) for each gray value.
  typedef std::map< float, float > MapType;
  MapType           count;
  MapType::iterator itMap;

  ProgressReporter progress( this, 0, output->GetRequestedRegion().GetNumberOfPixels() );

  // Process each faces.  These are N-d regions which border
  // the edge of the buffer.
  for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
    {
    // Create a neighborhood iterator for the normalized image for the
    // region for this face
    bit = ConstNeighborhoodIterator< ImageFloatType >(this->GetRadius(),
                                                      inputFloat, *fit);
    bit.OverrideBoundaryCondition(&nbc);
    bit.GoToBegin();
    unsigned int neighborhoodSize = bit.Size();

    // iterator for the output for this face
    ImageRegionIterator< ImageType > itOut(output, *fit);

    // iterate over the region for this face
    ArrayMapType::key_type key;
    while ( !bit.IsAtEnd() )
      {
      // AdaptiveHistogramEqualization algorithm
      //
      //
      float f;
      float sum;

      // "Histogram the window"
      count.clear();
      for ( i = 0; i < neighborhoodSize; ++i )
        {
        f = bit.GetPixel(i);
        itMap = count.find(f);
        if ( itMap != count.end() )
          {
          count[f] = count[f] + kernel;
          }
        else
          {
          count.insert( MapType::value_type(f, kernel) );
          }
        }

      typedef typename ImageType::PixelType PixelType;

      // if we cached the cumulative array
      // if not, use CumulativeFunction()
      if ( cachedCumulative )
        {
        sum = 0;
        itMap = count.begin();
        f = bit.GetCenterPixel();
        key.first = f;
        while ( itMap != count.end() )
          {
          key.second = itMap->first;
          sum = sum
                + itMap->second * CumulativeArray[key];
          ++itMap;
          }
        itOut.Set( (PixelType)( iscale * ( sum + 0.5 ) + min ) );
        }
      else
        {
        sum = 0;
        itMap = count.begin();
        f = bit.GetCenterPixel();
        while ( itMap != count.end() )
          {
          sum = sum + itMap->second *CumulativeFunction(f, itMap->first);
          ++itMap;
          }
        itOut.Set( (PixelType)( iscale * ( sum + 0.5 ) + min ) );
        }

      // move the neighborhood
      ++bit;
      ++itOut;
      progress.CompletedPixel();
      }
    }
}

template< typename TImageType >
void
AdaptiveHistogramEqualizationImageFilter< TImageType >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << "Alpha: " << m_Alpha << std::endl;
  os << "Beta: " << m_Beta << std::endl;
  os << "UseLookupTable: " << ( m_UseLookupTable ? "On" : "Off" ) << std::endl;
}
} // end namespace

#endif