LFColorSande.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * @file LFColorSande.cpp
  3. * @brief interface to ColorSande implementation
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 11/19/2007
  6. */
  7. // STL includes
  8. #include <errno.h>
  9. #include <iostream>
  10. #include <sstream>
  11. // nice-core includes
  12. #include <core/basics/StringTools.h>
  13. #include <core/basics/FileMgt.h>
  14. //
  15. #include <core/image/Convert.h>
  16. // nice-vislearning includes
  17. #include <vislearning/baselib/Globals.h>
  18. //
  19. #include "LFColorSande.h"
  20. #define DESCSIZE_DUMMY "/home/bachi/HiWi/nice/nice/objrec-froehlichexp/progs/IMG_8762.JPG"
  21. using namespace OBJREC;
  22. using namespace std;
  23. using namespace NICE;
  24. ///////////////////// ///////////////////// /////////////////////
  25. // CONSTRUCTORS / DESTRUCTORS
  26. ///////////////////// ///////////////////// /////////////////
  27. LFColorSande::LFColorSande() : LocalFeatureRepresentation ()
  28. {
  29. this->c_binaryExecutable = "";
  30. this->c_params = "--descriptor opponentsift";
  31. this->scales = "1+1.5+3.0+4.5+6";
  32. this->descriptor_size = -1;
  33. this->usegrid = false;
  34. int gridsizeInt = 5;
  35. std::ostringstream temp;
  36. temp << gridsizeInt;
  37. this->gridsize = temp.str();
  38. }
  39. LFColorSande::LFColorSande ( const NICE::Config * _conf, std::string _confSection )
  40. {
  41. this->initFromConfig( _conf );
  42. }
  43. LFColorSande::~LFColorSande()
  44. {
  45. }
  46. void LFColorSande::initFromConfig(const NICE::Config * _conf, const std::string & _confSection)
  47. {
  48. try
  49. {
  50. //a possible location on dbv could be: "/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor" );
  51. this->c_binaryExecutable = _conf->gS ( _confSection, "binaryExecutable" /*we do not add a default here, this has to adapted to your system!!!*/);
  52. }
  53. catch (NICE::Exception exception )
  54. {
  55. 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;
  56. throw exception;
  57. }
  58. this->c_params = _conf->gS ( _confSection, "params", "--descriptor opponentsift" );
  59. this->scales = _conf->gS ( _confSection, "scales", "1+1.5+3.0+4.5+6" );
  60. this->descriptor_size = _conf->gI ( _confSection, "descriptor_size", -1 );
  61. this->usegrid = _conf->gB ( _confSection, "usegrid", false );
  62. int gridsizeInt = _conf->gI ( _confSection, "grid", 5 );
  63. std::ostringstream temp;
  64. temp << gridsizeInt;
  65. this->gridsize = temp.str();
  66. if ( descriptor_size <= 0 )
  67. {
  68. fprintf ( stderr, "LFColorSande::LFColorSande: No descriptor size found in config -> self test\n" );
  69. /** get feature dimension **/
  70. NICE::Image testimg ( DESCSIZE_DUMMY );
  71. NICE::VVector features;
  72. NICE::VVector positions;
  73. this->extractFeatures ( testimg, features, positions );
  74. if ( features.size() <= 0 )
  75. fthrow ( Exception, "No features found in " << DESCSIZE_DUMMY << " picture." );
  76. this->descriptor_size = features[0].size();
  77. fprintf ( stderr, "LFColorSande::LFColorSande Self Test features:%d dimension:%d\n", ( int ) features.size(), descriptor_size );
  78. }
  79. if ( descriptor_size != _conf->gI ( "features", "descriptor_size", descriptor_size ) )
  80. {
  81. cerr << "Warning: LFColorSande: descriptor sizes do not match !!!" << endl;
  82. }
  83. }
  84. ///////////////////// ///////////////////// /////////////////////
  85. // FEATURE STUFF
  86. ///////////////////// ///////////////////// //////////////////
  87. int LFColorSande::getDescSize () const
  88. {
  89. return descriptor_size;
  90. }
  91. int LFColorSande::extractFeatures ( const NICE::Image & img, VVector & features,
  92. VVector & positions ) const
  93. {
  94. cerr << "Warning: LFColorSande is a color local feature implementation, but you are calling the gray-image version of extractFeatures" << endl;
  95. NICE::ColorImage colorimg;
  96. NICE::grayToRGB ( img, &colorimg );
  97. extractFeatures ( colorimg, features, positions );
  98. return 0;
  99. }
  100. int LFColorSande::extractFeatures ( const NICE::ColorImage & img, VVector & features, VVector & positions ) const
  101. {
  102. if ( features.size() != positions.size() )
  103. {
  104. positions.clear();
  105. }
  106. bool delete_imgfile = false;
  107. std::string imgfile = Globals::getCurrentImgFN();
  108. fprintf ( stderr, "imgfile: %s\n", imgfile.c_str() );
  109. if ( ( imgfile.size() <= 0 ) || ( ( !StringTools::regexMatch ( imgfile, ".[Jj][pP][Gg]$" ) )
  110. && ( !StringTools::regexMatch ( imgfile, ".[Pp][Nn][Gg]$" ) ) ) )
  111. {
  112. if ( imgfile.size() <= 0 )
  113. {
  114. imgfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
  115. fprintf ( stderr, "LFColorSande: write image to %s (write image)\n", imgfile.c_str() );
  116. ImageFile imgf ( imgfile );
  117. imgf.writer ( &img );
  118. } else {
  119. std::string tmpfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
  120. fprintf ( stderr, "LFColorSande: write image to %s (convert)\n", tmpfile.c_str() );
  121. std::string convertcall = "convert " + imgfile + " " + tmpfile;
  122. cerr << "convert call: " << convertcall << endl;
  123. system ( convertcall.c_str() );
  124. imgfile = tmpfile;
  125. }
  126. delete_imgfile = true;
  127. }
  128. std::string outputfn = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_output_%s" ) ;
  129. std::string gridparams = "";
  130. if ( usegrid )
  131. gridparams = " --detector densesampling --ds_spacing " + gridsize + " --ds_scales " + scales;
  132. std::string call = c_binaryExecutable + " " +
  133. imgfile + " " +
  134. c_params +
  135. gridparams +
  136. " -output " + outputfn;
  137. cerr << "LFColorSande: parameters: <" << c_params + gridparams << ">" << endl;
  138. clog << "Systemcall: " << call << endl;
  139. const int buffersize = 65536;
  140. char *buffer = new char [buffersize];
  141. FILE *f = popen ( call.c_str(), "r" );
  142. if ( f == NULL )
  143. {
  144. fthrow ( Exception, "Unable to run the implementation of van de Sande: " << call << endl << strerror ( errno ) );
  145. }
  146. while ( ! feof ( f ) )
  147. {
  148. if ( fgets ( buffer, buffersize, f ) <= 0 )
  149. {
  150. break;
  151. } else {
  152. fprintf ( stderr, "LFColorSande::extractFeatures: [INFO] %s", buffer );
  153. }
  154. }
  155. pclose ( f );
  156. f = fopen ( outputfn.c_str(), "r" );
  157. if ( f == NULL )
  158. {
  159. fthrow ( Exception, "Unable to read output of van de Sande implementation\n" );
  160. }
  161. if ( fgets ( buffer, buffersize, f ) <= 0 )
  162. {
  163. fprintf ( stderr, "LFColorSande::extractFeatures: output is empty !\n" );
  164. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  165. fprintf ( stderr, "call %s\n", call.c_str() );
  166. exit ( -1 );
  167. }
  168. if ( ! strcmp ( buffer, "KOEN1" ) )
  169. {
  170. fprintf ( stderr, "LFColorSande::extractFeatures: wrong file format\n" );
  171. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  172. fprintf ( stderr, "call %s\n", call.c_str() );
  173. exit ( -1 );
  174. }
  175. fgets ( buffer, buffersize, f );
  176. int dimension = atoi ( buffer );
  177. fprintf ( stderr, "LFColorSande: descriptor dimension = %d\n", dimension );
  178. if ( ( descriptor_size > 0 ) && ( dimension != descriptor_size ) )
  179. {
  180. fprintf ( stderr, "LFColorSande::extractFeatures: dimensions do not match %d -> %d!\n",
  181. dimension, descriptor_size );
  182. fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
  183. fprintf ( stderr, "call %s\n", call.c_str() );
  184. fprintf ( stderr, "dimension std::string buffer: \"%s\"", buffer );
  185. exit ( -1 );
  186. }
  187. fgets ( buffer, buffersize, f );
  188. int noDesc = atoi ( buffer );
  189. fprintf ( stderr, "LFColorSande::extractFeatures: no. of descriptors = %d\n", noDesc );
  190. NICE::Vector x;
  191. while ( ! feof ( f ) )
  192. {
  193. // <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
  194. //<CIRCLE x y scale orientation cornerness>
  195. if ( fgets ( buffer, buffersize, f ) == NULL )
  196. break;
  197. const char *buffer_data = strchr ( buffer, ';' );
  198. if ( strlen ( buffer_data ) <= 0 )
  199. {
  200. fprintf ( stderr, "LFColorSande::extractFeatures: it seems you forget to specify a descriptor\n" );
  201. exit ( -1 );
  202. }
  203. const char *buffer_key_start = strchr ( buffer, ' ' );
  204. const char *buffer_key_end = strchr ( buffer, '>' );
  205. buffer_key_start++;
  206. int keylen = buffer_key_end - buffer_key_start;
  207. char *key = new char [keylen+1];
  208. strncpy ( key, buffer_key_start, keylen );
  209. key[keylen] = '\0';
  210. buffer_data += 2;
  211. std::string buffer_data_s ( buffer_data );
  212. //clog << "[log] buffer = " << buffer_data_s << endl;
  213. StringTools::splitVector ( buffer_data_s, ' ', x );
  214. if ( ( int ) x.size() != dimension )
  215. {
  216. cerr << "Line = " << buffer_data_s << endl;
  217. cerr << "Vector = " << x << endl;
  218. cerr << "dimension = " << dimension << endl;
  219. cerr << "x.size() = " << x.size() << endl;
  220. fprintf ( stderr, "LFColorSande::extractFeatures: error parsing output !!\n" );
  221. exit ( -1 );
  222. } else {
  223. NICE::Vector pos;
  224. StringTools::splitVector ( key, ' ', pos );
  225. if ( pos.size() != 5 ) {
  226. fprintf ( stderr, "LFColorSande::extractFeatures: dimension mismatch (position information)\n" );
  227. exit ( -1 );
  228. }
  229. // Van De Sande verwendet MatLab-Darstellung der Koordinaten. Diese fangen bei 1 an und nicht bei 0.
  230. pos[0] --;
  231. pos[1] --;
  232. positions.push_back ( pos );
  233. features.push_back ( x );
  234. }
  235. }
  236. fclose ( f );
  237. if ( delete_imgfile )
  238. FileMgt::deleteTempFile ( imgfile );
  239. FileMgt::deleteTempFile ( outputfn );
  240. delete [] buffer;
  241. return 0;
  242. }
  243. ///////////////////// INTERFACE PERSISTENT /////////////////////
  244. // interface specific methods for store and restore
  245. ///////////////////// INTERFACE PERSISTENT /////////////////////
  246. void LFColorSande::restore ( std::istream & is, int format )
  247. {
  248. //delete everything we knew so far...
  249. this->clear();
  250. if ( is.good() )
  251. {
  252. std::string tmp;
  253. is >> tmp; //class name
  254. if ( ! this->isStartTag( tmp, "LFColorSande" ) )
  255. {
  256. std::cerr << " WARNING - attempt to restore LFColorSande, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  257. throw;
  258. }
  259. bool b_endOfBlock ( false ) ;
  260. while ( !b_endOfBlock )
  261. {
  262. is >> tmp; // start of block
  263. if ( this->isEndTag( tmp, "LFColorSande" ) )
  264. {
  265. b_endOfBlock = true;
  266. continue;
  267. }
  268. tmp = this->removeStartTag ( tmp );
  269. if ( tmp.compare("c_binaryExecutable") == 0 )
  270. {
  271. is >> this->c_binaryExecutable;
  272. is >> tmp; // end of block
  273. tmp = this->removeEndTag ( tmp );
  274. }
  275. else if ( tmp.compare("c_params") == 0 )
  276. {
  277. is >> this->c_params;
  278. is >> tmp; // end of block
  279. tmp = this->removeEndTag ( tmp );
  280. }
  281. else if ( tmp.compare("scales") == 0 )
  282. {
  283. is >> this->scales;
  284. is >> tmp; // end of block
  285. tmp = this->removeEndTag ( tmp );
  286. }
  287. else if ( tmp.compare("descriptor_size") == 0 )
  288. {
  289. is >> this->descriptor_size;
  290. is >> tmp; // end of block
  291. tmp = this->removeEndTag ( tmp );
  292. }
  293. else if ( tmp.compare("usegrid") == 0 )
  294. {
  295. is >> this->usegrid;
  296. is >> tmp; // end of block
  297. tmp = this->removeEndTag ( tmp );
  298. }
  299. else if ( tmp.compare("gridsize") == 0 )
  300. {
  301. is >> this->gridsize;
  302. is >> tmp; // end of block
  303. tmp = this->removeEndTag ( tmp );
  304. }
  305. else
  306. {
  307. std::cerr << "WARNING -- unexpected LFColorSande object -- " << tmp << " -- for restoration... aborting" << std::endl;
  308. throw;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. std::cerr << "LFColorSande::restore -- InStream not initialized - restoring not possible!" << std::endl;
  315. throw;
  316. }
  317. }
  318. void LFColorSande::store ( std::ostream & os, int format ) const
  319. {
  320. if (os.good())
  321. {
  322. // show starting point
  323. os << this->createStartTag( "LFColorSande" ) << std::endl;
  324. os << this->createStartTag( "c_binaryExecutable" ) << std::endl;
  325. os << this->c_binaryExecutable << std::endl;
  326. os << this->createEndTag( "c_binaryExecutable" ) << std::endl;
  327. os << this->createStartTag( "c_params" ) << std::endl;
  328. os << this->c_params << std::endl;
  329. os << this->createEndTag( "c_params" ) << std::endl;
  330. os << this->createStartTag( "scales" ) << std::endl;
  331. os << this->scales << std::endl;
  332. os << this->createEndTag( "scales" ) << std::endl;
  333. os << this->createStartTag( "descriptor_size" ) << std::endl;
  334. os << this->descriptor_size << std::endl;
  335. os << this->createEndTag( "descriptor_size" ) << std::endl;
  336. os << this->createStartTag( "usegrid" ) << std::endl;
  337. os << this->usegrid << std::endl;
  338. os << this->createEndTag( "usegrid" ) << std::endl;
  339. os << this->createStartTag( "gridsize" ) << std::endl;
  340. os << this->gridsize << std::endl;
  341. os << this->createEndTag( "gridsize" ) << std::endl;
  342. // done
  343. os << this->createEndTag( "LFColorSande" ) << std::endl;
  344. }
  345. else
  346. {
  347. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  348. }
  349. }
  350. void LFColorSande::clear ()
  351. {
  352. }