LocalFeatureColorWeijer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // STL includes
  2. #include <fstream>
  3. #include <iostream>
  4. #include <exception>
  5. // nice-vislearning includes
  6. #include "vislearning/baselib/ColorSpace.h"
  7. #include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
  8. using namespace OBJREC;
  9. using namespace std;
  10. using namespace NICE;
  11. //! color representation for visualization
  12. const int colors[11][3] =
  13. {
  14. {0, 0, 0}, // black
  15. {0, 0, 255}, // blue
  16. {165, 42, 42}, // brown
  17. {190, 190, 190}, // grey
  18. { 0, 255, 0}, // green
  19. {255, 165, 0}, // orange
  20. {255, 192, 203}, // pink
  21. {160, 32, 240}, // purple
  22. {255, 0, 0}, // red
  23. {255, 255, 255}, // white
  24. {255, 255, 0}, // yellow
  25. };
  26. LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
  27. {
  28. conf = c;
  29. tfile = conf->gS( "LocalFeatureColorWeijer", "table", "/home/dbv/bilder/colorWeijer/w2c.txt");
  30. restore();
  31. }
  32. LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
  33. {
  34. }
  35. int LocalFeatureColorWeijer::getDescSize() const
  36. {
  37. return LASTCOLOR;
  38. }
  39. void LocalFeatureColorWeijer::restore()
  40. {
  41. ifstream fin( tfile.c_str() );
  42. if(!fin.is_open())
  43. {
  44. fthrow(Exception,"ColorWeijer: could not find lookup table file.");
  45. }
  46. while(!fin.eof())
  47. {
  48. double rd,gd,bd;
  49. int r,g,b;
  50. fin >> rd;
  51. fin >> gd;
  52. fin >> bd;
  53. r = rd/8;
  54. g = gd/8;
  55. b = bd/8;
  56. for(int i = 0; i < 11; i++)
  57. {
  58. fin >> hist[r][g][b][i];
  59. }
  60. }
  61. /*
  62. for(int r = 0; r < 32; r++)
  63. {
  64. for(int g = 0; g < 32; g++)
  65. {
  66. for(int b = 0; b < 32; b++)
  67. {
  68. for(int i = 0; i < 11; i++)
  69. {
  70. fin >> hist[r][g][b][i];
  71. }
  72. }
  73. }
  74. } */
  75. }
  76. int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector & positions, VVector & features ) const
  77. {
  78. throw NICE::Exception ( "LocalFeatureColorWeijer extracts COLOR Features, it won't work on gray value images");
  79. }
  80. int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
  81. {
  82. int width = ( int )img.width();
  83. int height = ( int )img.height();
  84. for ( int j = 0; j < ( int )positions.size(); j++ )
  85. {
  86. int x = positions[j][0];
  87. int y = positions[j][1];
  88. int r = img(x,y,0)/8;
  89. int g = img(x,y,1)/8;
  90. int b = img(x,y,2)/8;
  91. Vector feat( 11 );
  92. for ( uint i = 0; i < 11; i++ )
  93. {
  94. feat[i] = hist[r][g][b][i];
  95. }
  96. features.push_back( feat );
  97. }
  98. return 1;
  99. }
  100. void LocalFeatureColorWeijer::visualizeFeatures( NICE::Image & mark, const VVector & positions, size_t color ) const
  101. {
  102. }
  103. int LocalFeatureColorWeijer::findColor( string &fn )
  104. {
  105. if ( fn.find( "black" ) != string::npos )
  106. return BLACK;
  107. if ( fn.find( "blue" ) != string::npos )
  108. return BLUE;
  109. if ( fn.find( "brown" ) != string::npos )
  110. return BROWN;
  111. if ( fn.find( "grey" ) != string::npos )
  112. return GREY;
  113. if ( fn.find( "green" ) != string::npos )
  114. return GREEN;
  115. if ( fn.find( "orange" ) != string::npos )
  116. return ORANGE;
  117. if ( fn.find( "pink" ) != string::npos )
  118. return PINK;
  119. if ( fn.find( "purple" ) != string::npos )
  120. return PURPLE;
  121. if ( fn.find( "red" ) != string::npos )
  122. return RED;
  123. if ( fn.find( "white" ) != string::npos )
  124. return WHITE;
  125. if ( fn.find( "yellow" ) != string::npos )
  126. return YELLOW;
  127. return -1;
  128. }
  129. void LocalFeatureColorWeijer::visualizeFeatures( NICE::ColorImage & out, const VVector & features, const VVector & position ) const
  130. {
  131. for ( int i = 0; i < ( int )position.size(); i++ )
  132. {
  133. int maxpos = 0;
  134. double maxval = 0.0;
  135. for ( int j = 0; j < ( int )features[i].size(); j++ )
  136. {
  137. if ( maxval < features[i][j] )
  138. {
  139. maxval = features[i][j];
  140. maxpos = j;
  141. }
  142. }
  143. out.setPixel( position[i][0], position[i][1], colors[maxpos][0], colors[maxpos][1], colors[maxpos][2] );
  144. }
  145. }
  146. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg ) const
  147. {
  148. ColorImage out;
  149. visualizeFeatures( cimg, out );
  150. }
  151. void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg, NICE::ColorImage &out ) const
  152. {
  153. VVector pos, feats;
  154. for ( int y = 0; y < cimg.height(); y++ )
  155. {
  156. for ( int x = 0; x < cimg.width(); x++ )
  157. {
  158. Vector vec( 2 );
  159. vec[0] = x;
  160. vec[1] = y;
  161. pos.push_back( vec );
  162. }
  163. }
  164. getDescriptors( cimg, pos, feats );
  165. //heatmap for a special class
  166. /*ofstream fout("out.txt");
  167. int counter = 0;
  168. for ( int y = 0; y < cimg.height(); y++ )
  169. {
  170. for ( int x = 0; x < cimg.width(); x++, counter++ )
  171. {
  172. fout << feats[counter][8] << " ";
  173. }
  174. }
  175. cout << "counter: " << counter << " feats.size(): " << feats.size() << endl;
  176. fout.close();*/
  177. out.resize( cimg.width(), cimg.height() );
  178. out.set( 0, 0, 0 );
  179. visualizeFeatures( out, feats, pos );
  180. ColorImage combinedout( cimg.width()*2, cimg.height() );
  181. int width = ( int )cimg.width();
  182. for ( int y = 0; y < ( int )cimg.height(); y++ )
  183. {
  184. for ( int x = 0; x < width; x++ )
  185. {
  186. combinedout.setPixel( x, y, cimg.getPixel( x, y, 0 ), cimg.getPixel( x, y, 1 ), cimg.getPixel( x, y, 2 ) );
  187. combinedout.setPixel( x + width, y, out.getPixel( x, y, 0 ), out.getPixel( x, y, 1 ), out.getPixel( x, y, 2 ) );
  188. }
  189. }
  190. showImage( combinedout, "result" );
  191. }
  192. void LocalFeatureColorWeijer::getFeats( const ColorImage &img, MultiChannelImageT<double> &feats )
  193. {
  194. int width = ( int )img.width();
  195. int height = ( int )img.height();
  196. feats.reInit( width, height, 11);
  197. for ( int y = 0; y < height; y++ )
  198. {
  199. for ( int x = 0; x < width; x++ )
  200. {
  201. int r = img(x,y,0)/8;
  202. int g = img(x,y,1)/8;
  203. int b = img(x,y,2)/8;
  204. for ( uint i = 0; i < 11; i++ )
  205. {
  206. feats.set( x, y, hist[r][g][b][i], i );
  207. }
  208. }
  209. }
  210. return;
  211. }