30 #include "../common/progress/TaskProgress.h" 31 #include "../raster/Band.h" 97 "Mismatch between the classes number and the max angular distances" );
99 for(
unsigned int maxAngularDistancesIdx = 0 ; maxAngularDistancesIdx <
103 maxAngularDistancesIdx ] >= 0.0 ),
104 "Invalid max angular distance" );
110 "Invalid classes samples pointer" )
112 "Invalid classes samples number" )
114 "Invalid classe samples number" )
115 const unsigned int dimsNumber = (
unsigned int)
118 "Invalid dimensions number" )
121 ClassesSamplesT::const_iterator classesIt =
123 const ClassesSamplesT::const_iterator classesItE =
125 unsigned int dimIdx = 0;
126 SamplesT::const_iterator samplesIt;
127 SamplesT::const_iterator samplesItE;
131 while( classesIt != classesItE )
133 const ClassIDT& classID = classesIt->first;
136 const SamplesT& classSamples = classesIt->second;
138 "Invalid class samples number" );
142 SampleT classMeans( dimsNumber );
144 for( dimIdx = 0 ; dimIdx < dimsNumber ; ++dimIdx )
146 double& dimMean = classMeans[ dimIdx ];
149 samplesIt = classSamples.begin();
150 samplesItE = classSamples.end();
152 while( samplesIt != samplesItE )
155 "Sample size mismatch" )
156 dimMean += samplesIt->operator[]( dimIdx );
161 dimMean /= (double)( classSamples.size() );
184 "Classification strategy not initialized" );
187 "Invalid input bands" );
199 std::vector< int >
dt;
202 std::vector< double > noDataValues;
206 "Output raster creation error" );
209 "Output raster palette creation error" );
214 std::unique_ptr< te::common::TaskProgress > progressPtr;
219 progressPtr->setMessage(
"Classifying" );
224 unsigned int col = 0;
225 unsigned int dim = 0;
226 unsigned int classIdx = 0;
227 double angularTR = 0;
228 double angularTT = 0;
229 double angularRR = 0;
230 double angularDist = 0;
231 double minAngularDist = 0;
232 unsigned int minAngularDistClassIdx = 0;
233 const double outNoDataValue =
m_outputRasterPtr->getBand( 0 )->getProperty()->m_noDataValue;
235 std::vector< double > currentPixelValues( nDims );
237 for(
unsigned int row = 0 ; row < nRows ; ++row )
239 for( col = 0 ; col <
nCols ; ++
col )
243 for( dim = 0 ; dim < nDims ; ++dim )
249 if( currentPixelValues[ 0 ] == inNoDataValue )
257 minAngularDist = DBL_MAX;
258 minAngularDistClassIdx = classesNumber;
260 for( classIdx = 0 ; classIdx < classesNumber ; ++classIdx )
268 for( dim = 0 ; dim < nDims ; ++dim )
270 const double& meanValue = classMeans[ dim ];
272 const double& readedValue = currentPixelValues[ dim ];;
274 angularTR += readedValue * meanValue;
275 angularRR += meanValue * meanValue;
276 angularTT += readedValue * readedValue;
279 angularDist = angularTR / ( sqrt( angularTT ) *
282 if( std::abs( angularDist ) > 1.0 )
284 angularDist = DBL_MAX;
288 angularDist = acos( angularDist );
291 if( ( angularDist < minAngularDist ) && ( angularDist <
294 minAngularDist = angularDist;
295 minAngularDistClassIdx = classIdx;
299 if( minAngularDistClassIdx == classesNumber )
309 progressPtr->pulse();
310 if( ! progressPtr->isActive() )
return false;
ClassIDT m_highestTrainClassID
bool execute()
Executes the classification strategy.
Base exception class for plugin module.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
bool createOutputRaster(const std::vector< int > &bandsDataTypes, const std::vector< double > &noDataValues)
Create the output raster using the EXPANSIBLE driver.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
This class can be used to inform the progress of a task.
Spectral Angle Mapper strategy factory.
ClassifierSAMStrategy::Parameters m_initParams
Initialization parameters.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
ClassesSamplesTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
std::vector< double > SampleT
Class sample type definition.
std::vector< unsigned int > m_inputRasterBands
Input raster bands.
const Parameters & operator=(const Parameters ¶ms)
Raster classifier strategy factory base class.
AbstractParameters * clone() const
Create a clone copy of this instance.
unsigned int unsigned int nCols
bool m_isInitialized
Is this instance initialized?
~ClassifierSAMStrategyFactory()
unsigned int getNumberOfRows() const
Returns the raster number of rows.
unsigned int ClassIDT
Class ID type definition (zero means invalid ID).
BandProperty * getProperty()
Returns the band property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Spectral Angle Mapper classification strategy.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the output raster.
bool initialize(ClassifierStrategyParameters const *const strategyParams)
Initialize the classification strategy.
Abstract parameters base interface.
SamplesT m_classesMeans
Classes means.
bool m_progressInterfaceEnabled
Progress interface status.
te::rst::Raster const * m_inputRasterPtr
A pointer to the input raster.
std::vector< ClassIDT > m_classesIndex2ID
An class index ordered vector of classes IDs;.
Classifier Strategy Parameters.
Raster classifier strategy base class.
std::vector< SampleT > SamplesT
Class samples container type definition.
Spectral Angle Mapper classification strategy.
#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...
bool setOutputRasterPalette(const unsigned int size)
Create and set the output raster palette folowing the current internal settings.
std::vector< double > m_maxAngularDistances
This is a vector of maximum acceptable angles (radians) between one pixel spectra and the reference s...
ClassifierSAMStrategyFactory()