30 #include "../common/progress/TaskProgress.h"
63 m_trainSamplesPtr.reset();
64 m_maxAngularDistances.clear();
99 "Mismatch between the classes number and the max angular distances" );
101 for(
unsigned int maxAngularDistancesIdx = 0 ; maxAngularDistancesIdx <
105 maxAngularDistancesIdx ] >= 0.0 ),
106 "Invalid max angular distance" );
112 "Invalid classes samples pointer" )
114 "Invalid classes samples number" )
116 "Invalid classe samples number" )
117 const unsigned int dimsNumber = (
unsigned int)
120 "Invalid dimensions number" )
123 ClassesSamplesT::const_iterator classesIt =
125 const ClassesSamplesT::const_iterator classesItE =
127 unsigned int dimIdx = 0;
128 SamplesT::const_iterator samplesIt;
129 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" );
140 SampleT classMeans( dimsNumber );
142 for( dimIdx = 0 ; dimIdx < dimsNumber ; ++dimIdx )
144 double& dimMean = classMeans[ dimIdx ];
147 samplesIt = classSamples.begin();
148 samplesItE = classSamples.end();
150 while( samplesIt != samplesItE )
153 "Sample size mismatch" )
154 dimMean += samplesIt->operator[]( dimIdx );
159 dimMean /= (double)( classSamples.size() );
180 const std::vector<unsigned int>& inputRasterBands,
182 const unsigned int outputRasterBand,
183 const bool enableProgressInterface)
throw(te::rp::Exception)
186 "Classification strategy not initialized" );
189 "Invalid input bands" );
191 "Invalid output band" );
193 outputRaster.getNumberOfColumns(),
"Rasters dims mismatch" );
195 outputRaster.getNumberOfRows(),
"Rasters dims mismatch" );
199 std::auto_ptr< te::common::TaskProgress > progressPtr;
200 if( enableProgressInterface )
203 progressPtr->setTotalSteps( inputRaster.getNumberOfRows() );
204 progressPtr->setMessage(
"Classifying" );
209 const unsigned int classesNumber = (
unsigned int)
m_classesMeans.size();
210 const unsigned int nRows = (
unsigned int)inputRaster.getNumberOfRows();
211 const unsigned int nCols = (
unsigned int)inputRaster.getNumberOfColumns();
212 const unsigned int nDims = (
unsigned int)
m_classesMeans[ 0 ].size();
213 unsigned int col = 0;
214 unsigned int dim = 0;
215 unsigned int classIdx = 0;
216 double angularTR = 0;
217 double angularTT = 0;
218 double angularRR = 0;
219 double angularDist = 0;
220 double readedValue = 0;
221 double minAngularDist = 0;
222 unsigned int minAngularDistClassIdx = 0;
224 for(
unsigned int row = 0 ; row < nRows ; ++row )
226 for( col = 0 ; col < nCols ; ++col )
230 minAngularDist = DBL_MAX;
231 minAngularDistClassIdx = classesNumber;
233 for( classIdx = 0 ; classIdx < classesNumber ; ++classIdx )
241 for( dim = 0 ; dim < nDims ; ++dim )
243 const double& meanValue = classMeans[ dim ];
245 inputRaster.getValue( col, row, readedValue, inputRasterBands[ dim ] );
247 angularTR += readedValue * meanValue;
248 angularRR += meanValue * meanValue;
249 angularTT += readedValue * readedValue;
252 angularDist = angularTR / ( sqrt( angularTT ) *
255 if( std::abs( angularDist ) > 1.0 )
257 angularDist = DBL_MAX;
261 angularDist = acos( angularDist );
264 if( ( angularDist < minAngularDist ) && ( angularDist <
267 minAngularDist = angularDist;
268 minAngularDistClassIdx = classIdx;
272 if( minAngularDistClassIdx == classesNumber )
273 outputRaster.setValue( col, row, 0, 0 );
279 if( enableProgressInterface )
281 progressPtr->pulse();
282 if( ! progressPtr->isActive() )
return false;
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...
ClassesSamplesTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
std::vector< double > SampleT
Class sample type definition.
const Parameters & operator=(const Parameters ¶ms)
#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...
Raster classifier strategy factory base class.
AbstractParameters * clone() const
Create a clone copy of this instance.
bool m_isInitialized
Is this instance initialized?
~ClassifierSAMStrategyFactory()
An abstract class for raster data strucutures.
unsigned int ClassIDT
Class ID type definition (zero means invalid ID).
Raster strategy parameters base class.
bool initialize(StrategyParameters const *const strategyParams)
Initialize the classification strategy.
Spectral Angle Mapper classification strategy.
Abstract parameters base interface.
SamplesT m_classesMeans
Classes means.
std::vector< ClassIDT > m_classesIndex2ID
An class index ordered vector of classes IDs;.
Raster classifier strategy base class.
std::vector< SampleT > SamplesT
Class samples container type definition.
Spectral Angle Mapper classification strategy.
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
std::vector< double > m_maxAngularDistances
This is a vector of maximum acceptable angles (radians) between one pixel spectra and the reference s...
ClassifierSAMStrategyFactory()
bool execute(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< te::gm::Polygon * > &inputPolygons, te::rst::Raster &outputRaster, const unsigned int outputRasterBand, const bool enableProgressInterface)
Executes the classification strategy.