LocalFeatureColorWeijer.cpp 9.3 KB

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