123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /**
- * @file LFColorSande.cpp
- * @brief interface to ColorSande implementation
- * @author Erik Rodner
- * @date 11/19/2007
- */
- #include <errno.h>
- #include <iostream>
- #include <core/image/Convert.h>
- #include "core/basics/StringTools.h"
- #include "core/basics/FileMgt.h"
- #include "vislearning/baselib/Globals.h"
- #include "vislearning/features/localfeatures/LFColorSande.h"
- #define DESCSIZE_DUMMY "/home/bachi/HiWi/nice/nice/objrec-froehlichexp/progs/IMG_8762.JPG"
- #include <sstream>
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- LFColorSande::LFColorSande ( const Config *conf, std::string section )
- {
- c_binaryExecutable = conf->gS ( section, "binaryExecutable", "/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor" );
- c_params = conf->gS ( section, "params", "--descriptor opponentsift" );
- scales = conf->gS ( section, "scales", "1+1.5+3.0+4.5+6" );
- descriptor_size = conf->gI ( section, "descriptor_size", -1 );
- usegrid = conf->gB ( section, "usegrid", false );
- int g = conf->gI ( section, "grid", 5 );
- std::ostringstream temp;
- temp << g;
- gridsize = temp.str();
- if ( descriptor_size <= 0 )
- {
- fprintf ( stderr, "LFColorSande::LFColorSande: No descriptor size found in config -> self test\n" );
- /** get feature dimension **/
- NICE::Image testimg ( DESCSIZE_DUMMY );
- VVector features;
- VVector positions;
- extractFeatures ( testimg, features, positions );
- if ( features.size() <= 0 )
- fthrow ( Exception, "No features found in " << DESCSIZE_DUMMY << " picture." );
- descriptor_size = features[0].size();
- fprintf ( stderr, "LFColorSande::LFColorSande Self Test features:%d dimension:%d\n", ( int ) features.size(), descriptor_size );
- }
- if ( descriptor_size != conf->gI ( "features", "descriptor_size", descriptor_size ) )
- {
- cerr << "Warning: LFColorSande: descriptor sizes do not match !!!" << endl;
- }
- }
- LFColorSande::~LFColorSande()
- {
- }
- int LFColorSande::getDescSize () const
- {
- return descriptor_size;
- }
- int LFColorSande::extractFeatures ( const NICE::Image & img, VVector & features,
- VVector & positions ) const
- {
- cerr << "Warning: LFColorSande is a color local feature implementation, but you are calling the gray-image version of extractFeatures" << endl;
- NICE::ColorImage colorimg;
- NICE::grayToRGB ( img, &colorimg );
- extractFeatures ( colorimg, features, positions );
- return 0;
- }
- int LFColorSande::extractFeatures ( const NICE::ColorImage & img, VVector & features, VVector & positions ) const
- {
- if ( features.size() != positions.size() )
- {
- positions.clear();
- }
- bool delete_imgfile = false;
- std::string imgfile = Globals::getCurrentImgFN();
- fprintf ( stderr, "imgfile: %s\n", imgfile.c_str() );
- if ( ( imgfile.size() <= 0 ) || ( ( !StringTools::regexMatch ( imgfile, ".[Jj][pP][Gg]$" ) )
- && ( !StringTools::regexMatch ( imgfile, ".[Pp][Nn][Gg]$" ) ) ) )
- {
- if ( imgfile.size() <= 0 )
- {
- imgfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
- fprintf ( stderr, "LFColorSande: write image to %s (write image)\n", imgfile.c_str() );
- ImageFile imgf ( imgfile );
- imgf.writer ( &img );
- } else {
- std::string tmpfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
- fprintf ( stderr, "LFColorSande: write image to %s (convert)\n", tmpfile.c_str() );
- std::string convertcall = "convert " + imgfile + " " + tmpfile;
- cerr << "convert call: " << convertcall << endl;
- system ( convertcall.c_str() );
- imgfile = tmpfile;
- }
- delete_imgfile = true;
- }
- std::string outputfn = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_output_%s" ) ;
- std::string gridparams = "";
- if ( usegrid )
- gridparams = " --detector densesampling --ds_spacing " + gridsize + " --ds_scales " + scales;
- std::string call = c_binaryExecutable + " " +
- imgfile + " " +
- c_params +
- gridparams +
- " -output " + outputfn;
- cerr << "LFColorSande: parameters: <" << c_params + gridparams << ">" << endl;
- clog << "Systemcall: " << call << endl;
- const int buffersize = 65536;
- char *buffer = new char [buffersize];
- FILE *f = popen ( call.c_str(), "r" );
- if ( f == NULL )
- {
- fthrow ( Exception, "Unable to run the implementation of van de Sande: " << call << endl << strerror ( errno ) );
- }
- while ( ! feof ( f ) )
- {
- if ( fgets ( buffer, buffersize, f ) <= 0 )
- {
- break;
- } else {
- fprintf ( stderr, "LFColorSande::extractFeatures: [INFO] %s", buffer );
- }
- }
- pclose ( f );
- f = fopen ( outputfn.c_str(), "r" );
- if ( f == NULL )
- {
- fthrow ( Exception, "Unable to read output of van de Sande implementation\n" );
- }
- if ( fgets ( buffer, buffersize, f ) <= 0 )
- {
- fprintf ( stderr, "LFColorSande::extractFeatures: output is empty !\n" );
- fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
- fprintf ( stderr, "call %s\n", call.c_str() );
- exit ( -1 );
- }
- if ( ! strcmp ( buffer, "KOEN1" ) )
- {
- fprintf ( stderr, "LFColorSande::extractFeatures: wrong file format\n" );
- fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
- fprintf ( stderr, "call %s\n", call.c_str() );
- exit ( -1 );
- }
- fgets ( buffer, buffersize, f );
- int dimension = atoi ( buffer );
- fprintf ( stderr, "LFColorSande: descriptor dimension = %d\n", dimension );
- if ( ( descriptor_size > 0 ) && ( dimension != descriptor_size ) )
- {
- fprintf ( stderr, "LFColorSande::extractFeatures: dimensions do not match %d -> %d!\n",
- dimension, descriptor_size );
- fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
- fprintf ( stderr, "call %s\n", call.c_str() );
- fprintf ( stderr, "dimension std::string buffer: \"%s\"", buffer );
- exit ( -1 );
- }
- fgets ( buffer, buffersize, f );
- int noDesc = atoi ( buffer );
- fprintf ( stderr, "LFColorSande::extractFeatures: no. of descriptors = %d\n", noDesc );
- NICE::Vector x;
- while ( ! feof ( f ) )
- {
- // <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
- if ( fgets ( buffer, buffersize, f ) == NULL )
- break;
- const char *buffer_data = strchr ( buffer, ';' );
- if ( strlen ( buffer_data ) <= 0 )
- {
- fprintf ( stderr, "LFColorSande::extractFeatures: it seems you forget to specify a descriptor\n" );
- exit ( -1 );
- }
- const char *buffer_key_start = strchr ( buffer, ' ' );
- const char *buffer_key_end = strchr ( buffer, '>' );
- buffer_key_start++;
- int keylen = buffer_key_end - buffer_key_start;
- char *key = new char [keylen+1];
- strncpy ( key, buffer_key_start, keylen );
- key[keylen] = '\0';
- buffer_data += 2;
- std::string buffer_data_s ( buffer_data );
- //clog << "[log] buffer = " << buffer_data_s << endl;
- StringTools::splitVector ( buffer_data_s, ' ', x );
- if ( ( int ) x.size() != dimension )
- {
- cerr << "Line = " << buffer_data_s << endl;
- cerr << "Vector = " << x << endl;
- cerr << "dimension = " << dimension << endl;
- cerr << "x.size() = " << x.size() << endl;
- fprintf ( stderr, "LFColorSande::extractFeatures: error parsing output !!\n" );
- exit ( -1 );
- } else {
- NICE::Vector pos;
- StringTools::splitVector ( key, ' ', pos );
- if ( pos.size() != 5 ) {
- fprintf ( stderr, "LFColorSande::extractFeatures: dimension mismatch (position information)\n" );
- exit ( -1 );
- }
- // Van De Sande verwendet MatLab-Darstellung der Koordinaten. Diese fangen bei 1 an und nicht bei 0.
- pos[0] --;
- pos[1] --;
- positions.push_back ( pos );
- features.push_back ( x );
- }
- }
- fclose ( f );
- if ( delete_imgfile )
- FileMgt::deleteTempFile ( imgfile );
- FileMgt::deleteTempFile ( outputfn );
- delete [] buffer;
- return 0;
- }
|