testKMeans.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef __clang__
  25. #ifndef __llvm__
  26. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  27. #endif
  28. #endif
  29. NICE::Image panel(200, 200);
  30. NICE::Image overlay(panel.width(), panel.height());
  31. panel.set(255);
  32. overlay.set(0);
  33. VVector x;
  34. VVector prototypes;
  35. std::vector<double> weights;
  36. std::vector<int> assignments;
  37. char c;
  38. const int noClasses = 2;
  39. do
  40. {
  41. vector<CoordT<double> > points;
  42. NICE::selectPoints(panel, points, "Select some points!");
  43. x.clear();
  44. for (vector<CoordT<double> >::const_iterator i = points.begin();
  45. i != points.end(); i++)
  46. {
  47. NICE::Vector xvec(2);
  48. xvec[0] = i->x;
  49. xvec[1] = i->y;
  50. x.push_back(xvec);
  51. }
  52. ClusterAlgorithm *clusteralg = new GMM(noClasses);
  53. // ClusterAlgorithm *clusteralg = new KMeans ( noClasses);
  54. // ClusterAlgorithm *clusteralg = new HierarchicalCluster( 0); //automatic cluster size estimation
  55. // ClusterAlgorithm *clusteralg = new KMeansHeuristic ( noClasses);
  56. //ClusterAlgorithm *clusteralg = new SpectralCluster ( noClasses, alpha );
  57. //ClusterAlgorithm *clusteralg = new KMeansOnline ( noClasses );
  58. clusteralg->cluster(x, prototypes, weights, assignments);
  59. delete clusteralg;
  60. overlay.set(0);
  61. for (size_t i = 0 ; i < prototypes.size() ; i++)
  62. {
  63. fprintf(stderr, "prototype %d (%f, %f)\n", (int)i, prototypes[i][0], prototypes[i][1]);
  64. Cross cross(Coord((int)prototypes[i][0], (int)prototypes[i][1]), 7);
  65. overlay.draw(cross, i + 1);
  66. }
  67. fprintf(stderr, "assignments: %d\n", (int)assignments.size());
  68. for (size_t i = 0 ; i < assignments.size() ; i++)
  69. {
  70. Cross cross(Coord((int)x[i][0], (int)x[i][1]) , 7);
  71. overlay.draw(cross, assignments[i] + 1);
  72. }
  73. NICE::showImageOverlay(panel, overlay, "Clustering results");
  74. c = getchar();
  75. }
  76. while (c != 'q');
  77. return 0;
  78. }
  79. #endif