GSCluster.cpp 2.6 KB

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