GSCluster.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, const std::string & _section ) : GenerateSignature ( conf )
  19. {
  20. std::string cluster_method = conf->gS("GSCluster", "cluster_method", "kmeans");
  21. if ( cluster_method == "kmeans" )
  22. {
  23. clualg = new OBJREC::KMeans ( conf );
  24. }
  25. else if ( cluster_method == "spectral" )
  26. {
  27. clualg = new SpectralCluster ( conf );
  28. }
  29. else if ( cluster_method == "kmeans_matlab" )
  30. {
  31. clualg = new KMeansMatlab ( conf );
  32. }
  33. else
  34. {
  35. fprintf (stderr, "FATAL ERROR GSCluster: cluster method %s unknown !\n", cluster_method.c_str());
  36. exit(-1);
  37. }
  38. }
  39. GSCluster::~GSCluster()
  40. {
  41. delete clualg;
  42. }
  43. void GSCluster::signature ( const VVector & features,
  44. NICE::Vector & signature )
  45. {
  46. std::vector<int> assignments;
  47. std::vector<double> weights;
  48. VVector prototypes;
  49. if ( (int)features.size() <= c_no_clusters )
  50. {
  51. prototypes = features;
  52. for ( int k = 0 ; k < (int)features.size(); k++ )
  53. weights.push_back( 1.0/(double)features.size() );
  54. } else {
  55. clualg->cluster ( features, prototypes, weights, assignments );
  56. }
  57. assert ( prototypes.size() <= 0 );
  58. int k = 0;
  59. signature.resize(prototypes.size()*(prototypes.begin()->size()+1));
  60. int index = 0;
  61. for ( VVector::const_iterator i = prototypes.begin();
  62. i != prototypes.end();
  63. i++, k++ )
  64. {
  65. // refactor-nice.pl: check this substitution
  66. // old: const Vector & p = *i;
  67. const NICE::Vector & p = *i;
  68. // refactor-nice.pl: check this substitution
  69. // old: signature.Append ( weights[k] );
  70. // refactor-nice.pl: check this substitution
  71. // old: signature.Append ( p );
  72. signature[index++] = weights[k];
  73. for ( int j = 0 ; j < (int)p.size() ; j++,index++ )
  74. signature[index] = p[j];
  75. }
  76. }