123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /**
- * @file CodebookPrototypes.cpp
- * @brief feature CodebookPrototypes
- * @author Erik Rodner
- * @date 02/15/2008
- */
- #include <core/image/Convert.h>
- #include <iostream>
- #include <assert.h>
- #include "CodebookPrototypes.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- CodebookPrototypes::CodebookPrototypes()
- {
- }
- CodebookPrototypes::CodebookPrototypes( const std::string & filename )
- {
- Codebook::read(filename);
- }
- CodebookPrototypes::CodebookPrototypes( const VVector & vv )
- {
- for ( const_iterator i = vv.begin(); i != vv.end(); i++ )
- push_back (*i);
- reinit ( vv.size() );
- }
- CodebookPrototypes::~CodebookPrototypes()
- {
- }
- void CodebookPrototypes::copy ( const Codebook *codebook )
- {
- Codebook::copy ( codebook );
- VVector::clear();
- const CodebookPrototypes *codebookp = dynamic_cast<const CodebookPrototypes *> ( codebook );
- assert ( codebookp != NULL );
- insert ( begin(), codebookp->begin(), codebookp->end() );
- }
- void CodebookPrototypes::vote ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
- {
- const double *xp = feature.getDataPointer();
- size_t k = 0;
- double mindist = numeric_limits<double>::max();
- size_t my_cluster = 0;
-
- int len = feature.size();
- for ( vector<Vector>::const_iterator i = begin();
- i != end();
- i++,k++ )
- {
- const NICE::Vector & cluster = *i;
- //slow ICE variant
- //double distance = (x - cluster).Length();
- double distance = 0.0;
- const double *clusterp = cluster.getDataPointer();
- for ( int i = 0 ; i < len ; i++ )
- {
- double h = clusterp[i] - xp[i];
- distance += h*h;
- }
- if ( distance < mindist )
- {
- my_cluster = k;
- mindist = distance;
- }
- }
- codebookEntry = my_cluster;
- weight = 1.0;
- distance = mindist;
- }
-
- void CodebookPrototypes::add ( const Codebook *codebook )
- {
- informativeMeasure.append ( codebook->getInformativeMeasure() );
- thresholds.append ( codebook->getThresholds() );
- classnos.append ( codebook->getClassNos() );
- const CodebookPrototypes *codebookp = dynamic_cast< const CodebookPrototypes *> ( codebook );
- assert ( codebookp != NULL );
- insert ( begin(), codebookp->begin(), codebookp->end() );
- }
-
- Codebook *CodebookPrototypes::clone () const
- {
- return (new CodebookPrototypes());
- }
- void CodebookPrototypes::clear ()
- {
- VVector::clear();
- Codebook::clear();
- }
- void CodebookPrototypes::restore ( istream & is, int format )
- {
- Codebook::restore ( is, format );
- VVector::restore ( is, format );
- }
- void CodebookPrototypes::store ( ostream & os, int format ) const
- {
- Codebook::store ( os, format );
- VVector::store ( os, format );
- }
- void CodebookPrototypes::displayCodebook ( int xsize, int ysize ) const
- {
- NICE::Image bigimg ( xsize * 5 , ysize * size() );
- bigimg.set(0);
- NICE::Image img_s (xsize, ysize);
- for ( int k = 0 ; k < (int)size() ; k++ )
- {
- NICE::Vector vimg = *(begin() + k);
- NICE::Image img ((int)sqrt((double)vimg.size()), (int)sqrt((double)vimg.size()));
- int i = 0;
- double max = - numeric_limits<double>::max();
- double min = numeric_limits<double>::min();
- for ( int y = 0 ; y < img.height() ; y++ )
- for ( int x = 0 ; x < img.width() ; x++,i++ )
- {
- if ( max < vimg[i] ) max = vimg[i];
- if ( min > vimg[i] ) min = vimg[i];
- }
- i = 0;
- for ( int y = 0 ; y < img.height() ; y++ )
- for ( int x = 0 ; x < img.width() ; x++,i++ )
- {
- img.setPixel( x, y, (int)((vimg[i] - min)*255/(max-min)) );
- }
-
- NICE::scale ( img, &img_s );
- for ( int y = 0 ; y < ysize ; y++ )
- for ( int x = 0 ; x < xsize ; x++ )
- bigimg.setPixel(x, y+k*ysize, img_s.getPixel(x,y) );
- {
- std::ostringstream os;
- os << "no: " << k;
- if ( (int)informativeMeasure.size() > k )
- os << " " << informativeMeasure[k];
- // refactor-nice.pl: check this substitution
- // old: Text ( os.str().c_str(), xsize+1, k*ysize + 1, 255, 0, bigimg );
- // REFACTOR-FIXME Unable to map this statement
- }
- }
- bigimg.write ("/tmp/display.bmp");
- }
|