testKMeans.cpp 2.4 KB

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