26 #ifndef __TERRALIB_CLASSIFICATION_INTERNAL_KMEANS_H 27 #define __TERRALIB_CLASSIFICATION_INTERNAL_KMEANS_H 30 #include "../common/AbstractParameters.h" 31 #include "../common/progress/TaskProgress.h" 32 #include "../raster/Grid.h" 61 template<
class TTRAIN,
class TCLASSIFY>
101 bool train(TTRAIN& itBegin, TTRAIN& itEnd,
102 const std::vector<
unsigned int>& attributesIndices,
103 const std::vector<
unsigned int>& labels,
104 const
bool enableProgressInterface) throw(
te::cl::
Exception);
106 bool classify(TCLASSIFY& itBegin, TCLASSIFY& itEnd,
107 const std::vector<
unsigned int>& attributesIndices,
108 std::vector<
unsigned int>& classification,
109 const
bool enableProgressInterface) throw(
te::cl::
Exception);
124 template <class TTRAIN, class TCLASSIFY>
130 template <
class TTRAIN,
class TCLASSIFY>
135 template <
class TTRAIN,
class TCLASSIFY>
147 template <
class TTRAIN,
class TCLASSIFY>
155 template <
class TTRAIN,
class TCLASSIFY>
162 template <
class TTRAIN,
class TCLASSIFY>
169 template <
class TTRAIN,
class TCLASSIFY>
174 template <
class TTRAIN,
class TCLASSIFY>
184 throw te::cl::Exception(
TE_TR(
"The value of K must be at least 2."));
197 template<
class TTRAIN,
class TCLASSIFY>
199 const std::vector<unsigned int>& attributesIndices,
200 const std::vector<unsigned int>& labels,
201 const bool enableProgressInterface)
throw(te::cl::Exception)
203 std::vector<double> maxValues;
204 std::vector<double> minValues;
206 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
208 maxValues.push_back(std::numeric_limits<double>::min());
209 minValues.push_back(std::numeric_limits<double>::max());
213 std::vector<unsigned int> tmpClassification;
217 tmpClassification.push_back(0);
218 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
220 if ((*it)[attributesIndices[i]] > maxValues[i])
221 maxValues[i] = (*it)[attributesIndices[i]];
222 if ((*it)[attributesIndices[i]] < minValues[i])
223 minValues[i] = (*it)[attributesIndices[i]];
228 srand((
unsigned) time(0));
233 std::vector<double> newMean;
234 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
237 ( maxValues[i] == minValues[i] )
239 ( ((
double)rand()) * maxValues[i] / ((
double)RAND_MAX) )
242 ( ( (
double)rand() ) / ((
double)RAND_MAX) )
244 ( maxValues[i] - minValues[i] )
253 std::vector<double> values;
254 std::map<unsigned int, std::vector<double> > tmpValues;
255 std::vector<unsigned int> tmpNs;
256 unsigned int tmpClass;
257 double distanceKMeans;
259 std::vector<std::vector<double> > oldKMeans;
262 tmpValues[k] = std::vector<double>(attributesIndices.size());
271 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
272 tmpValues[k][i] = 0.0;
281 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
282 values.push_back((*it)[attributesIndices[i]]);
284 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
285 tmpValues[tmpClass][i] += values[i];
293 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
295 m_KMeans[k][i] = (double) (tmpValues[k][i] / (
double) tmpNs[k]);
298 distanceKMeans = 0.0;
300 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
302 a_minus_b =
m_KMeans[k][i] - oldKMeans[k][i];
303 distanceKMeans += a_minus_b * a_minus_b;
305 distanceKMeans = sqrt(distanceKMeans);
315 template<
class TTRAIN,
class TCLASSIFY>
317 const std::vector<unsigned int>& attributesIndices,
318 std::vector<unsigned int>& classification,
319 const bool enableProgressInterface)
throw(te::cl::Exception)
321 TCLASSIFY it = itBegin;
322 std::vector<double> values(attributesIndices.size());
323 classification.clear();
332 classification.resize(N);
340 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
341 values[i] = (*it)[attributesIndices[i]];
351 template<
class TTRAIN,
class TCLASSIFY>
354 double minDistance = std::numeric_limits<double>::max();
356 unsigned int clusterNumber = 0;
361 if (distance < minDistance)
363 minDistance = distance;
368 return clusterNumber;
371 #endif // __TERRALIB_CLASSIFICATION_INTERNAL_KMEANS_H
unsigned int m_maxIterations
The maximum of iterations to perform if convergence is not achieved.
bool initialize(const Parameters ¶ms)
Base exception class for plugin module.
double m_epsilon
The stop criteria. When the clusters change in a value smaller then epsilon, the convergence is achie...
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.
const Parameters & operator=(const Parameters ¶ms)
Parameters m_parameters
Internal execution parameters.
bool classify(TCLASSIFY &itBegin, TCLASSIFY &itEnd, const std::vector< unsigned int > &attributesIndices, std::vector< unsigned int > &classification, const bool enableProgressInterface)
#define TE_TR(message)
It marks a string in order to get translated.
Configuration flags for the Terrralib Classification module.
std::vector< std::vector< double > > m_KMeans
The vector of K means.
unsigned int m_K
The number of clusters (means) to detect in image.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
AbstractParameters()
Constructor.
KMeans strategy for classification. Step-by-step:
Utility functions for Classification.
Abstract parameters base interface.
TECLEXPORT double GetEuclideanDistance(std::vector< double > v1, std::vector< double > v2)
Computes euclidean distance between two double vectors.
AbstractParameters * clone() const
Create a clone copy of this instance.
An exception class for the Classification module.
bool m_isInitialized
True if this instance is initialized.
unsigned int getClassification(std::vector< double > values)
bool train(TTRAIN &itBegin, TTRAIN &itEnd, const std::vector< unsigned int > &attributesIndices, const std::vector< unsigned int > &labels, const bool enableProgressInterface)