LFColorSande.cpp 8.1 KB

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