summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/hotelling.cc
diff options
context:
space:
mode:
Diffstat (limited to '2005/i/robert/src/ovision/see/hotelling.cc')
-rw-r--r--2005/i/robert/src/ovision/see/hotelling.cc100
1 files changed, 85 insertions, 15 deletions
diff --git a/2005/i/robert/src/ovision/see/hotelling.cc b/2005/i/robert/src/ovision/see/hotelling.cc
index b492510..ac490d0 100644
--- a/2005/i/robert/src/ovision/see/hotelling.cc
+++ b/2005/i/robert/src/ovision/see/hotelling.cc
@@ -7,46 +7,63 @@
#include "hotelling.hh"
/// Constructeur
-Hotelling::Hotelling (const std::vector<uint8_t[2]> &dataList)
+Hotelling::Hotelling (const std::vector<Hpoint> &dataList)
{
- e1_[0] = 0; e1_[1] = 0; e2_[0] = 0; e2_[1] = 0;
oconfig_ = OConfig::getInstance ();
-
dataSize_ = dataList.size ();
if (dataSize_ == 0)
- {
meanX_ = meanY_ = 0;
- }
else
{
+ // Calcul de la moyenne
mean (dataList);
- data_[0] = new uint8_t[dataSize_];
- data_[1] = new uint8_t[dataSize_];
+ // Initialisation des données
+ data_ = new Hpoint[dataSize_];
+ // Changement de repère des données
+ int i = 0;
+ for (std::vector<Hpoint>::const_iterator iter = dataList.begin ();
+ iter != dataList.end (); ++iter)
+ {
+ data_[i].set ((*iter)[0] - meanX_, (*iter)[1] - meanY_);
+ ++i;
+ }
}
-
}
/// Destructeur
Hotelling::~Hotelling (void)
{
- delete [] covMatrix_;
delete [] data_;
}
+/// Covariance
+double
+Hotelling::cov (const unsigned i, const unsigned j)
+{
+ double c = 0;
+ for (unsigned k = 0; k < dataSize_; ++k)
+ c += data_[k][i] * data_[k][j];
+ c /= (dataSize_-1);
+ return c;
+}
+
/// Calcul de la matrice de covariance
-uint8_t*
-Hotelling::cov ()
+void
+Hotelling::covMatrix ()
{
- uint8_t *tab;
- return tab;
+ /// Calcul de la covariance
+ covMatrix_[0][0] = cov (0, 0);
+ covMatrix_[0][1] = cov (0, 1);
+ covMatrix_[1][0] = covMatrix_[0][1];
+ covMatrix_[1][1] = cov (1, 1);
}
/// Calcul de la moyenne des deux composantes
void
-Hotelling::mean (const std::vector<uint8_t[2]> &dataList)
+Hotelling::mean (const std::vector<Hpoint> &dataList)
{
meanX_ = meanY_ = 0;
- for (std::vector<uint8_t[2]>::const_iterator iter = dataList.begin ();
+ for (std::vector<Hpoint>::const_iterator iter = dataList.begin ();
iter != dataList.end (); ++iter)
{
meanX_ += (*iter)[0];
@@ -56,9 +73,62 @@ Hotelling::mean (const std::vector<uint8_t[2]> &dataList)
meanY_ /= dataSize_;
}
+/// Calcul des eigenvalues
+void
+Hotelling::eigenValues ()
+{
+ double apd = covMatrix_[0][0] + covMatrix_[1][1];
+ double sqrtdelta = sqrt (apd*apd - 4* (covMatrix_[0][0]*covMatrix_[1][1] - covMatrix_[1][0]*covMatrix_[1][0]));
+ val1_ = (apd - sqrtdelta)/(2);
+ val2_ = (apd + sqrtdelta)/(2);
+}
+
+
/// Calcul des eigenvectors
void
Hotelling::eigenVectors ()
{
+ covMatrix ();
+ eigenValues ();
+ // Calcul des vecteurs
+ e2_.set (1, covMatrix_[1][0] / (val2_ - covMatrix_[1][1]));
+ e1_.set (1, covMatrix_[1][0] / (val1_ - covMatrix_[1][1]));
+ // Normalisation
+ e1_.norm ();
+ e2_.norm ();
+}
+
+/// Accessors
+void
+Hotelling::getPC (Hpoint &pca)
+{
+ if (val1_>val2_) pca = e1_;
+ else pca = e2_;
+}
+void
+Hotelling::getPC (double &x, double &y)
+{
+ Hpoint h;
+ getPC (h);
+ x = h[0]; y = h[1];
}
+/// Affiche la matrice de covariance
+void
+Hotelling::showCovMatrix () const
+{
+ std::cout << "Matrice de covariance :\n"
+ << "\t" << covMatrix_[0][0] << "\n"
+ << "\t" << covMatrix_[0][1] << "\n"
+ << "\t" << covMatrix_[1][0] << "\n"
+ << "\t" << covMatrix_[1][1] << std::endl;
+}
+
+/// Affiche les eigenvectors
+void
+Hotelling::showEigenVectors () const
+{
+ std::cout << "EigenVectors :\n"
+ << "\t" << e1_[0] << ", " << e1_[1] << "\n"
+ << "\t" << e2_[0] << ", " << e2_[1] << std::endl;
+}