LocalFeatureColorWeijer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
  29. {
  30. this->conf = c;
  31. this->tfile = this->conf->gS( "LocalFeatureColorWeijer", "table", "");
  32. // If no destination to the LUT was given, we try to use the file shipped with this library
  33. // Therefore, we try to catch the NICEHOME and if possible, append the location where the LUT file is stored
  34. // NOTE This will only work if NICEHOME is set in your environment (e.g., by exporting it in your bashrc)
  35. if ( this->tfile.compare("") == 0 )
  36. {
  37. char* pNICEHOME;
  38. pNICEHOME = getenv ("NICEHOME");
  39. if ( pNICEHOME != NULL )
  40. {
  41. this->tfile = std::string ( pNICEHOME );
  42. this->tfile += "/vislearning/features/localfeatures/input/colorWeijer/w2c.txt";
  43. }
  44. }
  45. this->restore();
  46. }
  47. LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
  48. {
  49. }
  50. int LocalFeatureColorWeijer::getDescSize() const
  51. {
  52. return LASTCOLOR;
  53. }
  54. void LocalFeatureColorWeijer::restore()
  55. {
  56. ifstream fin( this->tfile.c_str() );
  57. if( !fin.is_open() )
  58. {
  59. 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");
  60. }
  61. while(!fin.eof())
  62. {
  63. double rd,gd,bd;
  64. int r,g,b;
  65. fin >> rd;
  66. fin >> gd;
  67. fin >> bd;
  68. r = rd/8;
  69. g = gd/8;
  70. b = bd/8;
  71. // read LUT values for all 11 colornames
  72. for( int i = 0; i < 11; i++ )
  73. {
  74. fin >> hist[r][g][b][i];
  75. }
  76. }
  77. /*
  78. for(int r = 0; r < 32; r++)
  79. {
  80. for(int g = 0; g < 32; g++)
  81. {
  82. for(int b = 0; b < 32; b++)
  83. {
  84. for(int i = 0; i < 11; i++)
  85. {
  86. fin >> hist[r][g][b][i];
  87. }
  88. }
  89. }
  90. } */
  91. }
  92. int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector & positions, VVector & features ) const
  93. {
  94. throw NICE::Exception ( "LocalFeatureColorWeijer extracts COLOR Features, it won't work on gray value images");
  95. }
  96. int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
  97. {
  98. int width = ( int )img.width();
  99. int height = ( int )img.height();
  100. for ( int j = 0; j < ( int )positions.size(); j++ )
  101. {
  102. int x = positions[j][0];
  103. int y = positions[j][1];
  104. int r = img(x,y,0)/8;
  105. int g = img(x,y,1)/8;
  106. int b = img(x,y,2)/8;
  107. Vector feat( 11 );
  108. for ( uint i = 0; i < 11; i++ )
  109. {
  110. feat[i] = hist[r][g][b][i];
  111. }
  112. features.push_back( feat );
  113. }
  114. return 1;
  115. }
  116. void LocalFeatureColorWeijer::visualizeFeatures( NICE::Image & mark, const VVector & positions, size_t color ) const
  117. {
  118. }
  119. int LocalFeatureColorWeijer::findColor( string &fn )
  120. {
  121. if ( fn.find( "black" ) != string::npos )
  122. return BLACK;
  123. if ( fn.find( "blue" ) != string::npos )
  124. return BLUE;
  125. if ( fn.find( "brown" ) != string::npos )
  126. return BROWN;
  127. if ( fn.find( "grey" ) != string::npos )
  128. return GREY;
  129. if ( fn.find( "green" ) != string::npos )
  130. return GREEN;
  131. if ( fn.find( "orange" ) != string::npos )
  132. return ORANGE;
  133. if ( fn.find( "pink" ) != string::npos )
  134. return PINK;
  135. if ( fn.find( "purple" ) != string::npos )
  136. return PURPLE;
  137. if ( fn.find( "red" ) != string::npos )
  138. return RED;
  139. if ( fn.find( "white" ) != string::npos )
  140. return WHITE;
  141. if ( fn.find( "yellow" ) != string::npos )
  142. return YELLOW;
  143. return -1;
  144. }
  145. void LocalFeatureColorWeijer::visualizeFeatures( NICE::ColorImage & out, const VVector & features, const VVector & position ) const
  146. {
  147. for ( int i = 0; i < ( int )position.size(); i++ )
  148. {
  149. int maxpos = 0;
  150. double maxval = 0.0;
  151. for ( int j = 0; j < ( int )features[i].size(); j++ )
  152. {
  153. if ( maxval < features[i][j] )
  154. {
  155. maxval = features[i][j];
  156. maxpos = j;
  157. }
  158. }
  159. out.setPixel( position[i][0], position[i][1], colors[maxpos][0], colors[maxpos][1], colors[maxpos][2] );
  160. }
  161. }
  162. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg ) const
  163. {
  164. NICE::ColorImage out;
  165. this->visualizeFeatures( cimg, out );
  166. }
  167. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg, NICE::ColorImage &out ) const
  168. {
  169. VVector pos, feats;
  170. for ( int y = 0; y < cimg.height(); y++ )
  171. {
  172. for ( int x = 0; x < cimg.width(); x++ )
  173. {
  174. Vector vec( 2 );
  175. vec[0] = x;
  176. vec[1] = y;
  177. pos.push_back( vec );
  178. }
  179. }
  180. this->getDescriptors( cimg, pos, feats );
  181. //heatmap for a special class
  182. /*ofstream fout("out.txt");
  183. int counter = 0;
  184. for ( int y = 0; y < cimg.height(); y++ )
  185. {
  186. for ( int x = 0; x < cimg.width(); x++, counter++ )
  187. {
  188. fout << feats[counter][8] << " ";
  189. }
  190. }
  191. cout << "counter: " << counter << " feats.size(): " << feats.size() << endl;
  192. fout.close();*/
  193. out.resize( cimg.width(), cimg.height() );
  194. out.set( 0, 0, 0 );
  195. this->visualizeFeatures( out, feats, pos );
  196. NICE::ColorImage combinedout( cimg.width()*2, cimg.height() );
  197. int width = ( int )cimg.width();
  198. for ( int y = 0; y < ( int )cimg.height(); y++ )
  199. {
  200. for ( int x = 0; x < width; x++ )
  201. {
  202. combinedout.setPixel( x, y, cimg.getPixel( x, y, 0 ), cimg.getPixel( x, y, 1 ), cimg.getPixel( x, y, 2 ) );
  203. combinedout.setPixel( x + width, y, out.getPixel( x, y, 0 ), out.getPixel( x, y, 1 ), out.getPixel( x, y, 2 ) );
  204. }
  205. }
  206. showImage( combinedout, "result" );
  207. }
  208. void LocalFeatureColorWeijer::getFeats( const ColorImage &img, MultiChannelImageT<double> &feats )
  209. {
  210. int width = ( int )img.width();
  211. int height = ( int )img.height();
  212. feats.reInit( width, height, 11);
  213. for ( int y = 0; y < height; y++ )
  214. {
  215. for ( int x = 0; x < width; x++ )
  216. {
  217. int r = img(x,y,0)/8;
  218. int g = img(x,y,1)/8;
  219. int b = img(x,y,2)/8;
  220. for ( uint i = 0; i < 11; i++ )
  221. {
  222. feats.set( x, y, hist[r][g][b][i], i );
  223. }
  224. }
  225. }
  226. return;
  227. }