LFColorWeijer.cpp 5.5 KB

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