30 #include "../raster/Band.h" 31 #include "../raster/BandProperty.h" 32 #include "../common/MatrixUtils.h" 97 "Invalid classes samples pointer" )
99 "Invalid classes samples number" )
101 "Invalid classes samples number" )
108 const unsigned int dimsNumber =
static_cast<unsigned int>(castParamsPtr->
m_trainSamplesPtr->begin()->second.operator[]( 0 ).size());
111 Parameters::MClassesSamplesCT::const_iterator classesIt =
113 const Parameters::MClassesSamplesCT::const_iterator classesItE =
119 while( classesIt != classesItE )
125 "Invalid class samples number" );
129 unsigned int dimIdx = 0;
134 for( dimIdx = 0 ; dimIdx < dimsNumber ; ++dimIdx )
136 double& dimMean = classMeans[ dimIdx ];
139 Parameters::ClassSamplesContainerT::const_iterator samplesIt =
140 classSamples.begin();
141 const Parameters::ClassSamplesContainerT::const_iterator samplesItE =
145 while( samplesIt != samplesItE )
148 "Sample size mismatch" )
149 dimMean += samplesIt->operator[]( dimIdx );
155 dimMean /=
static_cast<double>( classSamples.size() );
177 "Classification strategy not initialized" );
180 "Invalid input bands" );
184 std::vector< int >
dt;
187 std::vector< double > noDataValues;
191 "Output raster creation error" );
194 "Output raster palette creation error" );
198 std::unique_ptr< te::common::TaskProgress > progressPtr;
203 progressPtr->setMessage(
"Classifying" );
208 const unsigned int classesNumber =
static_cast<unsigned int>(
m_classesMeans.size());
212 const double outNoDataValue =
m_outputRasterPtr->getBand( 0 )->getProperty()->m_noDataValue;
214 unsigned int col = 0;
215 unsigned int dim = 0;
216 boost::numeric::ublas::matrix< double > sample( nDims, 1 );
217 unsigned int classIdx = 0;
220 unsigned int bestClass = 0;
223 for(
unsigned int row = 0 ; row < nRows ; ++row )
225 for( col = 0 ; col <
nCols ; ++
col )
229 for( dim = 0 ; dim < nDims ; ++dim )
234 if( sample( 0, 0 ) == inNoDataValue )
240 unsigned int refClass = bestClass;
247 for( classIdx = 0 ; classIdx < classesNumber ; ++classIdx )
252 if( classIdx == refClass )
258 for( dim = 0 ; dim < nDims ; ++dim )
261 dist += (sample(dim, 0) - classMeans[classIdx]) * (sample(dim, 0) - classMeans[classIdx]);
269 if( dist < bestDist )
272 bestClass = classIdx;
283 progressPtr->pulse();
284 if( ! progressPtr->isActive() )
return false;
ClassifierEDStrategy::Parameters m_initParams
Initialization parameters.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
std::vector< std::vector< double > > m_classesMeans
Classes means;.
bool m_isInitialized
Is this instance initialized?
bool createOutputRaster(const std::vector< int > &bandsDataTypes, const std::vector< double > &noDataValues)
Create the output raster using the EXPANSIBLE driver.
AbstractParameters * clone() const
Create a clone copy of this instance.
This class can be used to inform the progress of a task.
const Parameters & operator=(const Parameters ¶ms)
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
std::vector< unsigned int > m_inputRasterBands
Input raster bands.
ClassifierEDStrategyFactory()
MClassesSamplesCTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
Raster classifier strategy factory base class.
std::vector< Parameters::ClassIDT > m_classesIndex2ID
An class index ordered vector of classes IDs;.
unsigned int unsigned int nCols
unsigned int getOutputNumberBands(void)
bool execute() _NOEXCEPT_OP(false)
Executes the classification strategy.
Euclidean Distance Classifier strategy.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
BandProperty * getProperty()
Returns the band property.
static te::dt::TimeDuration dt(20, 30, 50, 11)
Parameters::ClassIDT m_highestTrainClassID
The highest train class ID.
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state...
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
~ClassifierEDStrategyFactory()
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.
Abstract parameters base interface.
bool m_progressInterfaceEnabled
Progress interface status.
bool initialize(ClassifierStrategyParameters const *const strategyParams) _NOEXCEPT_OP(false)
Initialize the classification strategy.
te::rst::Raster const * m_inputRasterPtr
A pointer to the input raster.
Classifier Strategy Parameters.
Raster classifier strategy base class.
Euclidean Distance Classifier 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< ClassSampleT > ClassSamplesContainerT
Class samples container type definition.
std::vector< double > ClassSampleT
Class sample type definition.
Euclidean Distance strategy factory.
unsigned int ClassIDT
Class ID type definition (zero means invalid ID).