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