testKMeans.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifdef NOVISUAL
  2. #warning "testKMeans needs ICE with visualization !!"
  3. int main(int argc, char **argv) {};
  4. #else
  5. #include "core/vector/VectorT.h"
  6. #include "core/vector/MatrixT.h"
  7. #include "core/image/ImageT.h"
  8. #include "core/imagedisplay/ImageDisplay.h"
  9. #include <iostream>
  10. #include <core/image/CrossT.h>
  11. #include <core/imagedisplay/SimpleSelector.h>
  12. using namespace std;
  13. using namespace NICE;
  14. #include <vislearning/math/cluster/KMeans.h>
  15. #include <vislearning/math/cluster/KMeansOnline.h>
  16. #include <vislearning/math/cluster/KMeansMatlab.h>
  17. #include <vislearning/math/cluster/KMeansHeuristic.h>
  18. #include <vislearning/math/cluster/SpectralCluster.h>
  19. #include <vislearning/math/cluster/HierarchicalCluster.h>
  20. #include <vislearning/math/cluster/GMM.h>
  21. using namespace OBJREC;
  22. int main(int argc, char **argv)
  23. {
  24. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  25. NICE::Image panel(200, 200);
  26. NICE::Image overlay(panel.width(), panel.height());
  27. panel.set(255);
  28. overlay.set(0);
  29. VVector x;
  30. VVector prototypes;
  31. std::vector<double> weights;
  32. std::vector<int> assignments;
  33. char c;
  34. const int noClasses = 2;
  35. do
  36. {
  37. vector<CoordT<double> > points;
  38. NICE::selectPoints(panel, points, "Select some points!");
  39. x.clear();
  40. for (vector<CoordT<double> >::const_iterator i = points.begin();
  41. i != points.end(); i++)
  42. {
  43. NICE::Vector xvec(2);
  44. xvec[0] = i->x;
  45. xvec[1] = i->y;
  46. x.push_back(xvec);
  47. }
  48. ClusterAlgorithm *clusteralg = new GMM(noClasses);
  49. // ClusterAlgorithm *clusteralg = new KMeans ( noClasses);
  50. // ClusterAlgorithm *clusteralg = new HierarchicalCluster( 0); //automatic cluster size estimation
  51. // ClusterAlgorithm *clusteralg = new KMeansHeuristic ( noClasses);
  52. //ClusterAlgorithm *clusteralg = new SpectralCluster ( noClasses, alpha );
  53. //ClusterAlgorithm *clusteralg = new KMeansOnline ( noClasses );
  54. clusteralg->cluster(x, prototypes, weights, assignments);
  55. delete clusteralg;
  56. overlay.set(0);
  57. for (size_t i = 0 ; i < prototypes.size() ; i++)
  58. {
  59. fprintf(stderr, "prototype %d (%f, %f)\n", (int)i, prototypes[i][0], prototypes[i][1]);
  60. Cross cross(Coord((int)prototypes[i][0], (int)prototypes[i][1]), 7);
  61. overlay.draw(cross, i + 1);
  62. }
  63. fprintf(stderr, "assignments: %d\n", (int)assignments.size());
  64. for (size_t i = 0 ; i < assignments.size() ; i++)
  65. {
  66. Cross cross(Coord((int)x[i][0], (int)x[i][1]) , 7);
  67. overlay.draw(cross, assignments[i] + 1);
  68. }
  69. NICE::showImageOverlay(panel, overlay, "Clustering results");
  70. c = getchar();
  71. }
  72. while (c != 'q');
  73. return 0;
  74. }
  75. #endif