LocalFeatureColorWeijer.cpp 9.6 KB

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