LFMikolajczyk.cpp 5.1 KB

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