Loading...
Searching...
No Matches
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
36namespace 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 */
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 PercentCutUDLinearT /*!< Linear cutted in user defined minimum and maximum percentage values. */
87 };
88
89 ContrastType m_type; //!< The contrast type to be applied.
90
91 te::rst::Raster const* m_inRasterPtr; //!< Input raster.
92
93 std::vector< unsigned int > m_inRasterBands; //!< Bands to be processed from the input raster.
94
95 double m_outRangeMin; //!< Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
96
97 double m_outRangeMax; //!< Maximum value of data type range (default: std::numeric_limits<double>::max().
98
99 bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
100
101 bool m_sampled; //!< Enable/Disable uses all values to calculate histogram
102
103 bool m_cCutUDCSelecWideRange; ///!< Select the wide range of values to fit all bands.
104
105 //@}
106
107 /*!
108 \name Linear contrast parameters
109 */
110 /**@{*/
111
112 std::vector< double > m_lCMinInput; //!< The contrast minimum input greyscale value of each band.
113
114 std::vector< double > m_lCMaxInput; //!< The contrast maximum input greyscale value of each band.
115
116 //@}
117
118 /*!
119 \name Histogram equalization contrast parameters
120 */
121 /**@{*/
122
123 std::vector< double > m_hECMaxInput; //!< The contrast maximum input greyscale value of each band.
124
125 //@}
126
127 /*!
128 \name Square contrast parameters
129 */
130 /**@{*/
131
132 std::vector< double > m_squareCMinInput; //!< The contrast minimum input greyscale value of each band.
133
134 std::vector< double > m_squareCMaxInput; //!< The contrast maximum input greyscale value of each band.
135
136 //@}
137
138 /*!
139 \name Square root contrast parameters
140 */
141 /**@{*/
142
143 std::vector< double > m_squareRootCMinInput; //!< The contrast minimum input greyscale value of each band.
144
145 std::vector< double > m_squareRootCMaxInput; //!< The contrast maximum input greyscale value of each band.
146
147 //@}
148
149 /*!
150 \name Log contrast parameters
151 */
152 /**@{*/
153
154 std::vector< double > m_logCMinInput; //!< The contrast minimum input greyscale value of each band.
155
156 std::vector< double > m_logCMaxInput; //!< The contrast maximum input greyscale value of each band.
157
158 //@}
159
160 /*!
161 \name Mean and standard deviation normalization contrast parameters
162 */
163 /**@{*/
164
165 std::vector< double > m_sMASCMeanInput; //!< The mean greyscale to be applied in each band.
166
167 std::vector< double > m_sMASCStdInput; //!< The standard deviation to be applied in each band.
168
169 //@}
170
171 /*!
172 \name User defined Cumulative Cut EnhancementT contrast parameters
173 */
174 /**@{*/
175
176 std::vector< double > m_cCutUDCMinInput; //!< The contrast minimum percentage value of each band (0-100).
177
178 std::vector< double > m_cCutUDCMaxInput; //!< The contrast maximum percentage value of each band (0-100).
179
180 //@}
181
183
185
186 //overload
187 void reset() ;
188
189 //overload
191
192 //overload
193 AbstractParameters* clone() const;
194
195 //overload
196 bool serialize ( AlgorithmParametersSerializer& serializer ) const;
197 };
198
199 /*!
200 \class OutputParameters
201 \brief Contrast output parameters
202 \details The result will be written to the raster instance pointed
203 by m_outRasterPtr (in this case the output bands must also be
204 passed by m_outRasterBands ); or written to a new raster instance
205 created inside the given data source pointed by m_outDataSourcePtr
206 (in this case the data set name must be supplied - m_outDataSetName ).
207 */
209 {
210 public:
211
212 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).
213
214 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.
215
216 std::vector< unsigned int > m_outRasterBands; //!< Bands to be processed from the output raster.
217
218 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.
219
220 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.
221
223
225
227
228 //overload
229 void reset() ;
230
231 //overload
233
234 //overload
235 AbstractParameters* clone() const;
236 };
237
239
241
242 //overload
243 bool execute( AlgorithmOutputParameters& outputParams ) ;
244
245 //overload
246 void reset() ;
247
248 //overload
249 bool initialize( const AlgorithmInputParameters& inputParams ) ;
250
251 bool isInitialized() const;
252
253 /*!
254 \brief Returns gain and offset values for contrast types (when applicable).
255 \param inRangeMin Input values range minimum value.
256 \param inRangeMax Input values range maximum value.
257 \param outRangeMin Output values range minimum value.
258 \param outRangeMax Output values range maximum value.
259 \param gain Calculated gain.
260 \param offset1 Calculated offset 1.
261 \param offset2 Calculated offset 2.
262 \return true if OK, false on errors.
263 */
265 const double& inRangeMin, const double& inRangeMax,
266 const double& outRangeMin, const double& outRangeMax,
267 double& gain, double& offset1, double& offset2 );
268
269 protected:
270
271 /*!
272 \brief Type definition for a remapping function pointer.
273 */
274 typedef void (Contrast::*RemapFuncPtrT)( const double& inValue,
275 double& outValue );
276
277 Contrast::InputParameters m_inputParameters; //!< Contrast input execution parameters.
278 Contrast::OutputParameters* m_outputParametersPtr; //!< Contrast input execution parameters.
279
280 bool m_isInitialized; //!< Tells if this instance is initialized.
281
282 /*!
283 \brief Execute a linear contrast following the internal parameters
284 \return true if OK, false on errors.
285 */
287
288 /*!
289 \brief Execute the histogram equalization contrast following the internal parameters
290 \return true if OK, false on errors.
291 */
293
294 /*!
295 \brief Execute a square contrast following the internal parameters
296 \return true if OK, false on errors.
297 */
299
300 /*!
301 \brief Execute a square root contrast following the internal parameters
302 \return true if OK, false on errors.
303 */
305
306 /*!
307 \brief Execute a log contrast following the internal parameters
308 \return true if OK, false on errors.
309 */
311
312 /*!
313 \brief Execute the histogram equalization contrast following the internal parameters
314 \return true if OK, false on errors.
315 */
317
318 /*!
319 \brief Execute the decorrelation enhancement contrast following the internal parameters
320 \return true if OK, false on errors.
321 */
323
324 /*!
325 \brief Execute a cumulative linear contrast cutted by 2-98% following the internal parameters
326 \return true if OK, false on errors.
327 */
329
330
331 /*!
332 \brief Execute a linear contrast cutted by user defined percentual values.
333 \return true if OK, false on errors.
334 */
336
337 /*!
338 \brief Band gray levels remap using a remap function.
339 \param inRaster Input Raster
340 \param inRasterBandIdx Input raster band index.
341 \param outRaster Output raster.
342 \param outRasterBandIdx Output raster band index.
343 \param remapFuncPtr The remap function pointer used.
344 \param enableProgress Enable the use of a progress interface.
345 \return true if OK, false on errors.
346 */
347 bool remapBandLevels( const te::rst::Raster& inRaster,
348 const unsigned int inRasterBandIdx,
349 te::rst::Raster& outRaster,
350 const unsigned int outRasterBandIdx,
351 RemapFuncPtrT remapFuncPtr,
352 const bool enableProgress );
353
354 // Variables used by offSetGainRemap
358
359 // Variables used by offSetGainRemap
361
362 // Variables used by offSetGainRemap
364
365 // Variables used by offSetGainRemap
368
369 // Variables used by DecorrelationEnhancementRemap
373
374 // Variables used by HistogramEqualizationRemap
377 std::map<double, double> m_histogramEqualizationRemap_cdf;
378
379 /*!
380 \brief Remap on gray level using an offset and gain.
381 \param inValue Input gray level.
382 \param outValue Output gray level.
383 \note outValue = ( ( inValue + m_offSetGainRemap_offset1 ) * m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2
384 */
385 inline void offSetGainRemap( const double& inValue, double& outValue )
386 {
387 outValue = ( ( inValue + m_offSetGainRemap_offset1 ) *
388 m_offSetGainRemap_gain ) + m_offSetGainRemap_offset2;
389 };
390
391 /*!
392 \brief Remap on gray level using a square.
393 \param inValue Input gray level.
394 \param outValue Output gray level.
395 \note outValue = m_squareRemap_factor * std::pow( inValue, 2.0 )
396 */
397 inline void squareRemap( const double& inValue, double& outValue )
398 {
399 outValue = m_squareRemap_gain * std::pow( inValue, 2.0 );
400 };
401
402 /*!
403 \brief Remap on gray level using a square root.
404 \param inValue Input gray level.
405 \param outValue Output gray level.
406 \note outValue = m_squareRootRemap_gain * std::sqrt( inValue );
407 */
408 inline void squareRootRemap( const double& inValue, double& outValue )
409 {
410 outValue = m_squareRootRemap_gain * std::sqrt( inValue );
411 };
412
413 /*!
414 \brief Remap on gray level using a log.
415 \param inValue Input gray level.
416 \param outValue Output gray level.
417 \note outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
418 */
419 inline void logRemap( const double& inValue, double& outValue )
420 {
421 outValue = m_logRemap_gain * std::log10( inValue + m_logRemap_offset + 1.0 );
422 };
423
424 /*!
425 \brief Remap on gray level using two offsets and gain.
426 \param inValue Input gray level.
427 \param outValue Output gray level.
428 */
429 inline void DecorrelationEnhancementRemap( const double& inValue, double& outValue )
430 {
431 outValue = ( ( inValue + m_decorrelationEnhancementRemap_offset1 ) *
432 m_decorrelationEnhancementRemap_gain )
433 + m_decorrelationEnhancementRemap_offset2;
434 };
435
436 /*!
437 \brief Remap on gray level using two offsets and gain.
438 \param inValue Input gray level.
439 \param outValue Output gray level.
440 */
441 inline void histogramEqualizationRemap( const double& inValue, double& outValue )
442 {
443 outValue = ( ( m_histogramEqualizationRemap_cdf.lower_bound( inValue )->second ) - m_histogramEqualizationRemap_cDFMin ) *
444 m_histogramEqualizationRemap_gain;
445 };
446 };
447
448 } // end namespace rp
449} // end namespace te
450
451#endif
452
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:143
std::vector< double > m_squareCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:132
std::vector< double > m_lCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:112
bool m_sampled
Enable/Disable uses all values to calculate histogram.
Definition: Contrast.h:101
std::vector< unsigned int > m_inRasterBands
Bands to be processed from the input raster.
Definition: Contrast.h:93
std::vector< double > m_logCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:156
std::vector< double > m_sMASCStdInput
The standard deviation to be applied in each band.
Definition: Contrast.h:167
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: Contrast.h:99
ContrastType m_type
The contrast type to be applied.
Definition: Contrast.h:89
double m_outRangeMin
Minimum value of data type range (default: -1.0 * std::numeric_limits<double>::max() ).
Definition: Contrast.h:95
std::vector< double > m_squareCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:134
std::vector< double > m_lCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:114
std::vector< double > m_logCMinInput
The contrast minimum input greyscale value of each band.
Definition: Contrast.h:154
double m_outRangeMax
Maximum value of data type range (default: std::numeric_limits<double>::max().
Definition: Contrast.h:97
std::vector< double > m_cCutUDCMinInput
The contrast minimum percentage value of each band (0-100).
Definition: Contrast.h:176
const InputParameters & operator=(const InputParameters &params)
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:165
std::vector< double > m_hECMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:123
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: Contrast.h:91
std::vector< double > m_squareRootCMaxInput
The contrast maximum input greyscale value of each band.
Definition: Contrast.h:145
std::vector< double > m_cCutUDCMaxInput
The contrast maximum percentage value of each band (0-100).
Definition: Contrast.h:178
AbstractParameters * clone() const
Create a clone copy of this instance.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state.
Contrast output parameters.
Definition: Contrast.h:209
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Definition: Contrast.h:218
const OutputParameters & operator=(const OutputParameters &params)
std::vector< unsigned int > m_outRasterBands
Bands to be processed from the output raster.
Definition: Contrast.h:216
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:212
std::map< std::string, std::string > m_createdOutRasterInfo
The necessary information to create the raster (as described in te::raster::RasterFactory),...
Definition: Contrast.h:220
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:214
OutputParameters(const OutputParameters &)
AbstractParameters * clone() const
Create a clone copy of this instance.
Contrast enhancement.
Definition: Contrast.h:58
Contrast::InputParameters m_inputParameters
Contrast input execution parameters.
Definition: Contrast.h:277
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:278
bool execPercentCutUDLinear()
Execute a linear contrast cutted by user defined percentual values.
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:429
void histogramEqualizationRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Definition: Contrast.h:441
void squareRemap(const double &inValue, double &outValue)
Remap on gray level using a square.
Definition: Contrast.h:397
double m_histogramEqualizationRemap_cDFMin
Definition: Contrast.h:375
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:357
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:377
double m_logRemap_offset
Definition: Contrast.h:367
double m_decorrelationEnhancementRemap_offset1
Definition: Contrast.h:371
bool m_isInitialized
Tells if this instance is initialized.
Definition: Contrast.h:280
double m_offSetGainRemap_offset1
Definition: Contrast.h:355
double m_logRemap_gain
Definition: Contrast.h:366
double m_offSetGainRemap_offset2
Definition: Contrast.h:356
double m_decorrelationEnhancementRemap_gain
Definition: Contrast.h:370
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
double m_histogramEqualizationRemap_gain
Definition: Contrast.h:376
double m_decorrelationEnhancementRemap_offset2
Definition: Contrast.h:372
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:408
double m_squareRootRemap_gain
Definition: Contrast.h:363
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:385
double m_squareRemap_gain
Definition: Contrast.h:360
void logRemap(const double &inValue, double &outValue)
Remap on gray level using a log.
Definition: Contrast.h:419
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.