LFColorSande.cpp 8.6 KB

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