27 #include "../raster/Raster.h" 28 #include "../raster/Band.h" 29 #include "../raster/BandIterator.h" 30 #include "../raster/BandProperty.h" 31 #include "../raster/Grid.h" 32 #include "../raster/RasterProperty.h" 33 #include "../raster/RasterSummaryManager.h" 34 #include "../raster/RasterFactory.h" 35 #include "../memory/ExpansibleRaster.h" 36 #include "../common/progress/TaskProgress.h" 45 #include <boost/concept_check.hpp> 147 m_outRasterPtr =
nullptr;
148 m_createdOutRasterPtr.reset();
149 m_outRasterBands.clear();
150 m_createdOutRasterDSType.clear();
151 m_createdOutRasterInfo.clear();
180 throw( te::rp::Exception )
194 std::vector< te::rst::BandProperty* > bandsProperties;
195 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
218 "Output raster creation error" );
231 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
237 "Invalid output raster band" )
303 throw( te::rp::Exception )
312 "Invalid raster pointer" );
318 "Invalid bands number" );
320 if (inputParamsPtr->
m_outRangeMin != ( -1.0 * std::numeric_limits<double>::max() ) &&
321 inputParamsPtr->
m_outRangeMax != std::numeric_limits<double>::max())
327 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
333 "Invalid input raster band" );
338 switch( inputParamsPtr->
m_type )
344 "Invalid parameter m_lCMinInput" );
347 "Invalid parameter m_lCMaxInput" );
355 "Invalid parameter m_hECMaxInput" );
363 "Invalid parameter m_squareCMinInput" );
366 "Invalid parameter m_squareCMaxInput" );
374 "Invalid parameter m_squareRootCMinInput" );
377 "Invalid parameter m_squareRootRCMaxInput" );
385 "Invalid parameter m_logCMinInput" );
388 "Invalid parameter m_logCMaxInput" );
396 "Invalid parameter m_sMASCMeanInput" );
399 "Invalid parameter m_sMASCStdInput" );
425 const double& inRangeMin,
const double& inRangeMax,
426 const double& outRangeMin,
const double& outRangeMax,
427 double& gain,
double& offset1,
double& offset2 )
429 const double outRange = outRangeMax - outRangeMin;
430 const double inRange = inRangeMax - inRangeMin;
444 gain = outRange / inRange;
447 offset1 = (-1.0) * inRangeMin;
448 offset2 = outRangeMin;
460 gain = outRange / std::pow( inRange, 2.0 );
476 gain = outRange / std::sqrt( inRange );
492 gain = outRange / std::log10( inRange + 1.0 );
495 offset1 = -1.0 * inRangeMin;
517 std::unique_ptr< te::common::TaskProgress > progressPtr;
518 if( enableGlobalProgress )
521 progressPtr->setMessage(
"Contrast" );
525 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
556 (!enableGlobalProgress) ) )
561 if( enableGlobalProgress )
563 progressPtr->pulse();
564 if( ! progressPtr->isActive() )
return false;
586 std::unique_ptr< te::common::TaskProgress > progressPtr;
587 if( enableGlobalProgress )
590 progressPtr->setMessage(
"Contrast" );
615 const double minp = rasterSummaryPtr->at(inputBandIdx).m_minVal->real();
616 const double maxp = rasterSummaryPtr->at(inputBandIdx).m_maxVal->real();
621 const double pixelValueStep = isRealValuesBand ? ( ( maxp - minp ) /
629 if( isRealValuesBand )
631 for (
double pixel = minp; pixel <= maxp; pixel += pixelValueStep )
635 for (
double nj = minp; nj <= pixel; nj++)
637 newvalue += rasterSummaryPtr->at(inputBandIdx).m_histogramR->lower_bound( nj )->second;
645 for (
double pixel = minp; pixel <= maxp; pixel += pixelValueStep )
649 for (
double nj = minp; nj <= pixel; nj++)
651 newvalue += rasterSummaryPtr->at(inputBandIdx).m_histogramR->operator[](nj);
661 / ((double)totalPixelsNumber );
665 (!enableGlobalProgress) ) )
670 if( enableGlobalProgress )
672 progressPtr->pulse();
673 if( ! progressPtr->isActive() )
return false;
687 std::unique_ptr< te::common::TaskProgress > progressPtr;
688 if( enableGlobalProgress )
691 progressPtr->setMessage(
"Contrast" );
695 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
725 (!enableGlobalProgress) ) )
730 if( enableGlobalProgress )
732 progressPtr->pulse();
733 if( ! progressPtr->isActive() )
return false;
747 std::unique_ptr< te::common::TaskProgress > progressPtr;
748 if( enableGlobalProgress )
751 progressPtr->setMessage(
"Contrast" );
755 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
785 (!enableGlobalProgress) ) )
790 if( enableGlobalProgress )
792 progressPtr->pulse();
793 if( ! progressPtr->isActive() )
return false;
807 std::unique_ptr< te::common::TaskProgress > progressPtr;
808 if( enableGlobalProgress )
811 progressPtr->setMessage(
"Contrast" );
815 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
845 (!enableGlobalProgress) ) )
850 if( enableGlobalProgress )
852 progressPtr->pulse();
853 if( ! progressPtr->isActive() )
return false;
870 std::unique_ptr< te::common::TaskProgress > progressPtr;
874 progressPtr->setMessage(
"Contrast" );
899 const double meanp = rsummary->at(niband).m_meanVal->real();
900 const double stdp = rsummary->at(niband).m_stdVal->real();
914 newvalue = (value - meanp) * c1 + c2;
922 progressPtr->pulse();
923 if( ! progressPtr->isActive() )
return false;
938 std::unique_ptr< te::common::TaskProgress > progressPtr;
942 progressPtr->setMessage(
"Contrast" );
949 std::unique_ptr< te::rst::Raster > pcaRasterPtr;
950 boost::numeric::ublas::matrix< double > pcaMatrix;
955 std::vector< te::rst::BandProperty * > bandProperties;
956 std::vector< unsigned int > pcaRasterBands;
958 for(
unsigned int bandIdx = 0 ; bandIdx <
965 pcaRasterBands.push_back( bandIdx );
980 pcaMatrix, *pcaRasterPtr, pcaRasterBands, 0 ),
981 "Principal components generation error" );
988 progressPtr->pulse();
989 if( ! progressPtr->isActive() )
return false;
994 double pca0MeanValue = 0;
996 *pcaRasterPtr->getBand( 0 ), 0, pca0MeanValue ),
997 "Mean value calcule error" );
999 double pca0StdDevValue = 0;
1001 *pcaRasterPtr->getBand( 0 ), 0, &pca0MeanValue,
1002 pca0StdDevValue ),
"StdDev value calcule error" );
1006 for(
unsigned int pcaBandIdx = 1 ; pcaBandIdx <
1009 te::rst::Band& pcaBand = *pcaRasterPtr->getBand( pcaBandIdx );
1011 double meanValue = 0;
1013 "Mean value calcule error" );
1015 double stdDevValue = 0;
1017 stdDevValue ),
"StdDev value calcule error" );
1032 progressPtr->pulse();
1033 if( ! progressPtr->isActive() )
return false;
1042 "Inverse PCA error" );
1046 progressPtr->pulse();
1047 if( ! progressPtr->isActive() )
return false;
1055 const bool enableProgress )
1069 std::unique_ptr< te::common::TaskProgress > progressPtr;
1070 if( enableProgress )
1073 progressPtr->setMessage(
"Contrast" );
1076 if( ( inBlkWidthPixels == outBlkWidthPixels ) &&
1077 ( inBlkHeightPixels == outBlkHeightPixels ) )
1079 const unsigned int blockSizePixels = inBlkWidthPixels *
1083 unsigned char* inputBuffer =
new unsigned char[ inRasterBand.
getBlockSize() ];
1084 unsigned char* outputBuffer =
new unsigned char[ outRasterBand.
getBlockSize() ];
1085 double* inDoublesBuffer =
new double[ blockSizePixels ];
1086 double* outDoublesBuffer =
new double[ blockSizePixels ];
1089 unsigned int blockXIndex = 0;
1090 unsigned int blockOffset = 0;
1091 double outValue = 0;
1093 if( enableProgress ) progressPtr->setTotalSteps( yBlocksNmb * xBlocksNmb );
1095 for(
unsigned int blockYIndex = 0 ; blockYIndex < yBlocksNmb ;
1098 for( blockXIndex = 0 ; blockXIndex < xBlocksNmb ;
1101 inRasterBand.
read( blockXIndex, blockYIndex, inputBuffer );
1104 blockSizePixels, inDoublesBuffer );
1106 for( blockOffset = 0 ; blockOffset < blockSizePixels ; ++blockOffset )
1108 (this->*remapFuncPtr)( inDoublesBuffer[blockOffset],
1115 outputVectorDataType, outputBuffer );
1117 outRasterBand.
write( blockXIndex, blockYIndex, outputBuffer );
1119 if( enableProgress )
1121 progressPtr->pulse();
1122 if( ! progressPtr->isActive() )
return false;
1127 delete[] inputBuffer;
1128 delete[] outputBuffer;
1129 delete[] inDoublesBuffer;
1130 delete[] outDoublesBuffer;
1137 if( enableProgress ) progressPtr->setTotalSteps( linesNumber );
1139 unsigned int col = 0;
1140 double inputValue = 0;
1141 double outputValue = 0;
1143 for(
unsigned int line = 0 ;
line < linesNumber ; ++
line )
1145 for( col = 0 ; col < columnsNumber ; ++
col )
1148 (this->*remapFuncPtr)( inputValue, outputValue );
1153 if( enableProgress )
1155 progressPtr->pulse();
1156 if( ! progressPtr->isActive() )
return false;
double m_decorrelationEnhancementRemap_offset2
bool GetMeanValue(const te::rst::Band &band, const unsigned int maxThreads, double &meanValue)
Get the mean of band pixel values.
bool m_isInitialized
Tells if this instance is initialized.
te::rst::Raster * m_outRasterPtr
A pointer to a valid initiated raster instance where the result must be written, leave NULL to create...
double m_squareRemap_gain
A raster band description.
Base exception class for plugin module.
void squareRemap(const double &inValue, double &outValue)
Remap on gray level using a square.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
int m_nblocksx
The number of blocks in x.
virtual const char * what() const
It outputs the exception message.
int m_nblocksy
The number of blocks in y.
void logRemap(const double &inValue, double &outValue)
Remap on gray level using a log.
void TERPEXPORT GetDataTypeRange(const int dataType, double &min, double &max)
Returns the real data type range (all values that can be represented by the given data type)...
This class can be used to inform the progress of a task.
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).
Raster Processing algorithm output parameters base interface.
const OutputParameters & operator=(const OutputParameters ¶ms)
double m_histogramEqualizationRemap_gain
AbstractParameters * clone() const
Create a clone copy of this instance.
double m_histogramEqualizationRemap_cDFMin
double m_decorrelationEnhancementRemap_gain
bool DirectPrincipalComponents(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &pcaRaster, const std::vector< unsigned int > &pcaRasterBands, const unsigned int maxThreads)
Generate all principal components from the given input raster.
This class implements an iterator to "navigate" over a single band (const).
double m_decorrelationEnhancementRemap_offset1
static ConstBandIterator begin(const Band *b)
Returns an iterator referring to the first value of the band.
bool execDecorrelationEnhancement()
Execute the decorrelation enhancement contrast following the internal parameters. ...
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
void squareRootRemap(const double &inValue, double &outValue)
Remap on gray level using a square root.
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
bool execSquareContrast()
Execute a square contrast following the internal parameters.
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
std::vector< unsigned int > m_outRasterBands
Bands to be processed from the output raster.
Calculate the standard deviation value.
BandProperty * getProperty()
Returns the band property.
SummaryTypes
Types for the BandSummary.
int m_blkw
Block width (pixels).
std::map< double, double > m_histogramEqualizationRemap_cdf
void DecorrelationEnhancementRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Contrast::InputParameters m_inputParameters
Contrast input execution parameters.
virtual void write(int x, int y, void *buffer)=0
It writes a data block from the specified buffer.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
void Convert2DoublesVector(void *inputVector, const int inputVectorDataType, unsigned int inputVectorSize, double *outputVector)
Convert vector elements.
bool execSquareRootContrast()
Execute a square root contrast following the internal parameters.
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
double m_offSetGainRemap_offset1
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
Abstract parameters base interface.
virtual void read(int x, int y, void *buffer) const =0
It reads a data block to the specified buffer.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
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...
bool execHistogramEqualizationContrast()
Execute the histogram equalization contrast following the internal parameters.
double m_squareRootRemap_gain
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
void(Contrast::* RemapFuncPtrT)(const double &inValue, double &outValue)
Type definition for a remapping function pointer.
bool execLogContrast()
Execute a log contrast following the internal parameters.
A raster (stored in memory and eventually swapped to disk) where it is possible to dynamically add li...
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
double m_offSetGainRemap_gain
static Raster * make()
It creates and returns an empty raster with default raster driver.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
bool InversePrincipalComponents(const te::rst::Raster &pcaRaster, const boost::numeric::ublas::matrix< double > &pcaMatrix, te::rst::Raster &outputRaster, const std::vector< unsigned int > &outputRasterBands, const unsigned int maxThreads)
Regenerate the original raster from its principal components.
virtual Raster * getRaster() const =0
Returns the associated raster.
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
Raster Processing functions.
int getType() const
It returns the data type of the elements in the band.
int m_blkh
Block height (pixels).
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
void offSetGainRemap(const double &inValue, double &outValue)
Remap on gray level using an offset and gain.
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
void histogramEqualizationRemap(const double &inValue, double &outValue)
Remap on gray level using two offsets and gain.
Contrast::OutputParameters * m_outputParametersPtr
Contrast input execution parameters.
A rectified grid is the spatial support for raster data.
Contrast output parameters.
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.
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
Calculate the mean value.
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
double m_offSetGainRemap_offset2
bool GetStdDevValue(const te::rst::Band &band, const unsigned int maxThreads, double const *const meanValuePtr, double &stdDevValue)
Get the standard deviation of band pixel values.
Calculate the histogram for the real part.
bool execSetMeanAndStdContrast()
Execute the histogram equalization contrast following the internal parameters.
bool execLinearContrast()
Execute a linear contrast following the internal parameters.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
void ConvertDoublesVector(double *inputVector, unsigned int inputVectorSize, const int outputVectorDataType, void *outputVector)
Convert a doubles vector.
bool remapBandLevels(const te::rst::Band &inRasterBand, te::rst::Band &outRasterBand, RemapFuncPtrT remapFuncPtr, const bool enableProgress)
Band gray levels remap using a remap function.
#define TERP_INSTANCE_LOG_AND_RETURN_FALSE(message)
Logs a warning message, update the current instance error messsage and return false.
static ConstBandIterator end(const Band *b)
Returns an iterator referring to after the end of the iterator.