LFColorSande.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @file LFColorSande.cpp
  3. * @brief interface to ColorSande implementation
  4. * @author Erik Rodner
  5. * @date 11/19/2007
  6. */
  7. #include <errno.h>
  8. #include <iostream>
  9. #include <core/image/Convert.h>
  10. #include "core/basics/StringTools.h"
  11. #include "core/basics/FileMgt.h"
  12. #include "vislearning/baselib/Globals.h"
  13. #include "vislearning/features/localfeatures/LFColorSande.h"
  14. #define DESCSIZE_DUMMY "/home/bachi/HiWi/nice/nice/objrec-froehlichexp/progs/IMG_8762.JPG"
  15. #include <sstream>
  16. using namespace OBJREC;
  17. using namespace std;
  18. using namespace NICE;
  19. LFColorSande::LFColorSande ( const Config *conf, std::string section )
  20. {
  21. c_binaryExecutable = conf->gS ( section, "binaryExecutable", "/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor" );
  22. c_params = conf->gS ( section, "params", "--descriptor opponentsift" );
  23. scales = conf->gS ( section, "scales", "1+1.5+3.0+4.5+6" );
  24. descriptor_size = conf->gI ( section, "descriptor_size", -1 );
  25. usegrid = conf->gB ( section, "usegrid", false );
  26. int g = conf->gI ( section, "grid", 5 );
  27. std::ostringstream temp;
  28. temp << g;
  29. gridsize = temp.str();
  30. if ( descriptor_size <= 0 )
  31. {
  32. fprintf ( stderr, "LFColorSande::LFColorSande: No descriptor size found in config -> self test\n" );
  33. /** get feature dimension **/
  34. NICE::Image testimg ( DESCSIZE_DUMMY );
  35. VVector features;
  36. VVector positions;
  37. extractFeatures ( testimg, features, positions );
  38. if ( features.size() <= 0 )
  39. fthrow ( Exception, "No features found in " << DESCSIZE_DUMMY << " picture." );
  40. descriptor_size = features[0].size();
  41. fprintf ( stderr, "LFColorSande::LFColorSande Self Test features:%d dimension:%d\n", ( int ) features.size(), descriptor_size );
  42. }
  43. if ( descriptor_size != conf->gI ( "features", "descriptor_size", descriptor_size ) )
  44. {
  45. cerr << "Warning: LFColorSande: descriptor sizes do not match !!!" << endl;
  46. }
  47. }
  48. LFColorSande::~LFColorSande()
  49. {
  50. }
  51. int LFColorSande::getDescSize () const
  52. {
  53. return descriptor_size;
  54. }
  55. int LFColorSande::extractFeatures ( const NICE::Image & img, VVector & features,
  56. VVector & positions ) const
  57. {
  58. cerr << "Warning: LFColorSande is a color local feature implementation, but you are calling the gray-image version of extractFeatures" << endl;
  59. NICE::ColorImage colorimg;
  60. NICE::grayToRGB ( img, &colorimg );
  61. extractFeatures ( colorimg, features, positions );
  62. return 0;
  63. }
  64. int LFColorSande::extractFeatures ( const NICE::ColorImage & img, VVector & features, VVector & positions ) const
  65. {
  66. if ( features.size() != positions.size() )
  67. {
  68. positions.clear();
  69. }
  70. bool delete_imgfile = false;
  71. std::string imgfile = Globals::getCurrentImgFN();
  72. fprintf ( stderr, "imgfile: %s\n", imgfile.c_str() );
  73. if ( ( imgfile.size() <= 0 ) || ( ( !StringTools::regexMatch ( imgfile, ".[Jj][pP][Gg]$" ) )
  74. && ( !StringTools::regexMatch ( imgfile, ".[Pp][Nn][Gg]$" ) ) ) )
  75. {
  76. if ( imgfile.size() <= 0 )
  77. {
  78. imgfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
  79. fprintf ( stderr, "LFColorSande: write image to %s (write image)\n", imgfile.c_str() );
  80. ImageFile imgf ( imgfile );
  81. imgf.writer ( &img );
  82. } else {
  83. std::string tmpfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
  84. fprintf ( stderr, "LFColorSande: write image to %s (convert)\n", tmpfile.c_str() );
  85. std::string convertcall = "convert " + imgfile + " " + tmpfile;
  86. cerr << "convert call: " << convertcall << endl;
  87. system ( convertcall.c_str() );
  88. imgfile = tmpfile;
  89. }
  90. delete_imgfile = true;
  91. }
  92. std::string outputfn = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_output_%s" ) ;
  93. std::string gridparams = "";
  94. if ( usegrid )
  95. gridparams = " --detector densesampling --ds_spacing " + gridsize + " --ds_scales " + scales;
  96. std::string call = c_binaryExecutable + " " +
  97. imgfile + " " +
  98. c_params +
  99. gridparams +
  100. " -output " + outputfn;
  101. cerr << "LFColorSande: parameters: <" << c_params + gridparams << ">" << endl;
  102. clog << "Systemcall: " << call << endl;
  103. const int buffersize = 65536;
  104. char *buffer = new char [buffersize];
  105. FILE *f = popen ( call.c_str(), "r" );
  106. if ( f == NULL )
  107. {
  108. fthrow ( Exception, "Unable to run the implementation of van de Sande: " << call << endl << strerror ( errno ) );
  109. }
  110. while ( ! feof ( f ) )
  111. {
  112. if ( fgets ( buffer, buffersize, f ) <= 0 )
  113. {
  114. break;
  115. } else {
  116. fprintf ( stderr, "LFColorSande::extractFeatures: [INFO] %s", buffer );
  117. }
  118. }
  119. pclose ( f );
  120. f = fopen ( outputfn.c_str(), "r" );
  121. if ( f == NULL )
  122. {
  123. fthrow ( Exception, "Unable to read output of van de Sande implementation\n" );
  124. }
  125. if ( fgets ( buffer, buffersize, f ) <= 0 )
  126. {
  127. fprintf ( stderr, "LFColorSande::extractFeatures: output is empty !\n" );
  128. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  129. fprintf ( stderr, "call %s\n", call.c_str() );
  130. exit ( -1 );
  131. }
  132. if ( ! strcmp ( buffer, "KOEN1" ) )
  133. {
  134. fprintf ( stderr, "LFColorSande::extractFeatures: wrong file format\n" );
  135. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  136. fprintf ( stderr, "call %s\n", call.c_str() );
  137. exit ( -1 );
  138. }
  139. fgets ( buffer, buffersize, f );
  140. int dimension = atoi ( buffer );
  141. fprintf ( stderr, "LFColorSande: descriptor dimension = %d\n", dimension );
  142. if ( ( descriptor_size > 0 ) && ( dimension != descriptor_size ) )
  143. {
  144. fprintf ( stderr, "LFColorSande::extractFeatures: dimensions do not match %d -> %d!\n",
  145. dimension, descriptor_size );
  146. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  147. fprintf ( stderr, "call %s\n", call.c_str() );
  148. fprintf ( stderr, "dimension std::string buffer: \"%s\"", buffer );
  149. exit ( -1 );
  150. }
  151. fgets ( buffer, buffersize, f );
  152. int noDesc = atoi ( buffer );
  153. fprintf ( stderr, "LFColorSande::extractFeatures: no. of descriptors = %d\n", noDesc );
  154. NICE::Vector x;
  155. while ( ! feof ( f ) )
  156. {
  157. // <CIRCLE 119 307 1.26134 0 0.00014763>; 0 0 6 2 0 6 25 7 9 4 4 0 0 4 20 36 78 4 5 0 0
  158. if ( fgets ( buffer, buffersize, f ) == NULL )
  159. break;
  160. const char *buffer_data = strchr ( buffer, ';' );
  161. if ( strlen ( buffer_data ) <= 0 )
  162. {
  163. fprintf ( stderr, "LFColorSande::extractFeatures: it seems you forget to specify a descriptor\n" );
  164. exit ( -1 );
  165. }
  166. const char *buffer_key_start = strchr ( buffer, ' ' );
  167. const char *buffer_key_end = strchr ( buffer, '>' );
  168. buffer_key_start++;
  169. int keylen = buffer_key_end - buffer_key_start;
  170. char *key = new char [keylen+1];
  171. strncpy ( key, buffer_key_start, keylen );
  172. key[keylen] = '\0';
  173. buffer_data += 2;
  174. std::string buffer_data_s ( buffer_data );
  175. //clog << "[log] buffer = " << buffer_data_s << endl;
  176. StringTools::splitVector ( buffer_data_s, ' ', x );
  177. if ( ( int ) x.size() != dimension )
  178. {
  179. cerr << "Line = " << buffer_data_s << endl;
  180. cerr << "Vector = " << x << endl;
  181. cerr << "dimension = " << dimension << endl;
  182. cerr << "x.size() = " << x.size() << endl;
  183. fprintf ( stderr, "LFColorSande::extractFeatures: error parsing output !!\n" );
  184. exit ( -1 );
  185. } else {
  186. NICE::Vector pos;
  187. StringTools::splitVector ( key, ' ', pos );
  188. if ( pos.size() != 5 ) {
  189. fprintf ( stderr, "LFColorSande::extractFeatures: dimension mismatch (position information)\n" );
  190. exit ( -1 );
  191. }
  192. // Van De Sande verwendet MatLab-Darstellung der Koordinaten. Diese fangen bei 1 an und nicht bei 0.
  193. pos[0] --;
  194. pos[1] --;
  195. positions.push_back ( pos );
  196. features.push_back ( x );
  197. }
  198. }
  199. fclose ( f );
  200. if ( delete_imgfile )
  201. FileMgt::deleteTempFile ( imgfile );
  202. FileMgt::deleteTempFile ( outputfn );
  203. delete [] buffer;
  204. return 0;
  205. }