LFMikolajczyk.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file LFMikolajczyk.cpp
  3. * @brief interface to Mikolajczyk implementation
  4. * @author Erik Rodner
  5. * @date 11/19/2007
  6. */
  7. #include <iostream>
  8. #include "core/basics/StringTools.h"
  9. #include "core/basics/FileMgt.h"
  10. #include "vislearning/features/localfeatures/LFMikolajczyk.h"
  11. using namespace OBJREC;
  12. // refactor-nice.pl: check this substitution
  13. #define DESCSIZE_DUMMY "/home/dbv/bilder/PicDatabase/ImageData2/monica.1.pgm"
  14. using namespace std;
  15. using namespace NICE;
  16. LFMikolajczyk::LFMikolajczyk( const Config *conf )
  17. {
  18. c_binaryExecutable = conf->gS( "LFMikolajczyk", "binaryExecutable", "/home/rodner/script/extract_features_64bit.ln" );
  19. c_params = conf->gS("LFMikolajczyk", "params", "-haraff -sift");
  20. c_minScale = conf->gD("LFMikolajczyk", "min_scale", 1.0);
  21. descriptor_size = conf->gI("LFMikolajczyk", "descriptor_size", -1 );
  22. if ( descriptor_size <= 0 )
  23. {
  24. fprintf (stderr, "LFMikolajczyk::LFMikolajczyk: No descriptor size found in config -> self test\n");
  25. /** get feature dimension **/
  26. NICE::Image testimg (DESCSIZE_DUMMY);
  27. VVector features;
  28. VVector positions;
  29. extractFeatures ( testimg, features, positions );
  30. if ( features.size() <= 0 )
  31. {
  32. fprintf (stderr, "LFMikolajczyk::LFMikolajczyk: No features found in descriptor_size_DUMMY picture !!\n");
  33. exit(-1);
  34. }
  35. descriptor_size = features[0].size();
  36. fprintf (stderr, "LFMikolajczyk::LFMikolajczyk Self Test features:%d dimension:%d\n", (int)features.size(), descriptor_size );
  37. }
  38. if ( descriptor_size != conf->gI("features", "descriptor_size", 128) )
  39. {
  40. fprintf (stderr, "FATAL ERROR LFMikolajczyk: descriptor sizes do not match !!!\n");
  41. exit(-1);
  42. }
  43. }
  44. LFMikolajczyk::~LFMikolajczyk()
  45. {
  46. }
  47. int LFMikolajczyk::getDescSize () const
  48. {
  49. return descriptor_size;
  50. }
  51. int LFMikolajczyk::extractFeatures ( const NICE::Image & img, VVector & features,
  52. VVector & positions ) const
  53. {
  54. FILE *f;
  55. // refactor-nice.pl: check this substitution
  56. // old: string pgmfile = FileMgt::createTempFile ( "/tmp/osl_lfmikolajczyk_input_%s.pgm" );
  57. std::string pgmfile = FileMgt::createTempFile ( "/tmp/osl_lfmikolajczyk_input_%s.pgm" );
  58. // refactor-nice.pl: check this substitution
  59. // old: string outputfn = FileMgt::createTempFile ( "/tmp/osl_lfmikolajczyk_output_%s" ) ;
  60. std::string outputfn = FileMgt::createTempFile ( "/tmp/osl_lfmikolajczyk_output_%s" ) ;
  61. // refactor-nice.pl: check this substitution
  62. // old: string call = c_binaryExecutable + " " +
  63. std::string call = c_binaryExecutable + " " +
  64. c_params +
  65. " -i " + pgmfile +
  66. " -o1 " + outputfn +
  67. " 3>&1 2>&3 1>/dev/null";
  68. // refactor-nice.pl: check this substitution
  69. // old: WriteImg ( img, pgmfile );
  70. img.write (pgmfile);
  71. f = popen ( call.c_str(), "r" );
  72. if ( f == NULL )
  73. {
  74. fprintf (stderr, "LFMikolajczyk::extractFeatures: FATAL ERROR, unable to run the implementation of Mikolajczyk\n");
  75. exit(-1);
  76. }
  77. pclose(f);
  78. f = fopen ( outputfn.c_str(), "r" );
  79. if ( f == NULL )
  80. {
  81. fprintf (stderr, "LFMikolajczyk::extractFeatures: FATAL ERROR, unable to read output of Mikolajczyks implementation\n");
  82. exit(-1);
  83. }
  84. const int buffersize = 4048;
  85. char *buffer = new char [buffersize];
  86. fgets ( buffer, buffersize, f );
  87. int dimension = atoi (buffer);
  88. if ( (descriptor_size >= 0) && (dimension != descriptor_size) )
  89. {
  90. fprintf (stderr, "LFMikolajczyk::extractFeatures: FATAL ERROR dimension != descriptor_size\n");
  91. exit(-1);
  92. }
  93. fgets ( buffer, buffersize, f );
  94. int noDesc = atoi (buffer);
  95. fprintf (stderr, "LFMikolajczyk::extractFeatures: no. of descriptors = %d\n", noDesc );
  96. // refactor-nice.pl: check this substitution
  97. // old: Vector x;
  98. NICE::Vector x;
  99. while ( ! feof(f) )
  100. {
  101. if ( fgets(buffer, buffersize, f) == NULL )
  102. break;
  103. // refactor-nice.pl: check this substitution
  104. // old: string buffer_s (buffer);
  105. std::string buffer_s (buffer);
  106. StringTools::splitVector ( buffer_s, ' ', x );
  107. if ( (int)x.size() != dimension + 5 )
  108. {
  109. cerr << "Line = " << buffer_s << endl;
  110. cerr << "Vector = " << x << endl;
  111. cerr << "dimension = " << dimension << endl;
  112. cerr << "x.size() = " << x.size() << endl;
  113. fprintf (stderr, "LFMikolajczyk::extractFeatures: error parsing output !!\n");
  114. exit(-1);
  115. } else {
  116. double area = 2*M_PI/sqrt(x[2]*x[4] - x[3]*x[3]);
  117. area /= 36.0; // be comparable to SiftPP
  118. if ( area < c_minScale ) {
  119. continue;
  120. }
  121. // refactor-nice.pl: check this substitution
  122. // old: Vector pos ( x[0], // x coordinate
  123. NICE::Vector pos ( 5 );
  124. pos[0] = x[0]; // x coordinate
  125. pos[1] = x[1]; // y coordinate
  126. pos[2] = x[2]; // a
  127. pos[3] = x[3]; // b
  128. pos[4] = x[4]; // c
  129. // where a(x-u)(x-u)+2b(x-u)(y-v)+c(y-v)(y-v)=1
  130. positions.push_back( pos );
  131. features.push_back ( x.getRange(5, x.size()-1) );
  132. }
  133. }
  134. fclose (f);
  135. FileMgt::deleteTempFile ( pgmfile );
  136. FileMgt::deleteTempFile ( outputfn + ".params" );
  137. FileMgt::deleteTempFile ( outputfn );
  138. delete [] buffer;
  139. return 0;
  140. }
  141. ///////////////////// INTERFACE PERSISTENT /////////////////////
  142. // interface specific methods for store and restore
  143. ///////////////////// INTERFACE PERSISTENT /////////////////////
  144. void LFMikolajczyk::restore ( std::istream & is, int format )
  145. {
  146. fthrow ( Exception, "LFMikolajczyk::restore not implemented yet." );
  147. //TODO
  148. }
  149. void LFMikolajczyk::store ( std::ostream & os, int format ) const
  150. {
  151. fthrow ( Exception, "LFMikolajczyk::store not implemented yet." );
  152. //TODO
  153. }
  154. void LFMikolajczyk::clear ()
  155. {
  156. fthrow ( Exception, "LFMikolajczyk::clear not implemented yet." );
  157. //TODO
  158. }