FeatureFactory.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file FeatureFactory.cpp
  3. * @brief Abstract class for image classification
  4. * @author Erik Rodner
  5. * @date 07.09.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 "vislearning/features/fbase/FeatureFactory.h"
  14. #include "vislearning/baselib/ProgressBar.h"
  15. #include "vislearning/baselib/Preprocess.h"
  16. #include "vislearning/baselib/Globals.h"
  17. using namespace OBJREC;
  18. using namespace std;
  19. using namespace NICE;
  20. FeatureFactory::FeatureFactory( const Config *_conf )
  21. {
  22. Preprocess::Init ( _conf );
  23. }
  24. int FeatureFactory::convert ( const NICE::Image & img, NICE::Vector & vec )
  25. {
  26. fprintf (stderr, "FeatureFactory::convert: not yet implemented !\n");
  27. exit(-1);
  28. }
  29. int FeatureFactory::convertRGB ( const NICE::ColorImage & img, NICE::Vector & vec )
  30. {
  31. NICE::Vector tmp,vecr,vecg,vecb;
  32. NICE::Image *r = img.getChannel (0);
  33. NICE::Image *g = img.getChannel (1);
  34. NICE::Image *b = img.getChannel (2);
  35. //create feature vectors
  36. int ret;
  37. if(convert(*r,vecr)>=0 && convert(*g,vecg)>=0 && convert(*b,vecb)>=0)
  38. {
  39. vec.resize( vecr.size() + vecg.size() + vecb.size() );
  40. for ( unsigned int i = 0 ; i < vecr.size() ; i++ )
  41. vec[i] = vecr[i];
  42. for ( unsigned int i = 0 ; i < vecg.size() ; i++ )
  43. vec[i+vecr.size()] = vecg[i];
  44. for ( unsigned int i = 0 ; i < vecb.size() ; i++ )
  45. vec[i+vecr.size()+vecb.size()] = vecb[i];
  46. ret = 0;
  47. } else {
  48. ret = -1;
  49. }
  50. delete r;
  51. delete g;
  52. delete b;
  53. return ret;
  54. }
  55. FeatureFactory::~FeatureFactory()
  56. {
  57. }
  58. int FeatureFactory::convertSet ( const LabeledSet *filelist,
  59. LabeledSetVector & storage )
  60. {
  61. ProgressBar pb ("Feature Conversion");
  62. pb.show();
  63. LOOP_ALL_S(*filelist)
  64. {
  65. EACH_S(classno,fn);
  66. pb.update ( filelist->count() );
  67. NICE::Image img = Preprocess::ReadImgAdv ( fn );
  68. Globals::setCurrentImgFN(fn);
  69. NICE::Vector x;
  70. if ( convert ( img, x ) < 0 )
  71. {
  72. fprintf (stderr, "FeatureFactory: error building feature vector: %s !\n", fn.c_str() );
  73. continue;
  74. }
  75. storage.add (classno,x);
  76. }
  77. return 0;
  78. }
  79. int FeatureFactory::convertSetRGB ( const LabeledSet *filelist,
  80. LabeledSetVector & storage )
  81. {
  82. LOOP_ALL_S(*filelist)
  83. {
  84. EACH_S(classno,fn);
  85. NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( fn );
  86. Globals::setCurrentImgFN(fn);
  87. NICE::Vector x;
  88. if ( convertRGB ( img, x ) < 0 )
  89. {
  90. fprintf (stderr, "FeatureFactoy: error building feature vector: %s !\n", fn.c_str() );
  91. continue;
  92. }
  93. storage.add (classno,x);
  94. }
  95. return 0;
  96. }