123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // STL includes
- #include <fstream>
- #include <iostream>
- #include <exception>
- // nice-vislearning includes
- #include "vislearning/baselib/ColorSpace.h"
- #include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- //! color representation for visualization
- const int colors[11][3] =
- {
- {0, 0, 0}, // black
- {0, 0, 255}, // blue
- {165, 42, 42}, // brown
- {190, 190, 190}, // grey
- { 0, 255, 0}, // green
- {255, 165, 0}, // orange
- {255, 192, 203}, // pink
- {160, 32, 240}, // purple
- {255, 0, 0}, // red
- {255, 255, 255}, // white
- {255, 255, 0}, // yellow
- };
- LocalFeatureColorWeijer::LocalFeatureColorWeijer( const Config *c )
- {
- conf = c;
- tfile = conf->gS( "LocalFeatureColorWeijer", "table", "/home/dbv/bilder/colorWeijer/w2c.txt");
- restore();
- }
- LocalFeatureColorWeijer::~LocalFeatureColorWeijer()
- {
- }
- int LocalFeatureColorWeijer::getDescSize() const
- {
- return LASTCOLOR;
- }
- void LocalFeatureColorWeijer::restore()
- {
- ifstream fin( tfile.c_str() );
- if(!fin.is_open())
- {
- fthrow(Exception,"ColorWeijer: could not find lookup table file.");
- }
-
- while(!fin.eof())
- {
- double rd,gd,bd;
- int r,g,b;
- fin >> rd;
- fin >> gd;
- fin >> bd;
-
- r = rd/8;
- g = gd/8;
- b = bd/8;
- for(int i = 0; i < 11; i++)
- {
- fin >> hist[r][g][b][i];
- }
- }
- /*
- for(int r = 0; r < 32; r++)
- {
- for(int g = 0; g < 32; g++)
- {
- for(int b = 0; b < 32; b++)
- {
- for(int i = 0; i < 11; i++)
- {
- fin >> hist[r][g][b][i];
- }
- }
- }
- } */
- }
- int LocalFeatureColorWeijer::getDescriptors( const NICE::Image & img, VVector & positions, VVector & features ) const
- {
- throw NICE::Exception ( "LocalFeatureColorWeijer extracts COLOR Features, it won't work on gray value images");
- }
- int LocalFeatureColorWeijer::getDescriptors( const NICE::ColorImage & img, VVector & positions, VVector & features ) const
- {
- int width = ( int )img.width();
- int height = ( int )img.height();
- for ( int j = 0; j < ( int )positions.size(); j++ )
- {
- int x = positions[j][0];
- int y = positions[j][1];
- int r = img(x,y,0)/8;
- int g = img(x,y,1)/8;
- int b = img(x,y,2)/8;
-
- Vector feat( 11 );
- for ( uint i = 0; i < 11; i++ )
- {
- feat[i] = hist[r][g][b][i];
- }
- features.push_back( feat );
- }
-
- return 1;
- }
- void LocalFeatureColorWeijer::visualizeFeatures( NICE::Image & mark, const VVector & positions, size_t color ) const
- {
- }
- int LocalFeatureColorWeijer::findColor( string &fn )
- {
- if ( fn.find( "black" ) != string::npos )
- return BLACK;
- if ( fn.find( "blue" ) != string::npos )
- return BLUE;
- if ( fn.find( "brown" ) != string::npos )
- return BROWN;
- if ( fn.find( "grey" ) != string::npos )
- return GREY;
- if ( fn.find( "green" ) != string::npos )
- return GREEN;
- if ( fn.find( "orange" ) != string::npos )
- return ORANGE;
- if ( fn.find( "pink" ) != string::npos )
- return PINK;
- if ( fn.find( "purple" ) != string::npos )
- return PURPLE;
- if ( fn.find( "red" ) != string::npos )
- return RED;
- if ( fn.find( "white" ) != string::npos )
- return WHITE;
- if ( fn.find( "yellow" ) != string::npos )
- return YELLOW;
- return -1;
- }
- void LocalFeatureColorWeijer::visualizeFeatures( NICE::ColorImage & out, const VVector & features, const VVector & position ) const
- {
- for ( int i = 0; i < ( int )position.size(); i++ )
- {
- int maxpos = 0;
- double maxval = 0.0;
- for ( int j = 0; j < ( int )features[i].size(); j++ )
- {
- if ( maxval < features[i][j] )
- {
- maxval = features[i][j];
- maxpos = j;
- }
- }
- out.setPixel( position[i][0], position[i][1], colors[maxpos][0], colors[maxpos][1], colors[maxpos][2] );
- }
- }
- void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg ) const
- {
- ColorImage out;
- visualizeFeatures( cimg, out );
- }
- void LocalFeatureColorWeijer::visualizeFeatures( const NICE::ColorImage & cimg, NICE::ColorImage &out ) const
- {
- VVector pos, feats;
- for ( int y = 0; y < cimg.height(); y++ )
- {
- for ( int x = 0; x < cimg.width(); x++ )
- {
- Vector vec( 2 );
- vec[0] = x;
- vec[1] = y;
- pos.push_back( vec );
- }
- }
- getDescriptors( cimg, pos, feats );
-
- //heatmap for a special class
- /*ofstream fout("out.txt");
-
- int counter = 0;
- for ( int y = 0; y < cimg.height(); y++ )
- {
- for ( int x = 0; x < cimg.width(); x++, counter++ )
- {
- fout << feats[counter][8] << " ";
- }
- }
- cout << "counter: " << counter << " feats.size(): " << feats.size() << endl;
-
- fout.close();*/
- out.resize( cimg.width(), cimg.height() );
- out.set( 0, 0, 0 );
- visualizeFeatures( out, feats, pos );
- ColorImage combinedout( cimg.width()*2, cimg.height() );
- int width = ( int )cimg.width();
- for ( int y = 0; y < ( int )cimg.height(); y++ )
- {
- for ( int x = 0; x < width; x++ )
- {
- combinedout.setPixel( x, y, cimg.getPixel( x, y, 0 ), cimg.getPixel( x, y, 1 ), cimg.getPixel( x, y, 2 ) );
- combinedout.setPixel( x + width, y, out.getPixel( x, y, 0 ), out.getPixel( x, y, 1 ), out.getPixel( x, y, 2 ) );
- }
- }
- showImage( combinedout, "result" );
- }
- void LocalFeatureColorWeijer::getFeats( const ColorImage &img, MultiChannelImageT<double> &feats )
- {
- int width = ( int )img.width();
- int height = ( int )img.height();
- feats.reInit( width, height, 11);
- for ( int y = 0; y < height; y++ )
- {
- for ( int x = 0; x < width; x++ )
- {
- int r = img(x,y,0)/8;
- int g = img(x,y,1)/8;
- int b = img(x,y,2)/8;
-
- for ( uint i = 0; i < 11; i++ )
- {
- feats.set( x, y, hist[r][g][b][i], i );
- }
- }
- }
- return;
- }
|