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++)
235 newMean.push_back(rand() % (
int) (maxValues[i] - minValues[i]));
241 std::vector<double> values;
242 std::map<unsigned int, std::vector<double> > tmpValues;
243 std::vector<unsigned int> tmpNs;
244 unsigned int tmpClass;
245 double distanceKMeans;
247 std::vector<std::vector<double> > oldKMeans;
250 tmpValues[k] = std::vector<double>(attributesIndices.size());
259 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
260 tmpValues[k][i] = 0.0;
269 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
270 values.push_back((*it)[attributesIndices[i]]);
272 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
273 tmpValues[tmpClass][i] += values[i];
281 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
283 m_KMeans[k][i] = (double) (tmpValues[k][i] / (
double) tmpNs[k]);
286 distanceKMeans = 0.0;
288 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
290 a_minus_b =
m_KMeans[k][i] - oldKMeans[k][i];
291 distanceKMeans += a_minus_b * a_minus_b;
293 distanceKMeans = sqrt(distanceKMeans);
303 template<
class TTRAIN,
class TCLASSIFY>
305 const std::vector<unsigned int>& attributesIndices,
306 std::vector<unsigned int>& classification,
307 const bool enableProgressInterface)
throw(te::cl::Exception)
309 TCLASSIFY it = itBegin;
310 std::vector<double> values(attributesIndices.size());
311 classification.clear();
320 classification.resize(N);
328 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
329 values[i] = (*it)[attributesIndices[i]];
339 template<
class TTRAIN,
class TCLASSIFY>
342 double minDistance = std::numeric_limits<double>::max();
344 unsigned int clusterNumber = 0;
349 if (distance < minDistance)
351 minDistance = distance;
356 return clusterNumber;
359 #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.
An exception class for the Classification module.
bool m_isInitialized
True if this instance is initialized.
AbstractParameters * clone() const
Create a clone copy of this instance.
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)