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"
62 template<
class TTRAIN,
class TCLASSIFY>
102 bool train(TTRAIN& itBegin, TTRAIN& itEnd,
103 const std::vector<unsigned int>& attributesIndices,
104 const std::vector<unsigned int>& labels,
105 const bool enableProgressInterface) ;
107 bool classify(TCLASSIFY& itBegin, TCLASSIFY& itEnd,
108 const std::vector<unsigned int>& attributesIndices,
109 std::vector<unsigned int>& classification,
110 const bool enableProgressInterface) ;
125 template <
class TTRAIN,
class TCLASSIFY>
131 template <
class TTRAIN,
class TCLASSIFY>
136 template <
class TTRAIN,
class TCLASSIFY>
148 template <
class TTRAIN,
class TCLASSIFY>
156 template <
class TTRAIN,
class TCLASSIFY>
163 template <
class TTRAIN,
class TCLASSIFY>
170 template <
class TTRAIN,
class TCLASSIFY>
175 template <
class TTRAIN,
class TCLASSIFY>
185 throw te::cl::Exception(
TE_TR(
"The value of K must be at least 2."));
198 template<
class TTRAIN,
class TCLASSIFY>
200 const std::vector<unsigned int>& attributesIndices,
201 const std::vector<unsigned int>& labels,
202 const bool enableProgressInterface)
204 std::vector<double> maxValues;
205 std::vector<double> minValues;
207 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
209 maxValues.push_back(std::numeric_limits<double>::min());
210 minValues.push_back(std::numeric_limits<double>::max());
214 std::vector<unsigned int> tmpClassification;
218 tmpClassification.push_back(0);
219 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
221 if ((*it)[attributesIndices[i]] > maxValues[i])
222 maxValues[i] = (*it)[attributesIndices[i]];
223 if ((*it)[attributesIndices[i]] < minValues[i])
224 minValues[i] = (*it)[attributesIndices[i]];
229 srand((
unsigned) time(0));
234 std::vector<double> newMean;
235 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
238 ( maxValues[i] == minValues[i] )
240 ( ((
double)rand()) * maxValues[i] / ((
double)RAND_MAX) )
243 ( ( (
double)rand() ) / ((
double)RAND_MAX) )
245 ( maxValues[i] - minValues[i] )
254 std::vector<double> values;
255 std::map<unsigned int, std::vector<double> > tmpValues;
256 std::vector<unsigned int> tmpNs;
257 unsigned int tmpClass;
258 double distanceKMeans;
260 std::vector<std::vector<double> > oldKMeans;
263 tmpValues[k] = std::vector<double>(attributesIndices.size());
272 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
273 tmpValues[k][i] = 0.0;
282 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
283 values.push_back((*it)[attributesIndices[i]]);
285 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
286 tmpValues[tmpClass][i] += values[i];
294 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
296 m_KMeans[k][i] = (double) (tmpValues[k][i] / (
double) tmpNs[k]);
299 distanceKMeans = 0.0;
301 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
303 a_minus_b =
m_KMeans[k][i] - oldKMeans[k][i];
304 distanceKMeans += a_minus_b * a_minus_b;
306 distanceKMeans = sqrt(distanceKMeans);
316 template<
class TTRAIN,
class TCLASSIFY>
318 const std::vector<unsigned int>& attributesIndices,
319 std::vector<unsigned int>& classification,
320 const bool enableProgressInterface)
322 TCLASSIFY it = itBegin;
323 std::vector<double> values(attributesIndices.size());
324 classification.clear();
333 classification.resize(N);
341 for (
unsigned int i = 0; i < attributesIndices.size(); i++)
342 values[i] = (*it)[attributesIndices[i]];
352 template<
class TTRAIN,
class TCLASSIFY>
355 double minDistance = std::numeric_limits<double>::max();
357 unsigned int clusterNumber = 0;
362 if (distance < minDistance)
364 minDistance = distance;
369 return clusterNumber;
#define TE_TR(message)
It marks a string in order to get translated.
const Parameters & operator=(const Parameters ¶ms)
AbstractParameters * clone() const
Create a clone copy of this instance.
unsigned int m_K
The number of clusters (means) to detect in image.
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.
unsigned int m_maxIterations
The maximum of iterations to perform if convergence is not achieved.
KMeans strategy for classification. Step-by-step:
Parameters m_parameters
Internal execution parameters.
bool train(TTRAIN &itBegin, TTRAIN &itEnd, const std::vector< unsigned int > &attributesIndices, const std::vector< unsigned int > &labels, const bool enableProgressInterface)
unsigned int getClassification(std::vector< double > values)
bool m_isInitialized
True if this instance is initialized.
std::vector< std::vector< double > > m_KMeans
The vector of K means.
bool initialize(const Parameters ¶ms)
bool classify(TCLASSIFY &itBegin, TCLASSIFY &itEnd, const std::vector< unsigned int > &attributesIndices, std::vector< unsigned int > &classification, const bool enableProgressInterface)
Abstract parameters base interface.
AbstractParameters()
Constructor.
This class can be used to inform the progress of a task.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
TECLEXPORT double GetEuclideanDistance(std::vector< double > v1, std::vector< double > v2)
Computes euclidean distance between two double vectors.
Proxy configuration file for TerraView (see terraview_config.h).
An exception class for the XML module.