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>
 
   87             void reset() throw(te::cl::Exception);
 
  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     TR_CL(
"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;
 
  314     for (
unsigned int i = 0; i < attributesIndices.size(); i++)
 
  315       values.push_back((*it)[attributesIndices[i]]);
 
  324 template<
class TTRAIN, 
class TCLASSIFY>
 
  327   double minDistance = std::numeric_limits<double>::max();
 
  329   unsigned int clusterNumber = 0;
 
  334     if (distance < minDistance)
 
  336       minDistance = distance;
 
  341   return clusterNumber;
 
  344 #endif // __TERRALIB_CLASSIFICATION_INTERNAL_KMEANS_H 
KMeans strategy for classification. Step-by-step: 
 
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 initialize(const Parameters ¶ms)
 
AbstractParameters()
Constructor. 
 
Abstract parameters base interface. 
 
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1. 
 
Configuration flags for the Terrralib Classification module. 
 
unsigned int m_K
The number of clusters (means) to detect in image. 
 
unsigned int m_maxIterations
The maximum of iterations to perform if convergence is not achieved. 
 
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
 
double m_epsilon
The stop criteria. When the clusters change in a value smaller then epsilon, the convergence is achie...
 
std::vector< std::vector< double > > m_KMeans
The vector of K means. 
 
bool m_isInitialized
True if this instance is initialized. 
 
Utility functions for Classification. 
 
An exception class for the Classification module. 
 
TECLEXPORT double GetEuclideanDistance(std::vector< double > v1, std::vector< double > v2)
Computes euclidean distance between two double vectors. 
 
#define TR_CL(message)
It marks a string in order to get translated. This is a special mark used in the Terrralib Classifica...
 
This class can be used to inform the progress of a task. 
 
AbstractParameters * clone() const 
Create a clone copy of this instance. 
 
const Parameters & operator=(const Parameters ¶ms)
 
bool classify(TCLASSIFY &itBegin, TCLASSIFY &itEnd, const std::vector< unsigned int > &attributesIndices, std::vector< unsigned int > &classification, const bool enableProgressInterface)
 
Parameters m_parameters
Internal execution parameters.