Contrast.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/Contrast.h
22  \brief Contrast enhancement.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_CONTRAST_H
26 #define __TERRALIB_RP_INTERNAL_CONTRAST_H
27 
28 #include "Algorithm.h"
29 
30 #include <vector>
31 #include <string>
32 #include <map>
33 #include <memory>
34 #include <cmath>
35 
36 namespace te
37 {
38  namespace da
39  {
40  class DataSource;
41  }
42 
43  namespace rst
44  {
45  class Raster;
46  class Band;
47  }
48 
49  namespace rp
50  {
51  /*!
52  \class Contrast
53  \brief Contrast enhancement.
54  \details Apply contrast enhencement on the selected bands.
55  \ingroup rp_enh
56  */
57  class TERPEXPORT Contrast : public Algorithm
58  {
59  public:
60 
61  /*!
62  \class InputParameters
63  \brief Contrast input parameters
64  */
66  {
67  public:
68 
69  /*!
70  \name Global parameters
71  */
72  /**@{*/
73 
74  /*! \enum Allowed contrast types. */
76  {
77  InvalidContrastT, /*!< Invalid contrast. */
78  LinearContrastT, /*!< The histogram range will be changed to the supplied min/max range ( linear function ). */
79  HistogramEqualizationContrastT, /*!< The histogram of the image will be equalized automatically. */
80  SquareContrastT, /*!< The contrasted image will be created by using a square function. */
81  SquareRootContrastT, /*!< The contrasted image will be created by using a square root function. */
82  LogContrastT, /*!< The contrasted image will be created by using a log function. */
83  MeanAndStdContrastT, /*!< The contrasted image will have a predefined mean and standard deviation normalization. */
84  DecorrelationEnhancementT, /*!< Decorrelation Enhancement using principal components. */
85  CumulativeCut2EnhancementT /*!< Linear cumulative cutted in 2 - 98%. */
86  };
87 
88  ContrastType m_type; //!< The contrast type to be applied.
89 
90  te::rst::Raster const* m_inRasterPtr; //!< Input raster.
91 
92  std::vector< unsigned int > m_inRasterBands; //!< Bands to be processed from the input raster.
93 
94  double m_outRangeMin; //!< Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
95 
96  double m_outRangeMax; //!< Maximum value of data type range (default: std::numeric_limits<double>::max().
97 
98  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
99 
100  bool m_sampled; //!< Enable/Disable uses all values to calculate histogram
101 
102  //@}
103 
104  /*!
105  \name Linear contrast parameters
106  */
107  /**@{*/
108 
109  std::vector< double > m_lCMinInput; //!< The contrast minimum input greyscale value of each band.
110 
111  std::vector< double > m_lCMaxInput; //!< The contrast maximum input greyscale value of each band.
112 
113  //@}
114 
115  /*!
116  \name Histogram equalization contrast parameters
117  */
118  /**@{*/
119 
120  std::vector< double > m_hECMaxInput; //!< The contrast maximum input greyscale value of each band.
121 
122  //@}
123 
124  /*!
125  \name Square contrast parameters
126  */
127  /**@{*/
128 
129  std::vector< double > m_squareCMinInput; //!< The contrast minimum input greyscale value of each band.
130 
131  std::vector< double > m_squareCMaxInput; //!< The contrast maximum input greyscale value of each band.
132 
133  //@}
134 
135  /*!
136  \name Square root contrast parameters
137  */
138  /**@{*/
139 
140  std::vector< double > m_squareRootCMinInput; //!< The contrast minimum input greyscale value of each band.
141 
142  std::vector< double > m_squareRootCMaxInput; //!< The contrast maximum input greyscale value of each band.
143 
144  //@}
145 
146  /*!
147  \name Log contrast parameters
148  */
149  /**@{*/
150 
151  std::vector< double > m_logCMinInput; //!< The contrast minimum input greyscale value of each band.
152 
153  std::vector< double > m_logCMaxInput; //!< The contrast maximum input greyscale value of each band.
154 
155  //@}
156 
157  /*!
158  \name Mean and standard deviation normalization contrast parameters
159  */
160  /**@{*/
161 
162  std::vector< double > m_sMASCMeanInput; //!< The mean greyscale to be applied in each band.
163 
164  std::vector< double > m_sMASCStdInput; //!< The standard deviation to be applied in each band.
165 
166  //@}
167 
169 
171 
172  //overload
173  void reset() ;
174 
175  //overload
176  const InputParameters& operator=( const InputParameters& params );
177 
178  //overload
179  AbstractParameters* clone() const;
180 
181  //overload
182  bool serialize ( AlgorithmParametersSerializer& serializer ) const;
183  };
184 
185  /*!
186  \class OutputParameters
187  \brief Contrast output parameters
188  \details The result will be written to the raster instance pointed
189  by m_outRasterPtr (in this case the output bands must also be
190  passed by m_outRasterBands ); or written to a new raster instance
191  created inside the given data source pointed by m_outDataSourcePtr
192  (in this case the data set name must be supplied - m_outDataSetName ).
193  */
195  {
196  public:
197 
198  te::rst::Raster* m_outRasterPtr; //!< A pointer to a valid initiated raster instance where the result must be written, leave NULL to create a new instance(in this case the other output parameters must be used).
199 
200  std::unique_ptr< te::rst::Raster > m_createdOutRasterPtr; //!< A pointer to the created output raster instance, or an empty pointer empty if the result must be written to the raster pointed m_outRasterPtr.
201 
202  std::vector< unsigned int > m_outRasterBands; //!< Bands to be processed from the output raster.
203 
204  std::string m_createdOutRasterDSType; //!< Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the result must be written to the raster pointed m_outRasterPtr.
205 
206  std::map< std::string, std::string > m_createdOutRasterInfo; //!< The necessary information to create the raster (as described in te::raster::RasterFactory), leave empty if the result must be written to the raster pointed m_outRasterPtr.
207 
209 
211 
213 
214  //overload
215  void reset() ;
216 
217  //overload
218  const OutputParameters& operator=( const OutputParameters& params );
219 
220  //overload
221  AbstractParameters* clone() const;
222  };
223 
225 
227 
228  //overload
229  bool execute( AlgorithmOutputParameters& outputParams ) ;
230 
231  //overload
232  void reset() ;
233 
234  //overload
235  bool initialize( const AlgorithmInputParameters& inputParams ) ;
236 
237  bool isInitialized() const;
238 
239  /*!
240  \brief Returns gain and offset values for contrast types (when applicable).
241  \param inRangeMin Input values range minimum value.
242  \param inRangeMax Input values range maximum value.
243  \param outRangeMin Output values range minimum value.
244  \param outRangeMax Output values range maximum value.
245  \param gain Calculated gain.
246  \param offset1 Calculated offset 1.
247  \param offset2 Calculated offset 2.
248  \return true if OK, false on errors.
249  */
251  const double& inRangeMin, const double& inRangeMax,
252  const double& outRangeMin, const double& outRangeMax,
253  double& gain, double& offset1, double& offset2 );
254 
255  protected:
256 
257  /*!
258  \brief Type definition for a remapping function pointer.
259  */
260  typedef void (Contrast::*RemapFuncPtrT)( const double& inValue,
261  double& outValue );
262 
263  Contrast::InputParameters m_inputParameters; //!< Contrast input execution parameters.
264  Contrast::OutputParameters* m_outputParametersPtr; //!< Contrast input execution parameters.
265 
266  bool m_isInitialized; //!< Tells if this instance is initialized.
267 
268  /*!
269  \brief Execute a linear contrast following the internal parameters
270  \return true if OK, false on errors.
271  */
273 
274  /*!
275  \brief Execute the histogram equalization contrast following the internal parameters
276  \return true if OK, false on errors.
277  */
279 
280  /*!
281  \brief Execute a square contrast following the internal parameters
282  \return true if OK, false on errors.
283  */
285 
286  /*!
287  \brief Execute a square root contrast following the internal parameters
288  \return true if OK, false on errors.
289  */
291 
292  /*!
293  \brief Execute a log contrast following the internal parameters
294  \return true if OK, false on errors.
295  */
297 
298  /*!
299  \brief Execute the histogram equalization contrast following the internal parameters
300  \return true if OK, false on errors.
301  */
303 
304  /*!
305  \brief Execute the decorrelation enhancement contrast following the internal parameters
306  \return true if OK, false on errors.
307  */
309 
310  /*!
311  \brief Execute a cumulative linear contrast cutted by 2-98% following the internal parameters
312  \return true if OK, false on errors.
313  */
315 
316  /*!
317  \brief Band gray levels remap using a remap function.
318  \param inRaster Input Raster
319  \param inRasterBandIdx Input raster band index.
320  \param outRaster Output raster.
321  \param outRasterBandIdx Output raster band index.
322  \param remapFuncPtr The remap function pointer used.
323  \param enableProgress Enable the use of a progress interface.
324  \return true if OK, false on errors.
325  */
326  bool remapBandLevels( const te::rst::Raster& inRaster,
327  const unsigned int inRasterBandIdx,
328  te::rst::Raster& outRaster,
329  const unsigned int outRasterBandIdx,
330  RemapFuncPtrT remapFuncPtr,
331  const bool enableProgress );
332 
333  // Variables used by offSetGainRemap
337 
338  // Variables used by offSetGainRemap
340 
341  // Variables used by offSetGainRemap
343 
344  // Variables used by offSetGainRemap
347 
348  // Variables used by DecorrelationEnhancementRemap
352 
353  // Variables used by HistogramEqualizationRemap
356  std::map<double, double> m_histogramEqualizationRemap_cdf;
357 
358  /*!
359  \brief Remap on gray level using an offset and gain.
360  \param inValue Input gray level.
361  \param outValue Output gray level.
362  \note outValue = ( ( inValue + m_offSetGainRemap_offset1 ) * m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2
363  */
364  inline void offSetGainRemap( const double& inValue, double& outValue )
365  {
366  outValue = ( ( inValue + m_offSetGainRemap_offset1 ) *
367  m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2;
368  };
369 
370  /*!
371  \brief Remap on gray level using a square.
372  \param inValue Input gray level.
373  \param outValue Output gray level.
374  \note outValue = m_squareRemap_factor * std::pow( inValue, 2.0 )
375  */
376  inline void squareRemap( const double& inValue, double& outValue )
377  {
378  outValue = m_squareRemap_gain * std::pow( inValue, 2.0 );
379  };
380 
381  /*!
382  \brief Remap on gray level using a square root.
383  \param inValue Input gray level.
384  \param outValue Output gray level.
385  \note outValue = m_squareRootRemap_gain * std::sqrt( inValue );
386  */
387  inline void squareRootRemap( const double& inValue, double& outValue )
388  {
389  outValue = m_squareRootRemap_gain * std::sqrt( inValue );
390  };
391 
392  /*!
393  \brief Remap on gray level using a log.
394  \param inValue Input gray level.
395  \param outValue Output gray level.
396  \note outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
397  */
398  inline void logRemap( const double& inValue, double& outValue )
399  {
400  outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
401  };
402 
403  /*!
404  \brief Remap on gray level using two offsets and gain.
405  \param inValue Input gray level.
406  \param outValue Output gray level.
407  */
408  inline void DecorrelationEnhancementRemap( const double& inValue, double& outValue )
409  {
410  outValue = ( ( inValue + m_decorrelationEnhancementRemap_offset1 ) *
411  m_decorrelationEnhancementRemap_gain )
412  + m_decorrelationEnhancementRemap_offset2;
413  };
414 
415  /*!
416  \brief Remap on gray level using two offsets and gain.
417  \param inValue Input gray level.
418  \param outValue Output gray level.
419  */
420  inline void histogramEqualizationRemap( const double& inValue, double& outValue )
421  {
422  outValue = ( ( m_histogramEqualizationRemap_cdf.lower_bound( inValue )->second ) - m_histogramEqualizationRemap_cDFMin ) *
423  m_histogramEqualizationRemap_gain;
424  };
425  };
426 
427  } // end namespace rp
428 } // end namespace te
429 
430 #endif
431 
Raster Processing algorithm input parameters base interface.
Raster Processing algorithm output parameters base interface.
A class to standardize algorithm parameters serialization.
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
Contrast input parameters.
Definition: Contrast.h:66
std::vector< double > m_squareRootCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:140
std::vector< double > m_squareCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:129
std::vector< double > m_lCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:109
bool m_sampled
Enable/Disable uses all values to calculate histogram.
Definition: Contrast.h:100
std::vector< unsigned int > m_inRasterBands
Bands to be processed from the input raster.
Definition: Contrast.h:92
std::vector< double > m_logCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:153
std::vector< double > m_sMASCStdInput
The standard deviation to be applied in each band.
Definition: Contrast.h:164
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Contrast.h:98
ContrastType m_type
The contrast type to be applied.
Definition: Contrast.h:88
double m_outRangeMin
Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
Definition: Contrast.h:94
std::vector< double > m_squareCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:131
const InputParameters & operator=(const InputParameters &params)
AbstractParameters * clone() const
Create a clone copy of this instance.
std::vector< double > m_lCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:111
std::vector< double > m_logCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:151
double m_outRangeMax
Maximum value of data type range (default: std::numeric_limits<double>::max().
Definition: Contrast.h:96
bool serialize(AlgorithmParametersSerializer &serializer) const
Returns a parameter serialization object.
std::vector< double > m_sMASCMeanInput
The mean greyscale to be applied in each band.
Definition: Contrast.h:162
std::vector< double > m_hECMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:120
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Contrast.h:90
std::vector< double > m_squareRootCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:142
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
Contrast output parameters.
Definition: Contrast.h:195
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Definition: Contrast.h:204
const OutputParameters & operator=(const OutputParameters &params)
AbstractParameters * clone() const
Create a clone copy of this instance.
std::vector< unsigned int > m_outRasterBands
Bands to be processed from the output raster.
Definition: Contrast.h:202
te::rst::Raster * m_outRasterPtr
A pointer to a valid initiated raster instance where the result must be written, leave NULL to create...
Definition: Contrast.h:198
std::map< std::string, std::string > m_createdOutRasterInfo
The necessary information to create the raster (as described in te::raster::RasterFactory),...
Definition: Contrast.h:206
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
std::unique_ptr< te::rst::Raster > m_createdOutRasterPtr
A pointer to the created output raster instance, or an empty pointer empty if the result must be writ...
Definition: Contrast.h:200
OutputParameters(const OutputParameters &)
Contrast enhancement.
Definition: Contrast.h:58
Contrast::InputParameters m_inputParameters
Contrast input execution parameters.
Definition: Contrast.h:263
bool remapBandLevels(const te::rst::Raster &inRaster, const unsigned int inRasterBandIdx, te::rst::Raster &outRaster, const unsigned int outRasterBandIdx, RemapFuncPtrT remapFuncPtr, const bool enableProgress)
Band gray levels remap using a remap function.
bool execSquareContrast()
Execute a square contrast following the internal parameters.
Contrast::OutputParameters * m_outputParametersPtr
Contrast input execution parameters.
Definition: Contrast.h:264
bool execLogContrast()
Execute a log contrast following the internal parameters.
bool execDecorrelationEnhancement()
Execute the decorrelation enhancement contrast following the internal parameters.
void DecorrelationEnhancementRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:408
void histogramEqualizationRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:420
void squareRemap(const double &inValue, double &outValue)
Remap on gray level using a square.
Definition: Contrast.h:376
double m_histogramEqualizationRemap_cDFMin
Definition: Contrast.h:354
bool execSquareRootContrast()
Execute a square root contrast following the internal parameters.
bool execLinearContrast()
Execute a linear contrast following the internal parameters.
double m_offSetGainRemap_gain
Definition: Contrast.h:336
static bool getGainAndOffset(const InputParameters::ContrastType &type, const double &inRangeMin, const double &inRangeMax, const double &outRangeMin, const double &outRangeMax, double &gain, double &offset1, double &offset2)
Returns gain and offset values for contrast types (when applicable).
bool execCumulativeCut2Enhancement()
Execute a cumulative linear contrast cutted by 2-98% following the internal parameters.
std::map< double, double > m_histogramEqualizationRemap_cdf
Definition: Contrast.h:356
double m_logRemap_offset
Definition: Contrast.h:346
double m_decorrelationEnhancementRemap_offset1
Definition: Contrast.h:350
bool m_isInitialized
Tells if this instance is initialized.
Definition: Contrast.h:266
double m_offSetGainRemap_offset1
Definition: Contrast.h:334
double m_logRemap_gain
Definition: Contrast.h:345
double m_offSetGainRemap_offset2
Definition: Contrast.h:335
double m_decorrelationEnhancementRemap_gain
Definition: Contrast.h:349
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
double m_histogramEqualizationRemap_gain
Definition: Contrast.h:355
double m_decorrelationEnhancementRemap_offset2
Definition: Contrast.h:351
bool execHistogramEqualizationContrast()
Execute the histogram equalization contrast following the internal parameters.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
void squareRootRemap(const double &inValue, double &outValue)
Remap on gray level using a square root.
Definition: Contrast.h:387
double m_squareRootRemap_gain
Definition: Contrast.h:342
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
bool execSetMeanAndStdContrast()
Execute the histogram equalization contrast following the internal parameters.
void offSetGainRemap(const double &inValue, double &outValue)
Remap on gray level using an offset and gain.
Definition: Contrast.h:364
double m_squareRemap_gain
Definition: Contrast.h:339
void logRemap(const double &inValue, double &outValue)
Remap on gray level using a log.
Definition: Contrast.h:398
An abstract class for raster data strucutures.
Definition: Raster.h:72
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Abstract algorithm.