LocalFeatureColorWeijer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // STL includes
  2. #include <fstream>
  3. #include <iostream>
  4. #include <exception>
  5. #include <stdlib.h> /* used for getenv */
  6. // nice-vislearning includes
  7. #include "vislearning/baselib/ColorSpace.h"
  8. //
  9. #include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. //! color representation for visualization
  14. const int colors[11][3] =
  15. {
  16. {0, 0, 0}, // black
  17. {0, 0, 255}, // blue
  18. {165, 42, 42}, // brown
  19. {190, 190, 190}, // grey
  20. { 0, 255, 0}, // green
  21. {255, 165, 0}, // orange
  22. {255, 192, 203}, // pink
  23. {160, 32, 240}, // purple
  24. {255, 0, 0}, // red
  25. {255, 255, 255}, // white
  26. {255, 255, 0}, // yellow
  27. };
  28. ///////////////////// ///////////////////// /////////////////////
  29. // CONSTRUCTORS / DESTRUCTORS
  30. ///////////////////// ///////////////////// /////////////////////
  31. OBJREC::LocalFeatureColorWeijer::LocalFeatureColorWeijer() : LocalFeature ()
  32. {
  33. this->tfile = "";
  34. }
  35. LocalFeatureColorWeijer::LocalFeatureColorWeijer( const NICE::Config *_conf )
  36. {
  37. this->initFromConfig ( _conf );
  38. }
  39. LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
  40. {
  41. }
  42. void LocalFeatureColorWeijer::initFromConfig ( const NICE::Config * _conf, const std::string & _confSection )
  43. {
  44. this->tfile = _conf->gS( "LocalFeatureColorWeijer", "table", "");
  45. this->tfile = _conf->getAbsoluteFilenameRelativeToThisConfig( tfile );
  46. // If no destination to the LUT was given, we try to use the file shipped with this library
  47. // Therefore, we try to catch the NICEHOME and if possible, append the location where the LUT file is stored
  48. // NOTE This will only work if NICEHOME is set in your environment (e.g., by exporting it in your bashrc or sourcing the setenv.sh)
  49. if ( this->tfile.compare("") == 0 )
  50. {
  51. char* pNICEHOME;
  52. pNICEHOME = getenv ("NICEHOME");
  53. if ( pNICEHOME != NULL )
  54. {
  55. this->tfile = std::string ( pNICEHOME );
  56. this->tfile += "/vislearning/features/localfeatures/input/colorWeijer/w2c.txt";
  57. }
  58. }
  59. this->restoreLUT();
  60. }
  61. ///////////////////// ///////////////////// /////////////////////
  62. // FEATURE STUFF
  63. ///////////////////// ///////////////////// //////////////////
  64. int LocalFeatureColorWeijer::getDescSize() const
  65. {
  66. return LASTCOLOR;
  67. }
  68. void LocalFeatureColorWeijer::restoreLUT()
  69. {
  70. ifstream fin( this->tfile.c_str() );
  71. if( !fin.is_open() )
  72. {
  73. fthrow(Exception,"ColorWeijer: could not find lookup table file. Specify the path in the config, e.g., YOURNICEHOME/vislearning/features/localfeatures/input/colorWeijer/w2c.txt");
  74. }
  75. while( !fin.eof() )
  76. {
  77. double rd,gd,bd;
  78. int r,g,b;
  79. fin >> rd;
  80. fin >> gd;
  81. fin >> bd;
  82. r = rd/8;
  83. g = gd/8;
  84. b = bd/8;
  85. // read LUT values for all 11 colornames
  86. for( int i = 0; i < 11; i++ )
  87. {
  88. fin >> hist[r][g][b][i];
  89. }
  90. }
  91. /*
  92. for(int r = 0; r < 32; r++)
  93. {
  94. for(int g = 0; g < 32; g++)
  95. {
  96. for(int b = 0; b < 32; b++)
  97. {
  98. for(int i = 0; i < 11; i++)
  99. {
  100. fin >> hist[r][g][b][i];
  101. }
  102. }
  103. }
  104. } */
  105. }
  106. int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector & positions, VVector & features ) const
  107. {
  108. throw NICE::Exception ( "LocalFeatureColorWeijer extracts COLOR Features, it won't work on gray value images");
  109. }
  110. int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
  111. {
  112. for ( int j = 0; j < ( int )positions.size(); j++ )
  113. {
  114. int x = positions[j][0];
  115. int y = positions[j][1];
  116. int r = img(x,y,0)/8;
  117. int g = img(x,y,1)/8;
  118. int b = img(x,y,2)/8;
  119. NICE::Vector feat( 11 );
  120. for ( uint i = 0; i < 11; i++ )
  121. {
  122. feat[i] = hist[r][g][b][i];
  123. }
  124. features.push_back( feat );
  125. }
  126. return 1;
  127. }
  128. void LocalFeatureColorWeijer::visualizeFeatures( NICE::Image & mark, const VVector & positions, size_t color ) const
  129. {
  130. }
  131. int LocalFeatureColorWeijer::findColor( string &fn )
  132. {
  133. if ( fn.find( "black" ) != string::npos )
  134. return BLACK;
  135. if ( fn.find( "blue" ) != string::npos )
  136. return BLUE;
  137. if ( fn.find( "brown" ) != string::npos )
  138. return BROWN;
  139. if ( fn.find( "grey" ) != string::npos )
  140. return GREY;
  141. if ( fn.find( "green" ) != string::npos )
  142. return GREEN;
  143. if ( fn.find( "orange" ) != string::npos )
  144. return ORANGE;
  145. if ( fn.find( "pink" ) != string::npos )
  146. return PINK;
  147. if ( fn.find( "purple" ) != string::npos )
  148. return PURPLE;
  149. if ( fn.find( "red" ) != string::npos )
  150. return RED;
  151. if ( fn.find( "white" ) != string::npos )
  152. return WHITE;
  153. if ( fn.find( "yellow" ) != string::npos )
  154. return YELLOW;
  155. return -1;
  156. }
  157. void LocalFeatureColorWeijer::visualizeFeatures( NICE::ColorImage & out, const VVector & features, const VVector & position ) const
  158. {
  159. for ( int i = 0; i < ( int )position.size(); i++ )
  160. {
  161. int maxpos = 0;
  162. double maxval = 0.0;
  163. for ( int j = 0; j < ( int )features[i].size(); j++ )
  164. {
  165. if ( maxval < features[i][j] )
  166. {
  167. maxval = features[i][j];
  168. maxpos = j;
  169. }
  170. }
  171. out.setPixel( position[i][0], position[i][1], colors[maxpos][0], colors[maxpos][1], colors[maxpos][2] );
  172. }
  173. }
  174. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg ) const
  175. {
  176. NICE::ColorImage out;
  177. this->visualizeFeatures( cimg, out );
  178. }
  179. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg, NICE::ColorImage &out ) const
  180. {
  181. NICE::VVector pos, feats;
  182. for ( int y = 0; y < cimg.height(); y++ )
  183. {
  184. for ( int x = 0; x < cimg.width(); x++ )
  185. {
  186. Vector vec( 2 );
  187. vec[0] = x;
  188. vec[1] = y;
  189. pos.push_back( vec );
  190. }
  191. }
  192. this->getDescriptors( cimg, pos, feats );
  193. //heatmap for a special class
  194. /*ofstream fout("out.txt");
  195. int counter = 0;
  196. for ( int y = 0; y < cimg.height(); y++ )
  197. {
  198. for ( int x = 0; x < cimg.width(); x++, counter++ )
  199. {
  200. fout << feats[counter][8] << " ";
  201. }
  202. }
  203. cout << "counter: " << counter << " feats.size(): " << feats.size() << endl;
  204. fout.close();*/
  205. out.resize( cimg.width(), cimg.height() );
  206. out.set( 0, 0, 0 );
  207. this->visualizeFeatures( out, feats, pos );
  208. NICE::ColorImage combinedout( cimg.width()*2, cimg.height() );
  209. int width = ( int )cimg.width();
  210. for ( int y = 0; y < ( int )cimg.height(); y++ )
  211. {
  212. for ( int x = 0; x < width; x++ )
  213. {
  214. combinedout.setPixel( x, y, cimg.getPixel( x, y, 0 ), cimg.getPixel( x, y, 1 ), cimg.getPixel( x, y, 2 ) );
  215. combinedout.setPixel( x + width, y, out.getPixel( x, y, 0 ), out.getPixel( x, y, 1 ), out.getPixel( x, y, 2 ) );
  216. }
  217. }
  218. showImage( combinedout, "result" );
  219. }
  220. void LocalFeatureColorWeijer::getFeats( const NICE::ColorImage &img, MultiChannelImageT<double> &feats )
  221. {
  222. int width = ( int )img.width();
  223. int height = ( int )img.height();
  224. feats.reInit( width, height, 11);
  225. for ( int y = 0; y < height; y++ )
  226. {
  227. for ( int x = 0; x < width; x++ )
  228. {
  229. int r = img(x,y,0)/8;
  230. int g = img(x,y,1)/8;
  231. int b = img(x,y,2)/8;
  232. for ( uint i = 0; i < 11; i++ )
  233. {
  234. feats.set( x, y, hist[r][g][b][i], i );
  235. }
  236. }
  237. }
  238. }
  239. void LocalFeatureColorWeijer::getFeats( const ColorImage &img, NICE::ImageT<double> &feats, int color )
  240. {
  241. assert ( feats.width() > 0 );
  242. assert ( feats.height() > 0 );
  243. int width = ( int )img.width();
  244. int height = ( int )img.height();
  245. for ( int y = 0; y < height; y++ )
  246. for ( int x = 0; x < width; x++ )
  247. {
  248. int r = img(x,y,0)/8;
  249. int g = img(x,y,1)/8;
  250. int b = img(x,y,2)/8;
  251. feats.setPixel( x, y, hist[r][g][b][color] );
  252. }
  253. }
  254. ///////////////////// INTERFACE PERSISTENT /////////////////////
  255. // interface specific methods for store and restore
  256. ///////////////////// INTERFACE PERSISTENT /////////////////////
  257. void LocalFeatureColorWeijer::restore ( std::istream & is, int format )
  258. {
  259. //delete everything we knew so far...
  260. this->clear();
  261. bool b_restoreVerbose ( false );
  262. #ifdef B_RESTOREVERBOSE
  263. b_restoreVerbose = true;
  264. #endif
  265. if ( is.good() )
  266. {
  267. if ( b_restoreVerbose )
  268. std::cerr << " restore LocalFeatureColorWeijer" << std::endl;
  269. std::string tmp;
  270. is >> tmp; //class name
  271. if ( ! this->isStartTag( tmp, "LocalFeatureColorWeijer" ) )
  272. {
  273. std::cerr << " WARNING - attempt to restore LocalFeatureColorWeijer, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  274. throw;
  275. }
  276. bool b_endOfBlock ( false ) ;
  277. while ( !b_endOfBlock )
  278. {
  279. is >> tmp; // start of block
  280. if ( this->isEndTag( tmp, "LocalFeatureColorWeijer" ) )
  281. {
  282. b_endOfBlock = true;
  283. continue;
  284. }
  285. tmp = this->removeStartTag ( tmp );
  286. if ( b_restoreVerbose )
  287. std::cerr << " currently restore section " << tmp << " in LocalFeatureColorWeijer" << std::endl;
  288. if ( tmp.compare("tfile") == 0 )
  289. {
  290. is >> this->tfile;
  291. is >> tmp; // end of block
  292. tmp = this->removeEndTag ( tmp );
  293. }
  294. else
  295. {
  296. std::cerr << "WARNING -- unexpected LocalFeatureColorWeijer object -- " << tmp << " -- for restoration... aborting" << std::endl;
  297. throw;
  298. }
  299. }
  300. }
  301. else
  302. {
  303. std::cerr << "LocalFeatureColorWeijer::restore -- InStream not initialized - restoring not possible!" << std::endl;
  304. throw;
  305. }
  306. // try to load the LUT from a separate external file
  307. //NOTE we could also think about adding the LUT into the store/restore process. However, the table is BIG, so it would
  308. // blow up whole outfile
  309. this->restoreLUT ( ) ;
  310. }
  311. void LocalFeatureColorWeijer::store ( std::ostream & os, int format ) const
  312. {
  313. if (os.good())
  314. {
  315. // show starting point
  316. os << this->createStartTag( "LocalFeatureColorWeijer" ) << std::endl;
  317. os << this->createStartTag( "tfile" ) << std::endl;
  318. os << this->tfile << std::endl;
  319. os << this->createEndTag( "tfile" ) << std::endl;
  320. // done
  321. os << this->createEndTag( "LocalFeatureColorWeijer" ) << std::endl;
  322. }
  323. else
  324. {
  325. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  326. }
  327. }
  328. void LocalFeatureColorWeijer::clear ()
  329. {
  330. }