LocalFeatureColorWeijer.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // STL includes
  2. #include <fstream>
  3. #include <iostream>
  4. #include <exception>
  5. // nice-vislearning includes
  6. #include "vislearning/baselib/ColorSpace.h"
  7. //
  8. #include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. //! color representation for visualization
  13. const int colors[11][3] =
  14. {
  15. {0, 0, 0}, // black
  16. {0, 0, 255}, // blue
  17. {165, 42, 42}, // brown
  18. {190, 190, 190}, // grey
  19. { 0, 255, 0}, // green
  20. {255, 165, 0}, // orange
  21. {255, 192, 203}, // pink
  22. {160, 32, 240}, // purple
  23. {255, 0, 0}, // red
  24. {255, 255, 255}, // white
  25. {255, 255, 0}, // yellow
  26. };
  27. LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
  28. {
  29. this->conf = c;
  30. this->tfile = this->conf->gS( "LocalFeatureColorWeijer", "table", "");
  31. if ( this->tfile.compare("") == 0 )
  32. {
  33. // call echo $NICEHOME on a shell and capture the result
  34. // if NICEHOME is set in your environment, we can use this to
  35. // get the correct place to look for the settings...
  36. FILE* pipe = popen("echo $NICEHOME", "r");
  37. if ( !pipe )
  38. {
  39. // error in reading system call result
  40. // we do not output any error here, but throw an Exception lateron
  41. }
  42. else
  43. {
  44. char buffer[128];
  45. std::string s_NICEHOME = "";
  46. while( !feof(pipe) )
  47. {
  48. if(fgets(buffer, 128, pipe) != NULL)
  49. s_NICEHOME += buffer;
  50. }
  51. pclose(pipe);
  52. if ( s_NICEHOME.length() <= 1 )
  53. {
  54. // NICEHOME is not set as enviromental variable
  55. // this will produce an Exception lateron since no LUT file can be read
  56. this->tfile = "";
  57. }
  58. else
  59. this->tfile = s_NICEHOME + "/vislearning/features/localfeatures/input/colorWeijer/w2c.txt";
  60. }
  61. }
  62. this->restore();
  63. }
  64. LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
  65. {
  66. }
  67. int LocalFeatureColorWeijer::getDescSize() const
  68. {
  69. return LASTCOLOR;
  70. }
  71. void LocalFeatureColorWeijer::restore()
  72. {
  73. ifstream fin( this->tfile.c_str() );
  74. if( !fin.is_open() )
  75. {
  76. 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");
  77. }
  78. while(!fin.eof())
  79. {
  80. double rd,gd,bd;
  81. int r,g,b;
  82. fin >> rd;
  83. fin >> gd;
  84. fin >> bd;
  85. r = rd/8;
  86. g = gd/8;
  87. b = bd/8;
  88. // read LUT values for all 11 colornames
  89. for( int i = 0; i < 11; i++ )
  90. {
  91. fin >> hist[r][g][b][i];
  92. }
  93. }
  94. /*
  95. for(int r = 0; r < 32; r++)
  96. {
  97. for(int g = 0; g < 32; g++)
  98. {
  99. for(int b = 0; b < 32; b++)
  100. {
  101. for(int i = 0; i < 11; i++)
  102. {
  103. fin >> hist[r][g][b][i];
  104. }
  105. }
  106. }
  107. } */
  108. }
  109. int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector & positions, VVector & features ) const
  110. {
  111. throw NICE::Exception ( "LocalFeatureColorWeijer extracts COLOR Features, it won't work on gray value images");
  112. }
  113. int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
  114. {
  115. int width = ( int )img.width();
  116. int height = ( int )img.height();
  117. for ( int j = 0; j < ( int )positions.size(); j++ )
  118. {
  119. int x = positions[j][0];
  120. int y = positions[j][1];
  121. int r = img(x,y,0)/8;
  122. int g = img(x,y,1)/8;
  123. int b = img(x,y,2)/8;
  124. Vector feat( 11 );
  125. for ( uint i = 0; i < 11; i++ )
  126. {
  127. feat[i] = hist[r][g][b][i];
  128. }
  129. features.push_back( feat );
  130. }
  131. return 1;
  132. }
  133. void LocalFeatureColorWeijer::visualizeFeatures( NICE::Image & mark, const VVector & positions, size_t color ) const
  134. {
  135. }
  136. int LocalFeatureColorWeijer::findColor( string &fn )
  137. {
  138. if ( fn.find( "black" ) != string::npos )
  139. return BLACK;
  140. if ( fn.find( "blue" ) != string::npos )
  141. return BLUE;
  142. if ( fn.find( "brown" ) != string::npos )
  143. return BROWN;
  144. if ( fn.find( "grey" ) != string::npos )
  145. return GREY;
  146. if ( fn.find( "green" ) != string::npos )
  147. return GREEN;
  148. if ( fn.find( "orange" ) != string::npos )
  149. return ORANGE;
  150. if ( fn.find( "pink" ) != string::npos )
  151. return PINK;
  152. if ( fn.find( "purple" ) != string::npos )
  153. return PURPLE;
  154. if ( fn.find( "red" ) != string::npos )
  155. return RED;
  156. if ( fn.find( "white" ) != string::npos )
  157. return WHITE;
  158. if ( fn.find( "yellow" ) != string::npos )
  159. return YELLOW;
  160. return -1;
  161. }
  162. void LocalFeatureColorWeijer::visualizeFeatures( NICE::ColorImage & out, const VVector & features, const VVector & position ) const
  163. {
  164. for ( int i = 0; i < ( int )position.size(); i++ )
  165. {
  166. int maxpos = 0;
  167. double maxval = 0.0;
  168. for ( int j = 0; j < ( int )features[i].size(); j++ )
  169. {
  170. if ( maxval < features[i][j] )
  171. {
  172. maxval = features[i][j];
  173. maxpos = j;
  174. }
  175. }
  176. out.setPixel( position[i][0], position[i][1], colors[maxpos][0], colors[maxpos][1], colors[maxpos][2] );
  177. }
  178. }
  179. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg ) const
  180. {
  181. NICE::ColorImage out;
  182. this->visualizeFeatures( cimg, out );
  183. }
  184. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg, NICE::ColorImage &out ) const
  185. {
  186. VVector pos, feats;
  187. for ( int y = 0; y < cimg.height(); y++ )
  188. {
  189. for ( int x = 0; x < cimg.width(); x++ )
  190. {
  191. Vector vec( 2 );
  192. vec[0] = x;
  193. vec[1] = y;
  194. pos.push_back( vec );
  195. }
  196. }
  197. this->getDescriptors( cimg, pos, feats );
  198. //heatmap for a special class
  199. /*ofstream fout("out.txt");
  200. int counter = 0;
  201. for ( int y = 0; y < cimg.height(); y++ )
  202. {
  203. for ( int x = 0; x < cimg.width(); x++, counter++ )
  204. {
  205. fout << feats[counter][8] << " ";
  206. }
  207. }
  208. cout << "counter: " << counter << " feats.size(): " << feats.size() << endl;
  209. fout.close();*/
  210. out.resize( cimg.width(), cimg.height() );
  211. out.set( 0, 0, 0 );
  212. this->visualizeFeatures( out, feats, pos );
  213. NICE::ColorImage combinedout( cimg.width()*2, cimg.height() );
  214. int width = ( int )cimg.width();
  215. for ( int y = 0; y < ( int )cimg.height(); y++ )
  216. {
  217. for ( int x = 0; x < width; x++ )
  218. {
  219. combinedout.setPixel( x, y, cimg.getPixel( x, y, 0 ), cimg.getPixel( x, y, 1 ), cimg.getPixel( x, y, 2 ) );
  220. combinedout.setPixel( x + width, y, out.getPixel( x, y, 0 ), out.getPixel( x, y, 1 ), out.getPixel( x, y, 2 ) );
  221. }
  222. }
  223. showImage( combinedout, "result" );
  224. }
  225. void LocalFeatureColorWeijer::getFeats( const ColorImage &img, MultiChannelImageT<double> &feats )
  226. {
  227. int width = ( int )img.width();
  228. int height = ( int )img.height();
  229. feats.reInit( width, height, 11);
  230. for ( int y = 0; y < height; y++ )
  231. {
  232. for ( int x = 0; x < width; x++ )
  233. {
  234. int r = img(x,y,0)/8;
  235. int g = img(x,y,1)/8;
  236. int b = img(x,y,2)/8;
  237. for ( uint i = 0; i < 11; i++ )
  238. {
  239. feats.set( x, y, hist[r][g][b][i], i );
  240. }
  241. }
  242. }
  243. return;
  244. }