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() throw( te::rp::Exception );
174 
175  //overload
176  const InputParameters& operator=( const InputParameters& params );
177 
178  //overload
179  AbstractParameters* clone() const;
180  };
181 
182  /*!
183  \class OutputParameters
184  \brief Contrast output parameters
185  \details The result will be written to the raster instance pointed
186  by m_outRasterPtr (in this case the output bands must also be
187  passed by m_outRasterBands ); or written to a new raster instance
188  created inside the given data source pointed by m_outDataSourcePtr
189  (in this case the data set name must be supplied - m_outDataSetName ).
190  */
192  {
193  public:
194 
195  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).
196 
197  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.
198 
199  std::vector< unsigned int > m_outRasterBands; //!< Bands to be processed from the output raster.
200 
201  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.
202 
203  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.
204 
206 
208 
210 
211  //overload
212  void reset() throw( te::rp::Exception );
213 
214  //overload
215  const OutputParameters& operator=( const OutputParameters& params );
216 
217  //overload
218  AbstractParameters* clone() const;
219  };
220 
222 
224 
225  //overload
226  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
227 
228  //overload
229  void reset() throw( te::rp::Exception );
230 
231  //overload
232  bool initialize( const AlgorithmInputParameters& inputParams ) throw( te::rp::Exception );
233 
234  bool isInitialized() const;
235 
236  /*!
237  \brief Returns gain and offset values for contrast types (when applicable).
238  \param inRangeMin Input values range minimum value.
239  \param inRangeMax Input values range maximum value.
240  \param outRangeMin Output values range minimum value.
241  \param outRangeMax Output values range maximum value.
242  \param gain Calculated gain.
243  \param offset1 Calculated offset 1.
244  \param offset2 Calculated offset 2.
245  \return true if OK, false on errors.
246  */
247  static bool getGainAndOffset( const InputParameters::ContrastType& type,
248  const double& inRangeMin, const double& inRangeMax,
249  const double& outRangeMin, const double& outRangeMax,
250  double& gain, double& offset1, double& offset2 );
251 
252  protected:
253 
254  /*!
255  \brief Type definition for a remapping function pointer.
256  */
257  typedef void (Contrast::*RemapFuncPtrT)( const double& inValue,
258  double& outValue );
259 
260  Contrast::InputParameters m_inputParameters; //!< Contrast input execution parameters.
261  Contrast::OutputParameters* m_outputParametersPtr; //!< Contrast input execution parameters.
262 
263  bool m_isInitialized; //!< Tells if this instance is initialized.
264 
265  /*!
266  \brief Execute a linear contrast following the internal parameters
267  \return true if OK, false on errors.
268  */
269  bool execLinearContrast();
270 
271  /*!
272  \brief Execute the histogram equalization contrast following the internal parameters
273  \return true if OK, false on errors.
274  */
275  bool execHistogramEqualizationContrast();
276 
277  /*!
278  \brief Execute a square contrast following the internal parameters
279  \return true if OK, false on errors.
280  */
281  bool execSquareContrast();
282 
283  /*!
284  \brief Execute a square root contrast following the internal parameters
285  \return true if OK, false on errors.
286  */
287  bool execSquareRootContrast();
288 
289  /*!
290  \brief Execute a log contrast following the internal parameters
291  \return true if OK, false on errors.
292  */
293  bool execLogContrast();
294 
295  /*!
296  \brief Execute the histogram equalization contrast following the internal parameters
297  \return true if OK, false on errors.
298  */
299  bool execSetMeanAndStdContrast();
300 
301  /*!
302  \brief Execute the decorrelation enhancement contrast following the internal parameters
303  \return true if OK, false on errors.
304  */
305  bool execDecorrelationEnhancement();
306 
307  /*!
308  \brief Execute a cumulative linear contrast cutted by 2-98% following the internal parameters
309  \return true if OK, false on errors.
310  */
311  bool execCumulativeCut2Enhancement();
312 
313  /*!
314  \brief Band gray levels remap using a remap function.
315  \param inRaster Input Raster
316  \param inRasterBandIdx Input raster band index.
317  \param outRaster Output raster.
318  \param outRasterBandIdx Output raster band index.
319  \param remapFuncPtr The remap function pointer used.
320  \param enableProgress Enable the use of a progress interface.
321  \return true if OK, false on errors.
322  */
323  bool remapBandLevels( const te::rst::Raster& inRaster,
324  const unsigned int inRasterBandIdx,
325  te::rst::Raster& outRaster,
326  const unsigned int outRasterBandIdx,
327  RemapFuncPtrT remapFuncPtr,
328  const bool enableProgress );
329 
330  // Variables used by offSetGainRemap
331  double m_offSetGainRemap_offset1;
332  double m_offSetGainRemap_offset2;
333  double m_offSetGainRemap_gain;
334 
335  // Variables used by offSetGainRemap
336  double m_squareRemap_gain;
337 
338  // Variables used by offSetGainRemap
339  double m_squareRootRemap_gain;
340 
341  // Variables used by offSetGainRemap
342  double m_logRemap_gain;
343  double m_logRemap_offset;
344 
345  // Variables used by DecorrelationEnhancementRemap
346  double m_decorrelationEnhancementRemap_gain;
347  double m_decorrelationEnhancementRemap_offset1;
348  double m_decorrelationEnhancementRemap_offset2;
349 
350  // Variables used by HistogramEqualizationRemap
351  double m_histogramEqualizationRemap_cDFMin;
352  double m_histogramEqualizationRemap_gain;
353  std::map<double, double> m_histogramEqualizationRemap_cdf;
354 
355  /*!
356  \brief Remap on gray level using an offset and gain.
357  \param inValue Input gray level.
358  \param outValue Output gray level.
359  \note outValue = ( ( inValue + m_offSetGainRemap_offset1 ) * m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2
360  */
361  inline void offSetGainRemap( const double& inValue, double& outValue )
362  {
363  outValue = ( ( inValue + m_offSetGainRemap_offset1 ) *
364  m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2;
365  };
366 
367  /*!
368  \brief Remap on gray level using a square.
369  \param inValue Input gray level.
370  \param outValue Output gray level.
371  \note outValue = m_squareRemap_factor * std::pow( inValue, 2.0 )
372  */
373  inline void squareRemap( const double& inValue, double& outValue )
374  {
375  outValue = m_squareRemap_gain * std::pow( inValue, 2.0 );
376  };
377 
378  /*!
379  \brief Remap on gray level using a square root.
380  \param inValue Input gray level.
381  \param outValue Output gray level.
382  \note outValue = m_squareRootRemap_gain * std::sqrt( inValue );
383  */
384  inline void squareRootRemap( const double& inValue, double& outValue )
385  {
386  outValue = m_squareRootRemap_gain * std::sqrt( inValue );
387  };
388 
389  /*!
390  \brief Remap on gray level using a log.
391  \param inValue Input gray level.
392  \param outValue Output gray level.
393  \note outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
394  */
395  inline void logRemap( const double& inValue, double& outValue )
396  {
397  outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
398  };
399 
400  /*!
401  \brief Remap on gray level using two offsets and gain.
402  \param inValue Input gray level.
403  \param outValue Output gray level.
404  */
405  inline void DecorrelationEnhancementRemap( const double& inValue, double& outValue )
406  {
407  outValue = ( ( inValue + m_decorrelationEnhancementRemap_offset1 ) *
408  m_decorrelationEnhancementRemap_gain )
409  + m_decorrelationEnhancementRemap_offset2;
410  };
411 
412  /*!
413  \brief Remap on gray level using two offsets and gain.
414  \param inValue Input gray level.
415  \param outValue Output gray level.
416  */
417  inline void histogramEqualizationRemap( const double& inValue, double& outValue )
418  {
419  outValue = ( ( m_histogramEqualizationRemap_cdf.lower_bound( inValue )->second ) - m_histogramEqualizationRemap_cDFMin ) *
420  m_histogramEqualizationRemap_gain;
421  };
422  };
423 
424  } // end namespace rp
425 } // end namespace te
426 
427 #endif
428 
te::rp::Contrast::InputParameters::m_sMASCMeanInput
std::vector< double > m_sMASCMeanInput
The mean greyscale to be applied in each band.
Definition: Contrast.h:162
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rp::AlgorithmOutputParameters
Raster Processing algorithm output parameters base interface.
Definition: AlgorithmOutputParameters.h:40
te::rp::Contrast::InputParameters::m_type
ContrastType m_type
The contrast type to be applied.
Definition: Contrast.h:88
te::rp::Contrast::InputParameters::m_squareRootCMinInput
std::vector< double > m_squareRootCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:140
te::rp::Contrast::squareRootRemap
void squareRootRemap(const double &inValue, double &outValue)
Remap on gray level using a square root.
Definition: Contrast.h:384
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rp::Contrast::InputParameters::DecorrelationEnhancementT
@ DecorrelationEnhancementT
Definition: Contrast.h:84
te::rp::Contrast::InputParameters::InputParameters
InputParameters()
te::rp::AlgorithmInputParameters
Raster Processing algorithm input parameters base interface.
Definition: AlgorithmInputParameters.h:40
te::rp::Contrast::InputParameters::m_lCMinInput
std::vector< double > m_lCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:109
te::rp::Contrast::InputParameters::m_hECMaxInput
std::vector< double > m_hECMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:120
te::rp::Contrast::OutputParameters::OutputParameters
OutputParameters(const OutputParameters &)
te::rp::Contrast::InputParameters::InvalidContrastT
@ InvalidContrastT
Definition: Contrast.h:77
te::rp::Contrast::InputParameters::SquareContrastT
@ SquareContrastT
Definition: Contrast.h:80
te::rp::Contrast::OutputParameters
Contrast output parameters.
Definition: Contrast.h:192
te::rp::Contrast::InputParameters::reset
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
te::rp::Contrast::InputParameters::MeanAndStdContrastT
@ MeanAndStdContrastT
Definition: Contrast.h:83
te::rp::Contrast
Contrast enhancement.
Definition: Contrast.h:58
te::rp::Contrast::InputParameters::m_squareCMinInput
std::vector< double > m_squareCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:129
te::rp::Contrast::InputParameters::m_outRangeMin
double m_outRangeMin
Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
Definition: Contrast.h:94
te::rp::Algorithm
Raster Processing algorithm base interface.
Definition: Algorithm.h:42
te::rp::Contrast::InputParameters
Contrast input parameters.
Definition: Contrast.h:66
te::rp::Contrast::DecorrelationEnhancementRemap
void DecorrelationEnhancementRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:405
te::rp::Contrast::OutputParameters::reset
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
te::rp::Contrast::InputParameters::HistogramEqualizationContrastT
@ HistogramEqualizationContrastT
Definition: Contrast.h:79
te::rp::Contrast::InputParameters::LinearContrastT
@ LinearContrastT
Definition: Contrast.h:78
te::rp::Contrast::InputParameters::m_sampled
bool m_sampled
Enable/Disable uses all values to calculate histogram.
Definition: Contrast.h:100
te::Exception
Base exception class for plugin module.
Definition: Exception.h:42
te::rp::Contrast::OutputParameters::m_createdOutRasterInfo
std::map< std::string, std::string > m_createdOutRasterInfo
The necessary information to create the raster (as described in te::raster::RasterFactory),...
Definition: Contrast.h:203
te::rp::Contrast::InputParameters::m_outRangeMax
double m_outRangeMax
Maximum value of data type range (default: std::numeric_limits<double>::max().
Definition: Contrast.h:96
te::rp::Contrast::OutputParameters::~OutputParameters
~OutputParameters()
TERPEXPORT
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
te::rp::Contrast::InputParameters::m_logCMaxInput
std::vector< double > m_logCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:153
te::rp::Contrast::OutputParameters::m_outRasterBands
std::vector< unsigned int > m_outRasterBands
Bands to be processed from the output raster.
Definition: Contrast.h:199
te::rp::Contrast::InputParameters::~InputParameters
~InputParameters()
te::rp::Contrast::histogramEqualizationRemap
void histogramEqualizationRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:417
te::rp::Contrast::InputParameters::m_lCMaxInput
std::vector< double > m_lCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:111
te::rp::Contrast::InputParameters::m_squareCMaxInput
std::vector< double > m_squareCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:131
te::rp::Contrast::InputParameters::SquareRootContrastT
@ SquareRootContrastT
Definition: Contrast.h:81
te::rp::Contrast::InputParameters::m_logCMinInput
std::vector< double > m_logCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:151
te::rp::Contrast::InputParameters::m_squareRootCMaxInput
std::vector< double > m_squareRootCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:142
te::rp::Contrast::InputParameters::m_inRasterPtr
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Contrast.h:90
Algorithm.h
Abstract algorithm.
te::rp::Contrast::InputParameters::m_sMASCStdInput
std::vector< double > m_sMASCStdInput
The standard deviation to be applied in each band.
Definition: Contrast.h:164
te::rp::Contrast::OutputParameters::m_createdOutRasterDSType
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Definition: Contrast.h:201
te::rp::Contrast::OutputParameters::OutputParameters
OutputParameters()
te::rp::Contrast::InputParameters::m_inRasterBands
std::vector< unsigned int > m_inRasterBands
Bands to be processed from the input raster.
Definition: Contrast.h:92
te::rp::Contrast::InputParameters::m_enableProgress
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Contrast.h:98
te::rp::Contrast::squareRemap
void squareRemap(const double &inValue, double &outValue)
Remap on gray level using a square.
Definition: Contrast.h:373
te::rp::Contrast::OutputParameters::m_outRasterPtr
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:195
te::rp::Contrast::InputParameters::LogContrastT
@ LogContrastT
Definition: Contrast.h:82
te::rp::Contrast::logRemap
void logRemap(const double &inValue, double &outValue)
Remap on gray level using a log.
Definition: Contrast.h:395
te::rp::Contrast::OutputParameters::m_createdOutRasterPtr
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:197
te::rp::Contrast::InputParameters::ContrastType
ContrastType
Definition: Contrast.h:76