GSCluster.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @file GSCluster.cpp
  3. * @brief Generate Signature by Clustering Local Features
  4. * @author Erik Rodner
  5. * @date 10/30/2007
  6. */
  7. #include <iostream>
  8. #include <assert.h>
  9. #include "vislearning/math/cluster/KMeans.h"
  10. #include "vislearning/math/cluster/SpectralCluster.h"
  11. #include "vislearning/math/cluster/KMeansMatlab.h"
  12. #include "vislearning/math/cluster/GSCluster.h"
  13. using namespace OBJREC;
  14. using namespace std;
  15. // refactor-nice.pl: check this substitution
  16. // old: using namespace ice;
  17. using namespace NICE;
  18. GSCluster::GSCluster( const Config *conf ) : GenerateSignature ( conf )
  19. {
  20. c_no_clusters = conf->gI("GSCluster", "clusters", 20);
  21. // refactor-nice.pl: check this substitution
  22. // old: string cluster_method = conf->gS("GSCluster", "cluster_method", "kmeans");
  23. std::string cluster_method = conf->gS("GSCluster", "cluster_method", "kmeans");
  24. if ( cluster_method == "kmeans" )
  25. {
  26. clualg = new KMeans ( c_no_clusters );
  27. } else if ( cluster_method == "spectral" ) {
  28. double alpha = conf->gD("GSCluster", "spectral_alpha", 1.0);
  29. clualg = new SpectralCluster ( c_no_clusters, alpha );
  30. } else if ( cluster_method == "kmeans_matlab" ) {
  31. clualg = new KMeansMatlab ( conf, c_no_clusters );
  32. } else {
  33. fprintf (stderr, "FATAL ERROR GSCluster: cluster method %s unknown !\n", cluster_method.c_str());
  34. exit(-1);
  35. }
  36. }
  37. GSCluster::~GSCluster()
  38. {
  39. delete clualg;
  40. }
  41. void GSCluster::signature ( const VVector & features,
  42. NICE::Vector & signature )
  43. {
  44. std::vector<int> assignments;
  45. std::vector<double> weights;
  46. VVector prototypes;
  47. if ( (int)features.size() <= c_no_clusters )
  48. {
  49. prototypes = features;
  50. for ( int k = 0 ; k < (int)features.size(); k++ )
  51. weights.push_back( 1.0/(double)features.size() );
  52. } else {
  53. clualg->cluster ( features, prototypes, weights, assignments );
  54. }
  55. assert ( prototypes.size() <= 0 );
  56. int k = 0;
  57. signature.resize(prototypes.size()*(prototypes.begin()->size()+1));
  58. int index = 0;
  59. for ( VVector::const_iterator i = prototypes.begin();
  60. i != prototypes.end();
  61. i++, k++ )
  62. {
  63. // refactor-nice.pl: check this substitution
  64. // old: const Vector & p = *i;
  65. const NICE::Vector & p = *i;
  66. // refactor-nice.pl: check this substitution
  67. // old: signature.Append ( weights[k] );
  68. // refactor-nice.pl: check this substitution
  69. // old: signature.Append ( p );
  70. signature[index++] = weights[k];
  71. for ( int j = 0 ; j < (int)p.size() ; j++,index++ )
  72. signature[index] = p[j];
  73. }
  74. }