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  };
86 
87  ContrastType m_type; //!< The contrast type to be applied.
88 
89  te::rst::Raster const* m_inRasterPtr; //!< Input raster.
90 
91  std::vector< unsigned int > m_inRasterBands; //!< Bands to be processed from the input raster.
92 
93  double m_outRangeMin; //!< Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
94 
95  double m_outRangeMax; //!< Maximum value of data type range (default: std::numeric_limits<double>::max().
96 
97  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
98 
99  //@}
100 
101  /*!
102  \name Linear contrast parameters
103  */
104  /**@{*/
105 
106  std::vector< double > m_lCMinInput; //!< The contrast minimum input greyscale value of each band.
107 
108  std::vector< double > m_lCMaxInput; //!< The contrast maximum input greyscale value of each band.
109 
110  //@}
111 
112  /*!
113  \name Histogram equalization contrast parameters
114  */
115  /**@{*/
116 
117  std::vector< double > m_hECMaxInput; //!< The contrast maximum input greyscale value of each band.
118 
119  //@}
120 
121  /*!
122  \name Square contrast parameters
123  */
124  /**@{*/
125 
126  std::vector< double > m_squareCMinInput; //!< The contrast minimum input greyscale value of each band.
127 
128  std::vector< double > m_squareCMaxInput; //!< The contrast maximum input greyscale value of each band.
129 
130  //@}
131 
132  /*!
133  \name Square root contrast parameters
134  */
135  /**@{*/
136 
137  std::vector< double > m_squareRootCMinInput; //!< The contrast minimum input greyscale value of each band.
138 
139  std::vector< double > m_squareRootCMaxInput; //!< The contrast maximum input greyscale value of each band.
140 
141  //@}
142 
143  /*!
144  \name Log contrast parameters
145  */
146  /**@{*/
147 
148  std::vector< double > m_logCMinInput; //!< The contrast minimum input greyscale value of each band.
149 
150  std::vector< double > m_logCMaxInput; //!< The contrast maximum input greyscale value of each band.
151 
152  //@}
153 
154  /*!
155  \name Mean and standard deviation normalization contrast parameters
156  */
157  /**@{*/
158 
159  std::vector< double > m_sMASCMeanInput; //!< The mean greyscale to be applied in each band.
160 
161  std::vector< double > m_sMASCStdInput; //!< The standard deviation to be applied in each band.
162 
163  //@}
164 
165  InputParameters();
166 
167  ~InputParameters();
168 
169  //overload
170  void reset() throw( te::rp::Exception );
171 
172  //overload
173  const InputParameters& operator=( const InputParameters& params );
174 
175  //overload
176  AbstractParameters* clone() const;
177  };
178 
179  /*!
180  \class OutputParameters
181  \brief Contrast output parameters
182  \details The result will be written to the raster instance pointed
183  by m_outRasterPtr (in this case the output bands must also be
184  passed by m_outRasterBands ); or written to a new raster instance
185  created inside the given data source pointed by m_outDataSourcePtr
186  (in this case the data set name must be supplied - m_outDataSetName ).
187  */
189  {
190  public:
191 
192  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).
193 
194  std::auto_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.
195 
196  std::vector< unsigned int > m_outRasterBands; //!< Bands to be processed from the output raster.
197 
198  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.
199 
200  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.
201 
203 
205 
206  ~OutputParameters();
207 
208  //overload
209  void reset() throw( te::rp::Exception );
210 
211  //overload
212  const OutputParameters& operator=( const OutputParameters& params );
213 
214  //overload
215  AbstractParameters* clone() const;
216  };
217 
218  Contrast();
219 
220  ~Contrast();
221 
222  //overload
223  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
224 
225  //overload
226  void reset() throw( te::rp::Exception );
227 
228  //overload
229  bool initialize( const AlgorithmInputParameters& inputParams ) throw( te::rp::Exception );
230 
231  bool isInitialized() const;
232 
233  /*!
234  \brief Returns gain and offset values for contrast types (when applicable).
235  \param inRangeMin Input values range minimum value.
236  \param inRangeMax Input values range maximum value.
237  \param outRangeMin Output values range minimum value.
238  \param outRangeMax Output values range maximum value.
239  \param gain Calculated gain.
240  \param offset1 Calculated offset 1.
241  \param offset2 Calculated offset 2.
242  \return true if OK, false on errors.
243  */
244  static bool getGainAndOffset( const InputParameters::ContrastType& type,
245  const double& inRangeMin, const double& inRangeMax,
246  const double& outRangeMin, const double& outRangeMax,
247  double& gain, double& offset1, double& offset2 );
248 
249  protected:
250 
251  /*!
252  \brief Type definition for a remapping function pointer.
253  */
254  typedef void (Contrast::*RemapFuncPtrT)( const double& inValue,
255  double& outValue );
256 
257  Contrast::InputParameters m_inputParameters; //!< Contrast input execution parameters.
258  Contrast::OutputParameters* m_outputParametersPtr; //!< Contrast input execution parameters.
259 
260  bool m_isInitialized; //!< Tells if this instance is initialized.
261 
262  /*!
263  \brief Execute a linear contrast following the internal parameters
264  \return true if OK, false on errors.
265  */
266  bool execLinearContrast();
267 
268  /*!
269  \brief Execute the histogram equalization contrast following the internal parameters
270  \return true if OK, false on errors.
271  */
272  bool execHistogramEqualizationContrast();
273 
274  /*!
275  \brief Execute a square contrast following the internal parameters
276  \return true if OK, false on errors.
277  */
278  bool execSquareContrast();
279 
280  /*!
281  \brief Execute a square root contrast following the internal parameters
282  \return true if OK, false on errors.
283  */
284  bool execSquareRootContrast();
285 
286  /*!
287  \brief Execute a log contrast following the internal parameters
288  \return true if OK, false on errors.
289  */
290  bool execLogContrast();
291 
292  /*!
293  \brief Execute the histogram equalization contrast following the internal parameters
294  \return true if OK, false on errors.
295  */
296  bool execSetMeanAndStdContrast();
297 
298  /*!
299  \brief Execute the decorrelation enhancement contrast following the internal parameters
300  \return true if OK, false on errors.
301  */
302  bool execDecorrelationEnhancement();
303 
304  /*!
305  \brief Band gray levels remap using a remap function.
306  \param inRasterBand Input raster band.
307  \param outRasterBand Output raster band.
308  \param remapFuncPtr The remap function pointer used.
309  \param enableProgress Enable the use of a progress interface.
310  \return true if OK, false on errors.
311  */
312  bool remapBandLevels( const te::rst::Band& inRasterBand,
313  te::rst::Band& outRasterBand, RemapFuncPtrT remapFuncPtr,
314  const bool enableProgress );
315 
316  // Variables used by offSetGainRemap
317  double m_offSetGainRemap_offset1;
318  double m_offSetGainRemap_offset2;
319  double m_offSetGainRemap_gain;
320 
321  // Variables used by offSetGainRemap
322  double m_squareRemap_gain;
323 
324  // Variables used by offSetGainRemap
325  double m_squareRootRemap_gain;
326 
327  // Variables used by offSetGainRemap
328  double m_logRemap_gain;
329  double m_logRemap_offset;
330 
331  // Variables used by DecorrelationEnhancementRemap
332  double m_decorrelationEnhancementRemap_gain;
333  double m_decorrelationEnhancementRemap_offset1;
334  double m_decorrelationEnhancementRemap_offset2;
335 
336  /*!
337  \brief Remap on gray level using an offset and gain.
338  \param inValue Input gray level.
339  \param outValue Output gray level.
340  \note outValue = ( ( inValue + m_offSetGainRemap_offset1 ) * m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2
341  */
342  inline void offSetGainRemap( const double& inValue, double& outValue )
343  {
344  outValue = ( ( inValue + m_offSetGainRemap_offset1 ) *
345  m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2;
346  };
347 
348  /*!
349  \brief Remap on gray level using a square.
350  \param inValue Input gray level.
351  \param outValue Output gray level.
352  \note outValue = m_squareRemap_factor * std::pow( inValue, 2.0 )
353  */
354  inline void squareRemap( const double& inValue, double& outValue )
355  {
356  outValue = m_squareRemap_gain * std::pow( inValue, 2.0 );
357  };
358 
359  /*!
360  \brief Remap on gray level using a square root.
361  \param inValue Input gray level.
362  \param outValue Output gray level.
363  \note outValue = m_squareRootRemap_gain * std::sqrt( inValue );
364  */
365  inline void squareRootRemap( const double& inValue, double& outValue )
366  {
367  outValue = m_squareRootRemap_gain * std::sqrt( inValue );
368  };
369 
370  /*!
371  \brief Remap on gray level using a log.
372  \param inValue Input gray level.
373  \param outValue Output gray level.
374  \note outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
375  */
376  inline void logRemap( const double& inValue, double& outValue )
377  {
378  outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
379  };
380 
381  /*!
382  \brief Remap on gray level using two offsets and gain.
383  \param inValue Input gray level.
384  \param outValue Output gray level.
385  */
386  inline void DecorrelationEnhancementRemap( const double& inValue, double& outValue )
387  {
388  outValue = ( ( inValue + m_decorrelationEnhancementRemap_offset1 ) *
389  m_decorrelationEnhancementRemap_gain )
390  + m_decorrelationEnhancementRemap_offset2;
391  };
392  };
393 
394  } // end namespace rp
395 } // end namespace te
396 
397 #endif
398 
std::vector< double > m_logCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:150
std::vector< double > m_lCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:106
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:192
double m_outRangeMin
Minimum value of data type range (default: -1.0 * std::numeric_limits::max() )...
Definition: Contrast.h:93
ContrastType m_type
The contrast type to be applied.
Definition: Contrast.h:87
Base exception class for plugin module.
Definition: Exception.h:42
void squareRemap(const double &inValue, double &outValue)
Remap on gray level using a square.
Definition: Contrast.h:354
std::vector< double > m_squareRootCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:137
void logRemap(const double &inValue, double &outValue)
Remap on gray level using a log.
Definition: Contrast.h:376
Raster Processing algorithm output parameters base interface.
std::vector< double > m_squareRootCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:139
Raster Processing algorithm base interface class.
std::vector< double > m_logCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:148
void squareRootRemap(const double &inValue, double &outValue)
Remap on gray level using a square root.
Definition: Contrast.h:365
Raster Processing algorithm base interface.
Definition: Algorithm.h:41
Contrast input parameters.
Definition: Contrast.h:65
An abstract class for raster data strucutures.
Definition: Raster.h:71
std::vector< unsigned int > m_outRasterBands
Bands to be processed from the output raster.
Definition: Contrast.h:196
double m_outRangeMax
Maximum value of data type range (default: std::numeric_limits::max().
Definition: Contrast.h:95
URI C++ Library.
void DecorrelationEnhancementRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:386
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
std::vector< unsigned int > m_inRasterBands
Bands to be processed from the input raster.
Definition: Contrast.h:91
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Contrast.h:97
std::vector< double > m_hECMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:117
std::vector< double > m_sMASCStdInput
The standard deviation to be applied in each band.
Definition: Contrast.h:161
std::auto_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:194
std::vector< double > m_squareCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:128
std::vector< double > m_lCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:108
std::vector< double > m_sMASCMeanInput
The mean greyscale to be applied in each band.
Definition: Contrast.h:159
Raster Processing algorithm input parameters base interface.
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Definition: Contrast.h:198
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Contrast.h:89
Contrast output parameters.
Definition: Contrast.h:188
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.
Definition: Contrast.h:200
Contrast enhancement.
Definition: Contrast.h:57
std::vector< double > m_squareCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:126