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>
68 m_hECMaxInput.clear();
70 m_squareCMinInput.clear();
71 m_squareCMaxInput.clear();
73 m_squareCMinInput.clear();
74 m_squareCMaxInput.clear();
76 m_squareRootCMinInput.clear();
77 m_squareRootCMaxInput.clear();
79 m_logCMinInput.clear();
80 m_logCMaxInput.clear();
82 m_sMASCMeanInput.clear();
83 m_sMASCStdInput.clear();
86 m_inRasterBands.clear();
87 m_enableProgress =
false;
145 m_createdOutRasterPtr.reset();
146 m_outRasterBands.clear();
147 m_createdOutRasterDSType.clear();
148 m_createdOutRasterInfo.clear();
179 throw( te::rp::Exception )
193 std::vector< te::rst::BandProperty* > bandsProperties;
194 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
217 "Output raster creation error" );
230 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
236 "Invalid output raster band" )
300 throw( te::rp::Exception )
309 "Invalid raster pointer" );
315 "Invalid bands number" );
317 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
323 "Invalid input raster band" );
328 switch( inputParamsPtr->
m_type )
334 "Invalid parameter m_lCMinInput" );
337 "Invalid parameter m_lCMaxInput" );
345 "Invalid parameter m_hECMaxInput" );
353 "Invalid parameter m_squareCMinInput" );
356 "Invalid parameter m_squareCMaxInput" );
364 "Invalid parameter m_squareRootCMinInput" );
367 "Invalid parameter m_squareRootRCMaxInput" );
375 "Invalid parameter m_logCMinInput" );
378 "Invalid parameter m_logCMaxInput" );
386 "Invalid parameter m_sMASCMeanInput" );
389 "Invalid parameter m_sMASCStdInput" );
421 std::auto_ptr< te::common::TaskProgress > progressPtr;
422 if( enableGlobalProgress )
425 progressPtr->setMessage(
"Contrast" );
429 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
437 double outRangeMin = 0.0;
438 double outRangeMax = 0.0;
440 outRangeMin, outRangeMax );
441 const double outRasterRange = outRangeMax - outRangeMin;
446 if( inRasterRange != 0.0 )
461 (!enableGlobalProgress) ) )
466 if( enableGlobalProgress )
468 progressPtr->pulse();
469 if( ! progressPtr->isActive() )
return false;
490 std::auto_ptr< te::common::TaskProgress > progressPtr;
494 progressPtr->setMessage(
"Contrast" );
500 const double lutfactor =
509 double outRangeMin = 0.0;
510 double outRangeMax = 0.0;
512 outRangeMin, outRangeMax );
519 std::map<double, double> lut;
520 const double minp = rsummary->at(niband).m_minVal->real();
521 const double maxp = rsummary->at(niband).m_maxVal->real();
522 for (
double pixel = minp; pixel <= maxp; pixel++)
525 for (
double nj = minp; nj <= pixel; nj++)
526 newvalue += rsummary->at(niband).m_histogramR->operator[](nj);
527 lut[pixel] = lutfactor * newvalue;
530 while (ibandit != ibanditend)
532 value = lut[ *ibandit ];
533 value = std::max( outRangeMin, value );
534 value = std::min( outRangeMax, value );
535 oband->
setValue(ibandit.getColumn(), ibandit.getRow(), value);
541 progressPtr->pulse();
542 if( ! progressPtr->isActive() )
return false;
556 std::auto_ptr< te::common::TaskProgress > progressPtr;
557 if( enableGlobalProgress )
560 progressPtr->setMessage(
"Contrast" );
564 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
572 double outRangeMin = 0.0;
573 double outRangeMax = 0.0;
575 outRangeMin, outRangeMax );
576 const double outRasterRange = outRangeMax - outRangeMin;
581 if( inRasterRange != 0.0 )
592 (!enableGlobalProgress) ) )
597 if( enableGlobalProgress )
599 progressPtr->pulse();
600 if( ! progressPtr->isActive() )
return false;
614 std::auto_ptr< te::common::TaskProgress > progressPtr;
615 if( enableGlobalProgress )
618 progressPtr->setMessage(
"Contrast" );
622 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
630 double outRangeMin = 0.0;
631 double outRangeMax = 0.0;
633 outRangeMin, outRangeMax );
634 const double outRasterRange = outRangeMax - outRangeMin;
639 if( inRasterRange != 0.0 )
650 (!enableGlobalProgress) ) )
655 if( enableGlobalProgress )
657 progressPtr->pulse();
658 if( ! progressPtr->isActive() )
return false;
672 std::auto_ptr< te::common::TaskProgress > progressPtr;
673 if( enableGlobalProgress )
676 progressPtr->setMessage(
"Contrast" );
680 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
688 double outRangeMin = 0.0;
689 double outRangeMax = 0.0;
691 outRangeMin, outRangeMax );
692 const double outRasterRange = outRangeMax - outRangeMin;
699 if( inRasterRange != 0.0 )
710 (!enableGlobalProgress) ) )
715 if( enableGlobalProgress )
717 progressPtr->pulse();
718 if( ! progressPtr->isActive() )
return false;
735 std::auto_ptr< te::common::TaskProgress > progressPtr;
739 progressPtr->setMessage(
"Contrast" );
752 double outRangeMin = 0.0;
753 double outRangeMax = 0.0;
755 outRangeMin, outRangeMax );
762 const double meanp = rsummary->at(niband).m_meanVal->real();
763 const double stdp = rsummary->at(niband).m_stdVal->real();
777 newvalue = (value - meanp) * c1 + c2;
778 newvalue = std::max( outRangeMin, newvalue );
779 newvalue = std::min( outRangeMax, newvalue );
785 progressPtr->pulse();
786 if( ! progressPtr->isActive() )
return false;
801 std::auto_ptr< te::common::TaskProgress > progressPtr;
805 progressPtr->setMessage(
"Contrast" );
812 std::auto_ptr< te::rst::Raster > pcaRasterPtr;
813 boost::numeric::ublas::matrix< double > pcaMatrix;
818 std::vector< te::rst::BandProperty * > bandProperties;
819 std::vector< unsigned int > pcaRasterBands;
821 for(
unsigned int bandIdx = 0 ; bandIdx <
828 pcaRasterBands.push_back( bandIdx );
843 pcaMatrix, *pcaRasterPtr, pcaRasterBands, 0 ),
844 "Principal components generation error" );
851 progressPtr->pulse();
852 if( ! progressPtr->isActive() )
return false;
857 double pca0MeanValue = 0;
859 *pcaRasterPtr->getBand( 0 ), 0, pca0MeanValue ),
860 "Mean value calcule error" );
862 double pca0StdDevValue = 0;
864 *pcaRasterPtr->getBand( 0 ), 0, &pca0MeanValue,
865 pca0StdDevValue ),
"StdDev value calcule error" );
869 for(
unsigned int pcaBandIdx = 1 ; pcaBandIdx <
872 te::rst::Band& pcaBand = *pcaRasterPtr->getBand( pcaBandIdx );
874 double meanValue = 0;
876 "Mean value calcule error" );
878 double stdDevValue = 0;
880 stdDevValue ),
"StdDev value calcule error" );
895 progressPtr->pulse();
896 if( ! progressPtr->isActive() )
return false;
905 "Inverse PCA error" );
909 progressPtr->pulse();
910 if( ! progressPtr->isActive() )
return false;
918 const bool enableProgress )
924 double outRangeMin = 0.0;
925 double outRangeMax = 0.0;
927 outRangeMin, outRangeMax );
929 std::auto_ptr< te::common::TaskProgress > progressPtr;
933 progressPtr->setMessage(
"Contrast" );
936 if( ( inBlkWidthPixels == outBlkWidthPixels ) &&
937 ( inBlkHeightPixels == outBlkHeightPixels ) )
939 const unsigned int blockSizePixels = inBlkWidthPixels *
943 unsigned char* inputBuffer =
new unsigned char[ inRasterBand.
getBlockSize() ];
944 unsigned char* outputBuffer =
new unsigned char[ outRasterBand.
getBlockSize() ];
945 double* inDoublesBuffer =
new double[ blockSizePixels ];
946 double* outDoublesBuffer =
new double[ blockSizePixels ];
949 unsigned int blockXIndex = 0;
950 unsigned int blockOffset = 0;
953 if( enableProgress ) progressPtr->setTotalSteps( yBlocksNmb * xBlocksNmb );
955 for(
unsigned int blockYIndex = 0 ; blockYIndex < yBlocksNmb ;
958 for( blockXIndex = 0 ; blockXIndex < xBlocksNmb ;
961 inRasterBand.
read( blockXIndex, blockYIndex, inputBuffer );
964 blockSizePixels, inDoublesBuffer );
966 for( blockOffset = 0 ; blockOffset < blockSizePixels ; ++blockOffset )
968 (this->*remapFuncPtr)( inDoublesBuffer[blockOffset],
970 outDoublesBuffer[blockOffset] = std::max( outRangeMin, std::min(
971 outRangeMax, outValue ) );
975 outputVectorDataType, outputBuffer );
977 outRasterBand.
write( blockXIndex, blockYIndex, outputBuffer );
981 progressPtr->pulse();
982 if( ! progressPtr->isActive() )
return false;
987 delete[] inputBuffer;
988 delete[] outputBuffer;
989 delete[] inDoublesBuffer;
990 delete[] outDoublesBuffer;
997 if( enableProgress ) progressPtr->setTotalSteps( linesNumber );
999 unsigned int col = 0;
1000 double inputValue = 0;
1001 double outputValue = 0;
1003 for(
unsigned int line = 0 ; line < linesNumber ; ++line )
1005 for( col = 0 ; col < columnsNumber ; ++col )
1007 inRasterBand.
getValue( col, line, inputValue );
1008 (this->*remapFuncPtr)( inputValue, outputValue );
1009 outRasterBand.
setValue( col, line, std::max( outRangeMin, std::min(
1010 outRangeMax, outputValue ) ) );
1013 if( enableProgress )
1015 progressPtr->pulse();
1016 if( ! progressPtr->isActive() )
return false;
bool GetMeanValue(const te::rst::Band &band, const unsigned int maxThreads, double &meanValue)
Get the mean of band pixel values.
double m_squareRemap_factor
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...
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
A raster band description.
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 Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
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.
Raster Processing algorithm output parameters base interface.
const OutputParameters & operator=(const OutputParameters ¶ms)
virtual void read(int x, int y, void *buffer) const =0
It reads a data block to the specified buffer.
AbstractParameters * clone() const
Create a clone copy of this instance.
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).
static ConstBandIterator begin(const Band *b)
Returns an iterator referring to the first value of the band.
Raster Processing functions.
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...
const Algorithm & operator=(const Algorithm &)
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.
#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).
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
virtual Raster * getRaster() const =0
Returns the associated raster.
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.
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.
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...
Abstract parameters base interface.
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.
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.
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.
std::string m_createdOutRasterDSType
Output raster data source type (as described in te::raster::RasterFactory ), leave empty if the resul...
int getType() const
It returns the data type of the elements in the band.
void(Contrast::* RemapFuncPtrT)(const double &inValue, double &outValue)
Type definition for a remapping function pointer.
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.
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.
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.
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.
static ConstBandIterator end(const Band *b)
Returns an iterator referring to after the end of the iterator.