LFMikolajczyk.cpp 6.1 KB

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