|
@@ -1,15 +1,16 @@
|
|
/**
|
|
/**
|
|
* @file CodebookPrototypes.cpp
|
|
* @file CodebookPrototypes.cpp
|
|
* @brief feature CodebookPrototypes
|
|
* @brief feature CodebookPrototypes
|
|
-* @author Erik Rodner
|
|
|
|
-* @date 02/15/2008
|
|
|
|
-
|
|
|
|
|
|
+* @author Erik Rodner, Alexander Freytag
|
|
|
|
+* @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
|
|
*/
|
|
*/
|
|
-#include <core/image/Convert.h>
|
|
|
|
-
|
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
#include <assert.h>
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
+#include <core/image/Convert.h>
|
|
|
|
+
|
|
|
|
+#include "vislearning/math/distances/genericDistance.h"
|
|
|
|
+
|
|
#include "CodebookPrototypes.h"
|
|
#include "CodebookPrototypes.h"
|
|
|
|
|
|
using namespace OBJREC;
|
|
using namespace OBJREC;
|
|
@@ -20,6 +21,7 @@ using namespace NICE;
|
|
|
|
|
|
CodebookPrototypes::CodebookPrototypes()
|
|
CodebookPrototypes::CodebookPrototypes()
|
|
{
|
|
{
|
|
|
|
+ this->clear();
|
|
}
|
|
}
|
|
|
|
|
|
CodebookPrototypes::CodebookPrototypes( const std::string & filename )
|
|
CodebookPrototypes::CodebookPrototypes( const std::string & filename )
|
|
@@ -27,15 +29,24 @@ CodebookPrototypes::CodebookPrototypes( const std::string & filename )
|
|
Codebook::read(filename);
|
|
Codebook::read(filename);
|
|
}
|
|
}
|
|
|
|
|
|
-CodebookPrototypes::CodebookPrototypes( const VVector & vv )
|
|
|
|
|
|
+CodebookPrototypes::CodebookPrototypes( const NICE::VVector & vv )
|
|
{
|
|
{
|
|
- for ( const_iterator i = vv.begin(); i != vv.end(); i++ )
|
|
|
|
- push_back (*i);
|
|
|
|
- reinit ( vv.size() );
|
|
|
|
|
|
+ this->append ( vv, true /* _copyData*/ );
|
|
|
|
+
|
|
|
|
+ reinit ( vv.size() );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+CodebookPrototypes::CodebookPrototypes( NICE::Config * _conf, const std::string & _section) : Codebook ( _conf, _section )
|
|
|
|
+{
|
|
|
|
+ this->distanceType = _conf->gS( _section, "distanceType", "euclidean" );
|
|
|
|
+ this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
|
|
|
|
+
|
|
|
|
+ this->clear();
|
|
}
|
|
}
|
|
|
|
|
|
CodebookPrototypes::~CodebookPrototypes()
|
|
CodebookPrototypes::~CodebookPrototypes()
|
|
{
|
|
{
|
|
|
|
+ //NOTE the config does not need to be deleted, it is just a pointer to an external data structure
|
|
}
|
|
}
|
|
|
|
|
|
void CodebookPrototypes::copy ( const Codebook *codebook )
|
|
void CodebookPrototypes::copy ( const Codebook *codebook )
|
|
@@ -45,9 +56,12 @@ void CodebookPrototypes::copy ( const Codebook *codebook )
|
|
const CodebookPrototypes *codebookp = dynamic_cast<const CodebookPrototypes *> ( codebook );
|
|
const CodebookPrototypes *codebookp = dynamic_cast<const CodebookPrototypes *> ( codebook );
|
|
assert ( codebookp != NULL );
|
|
assert ( codebookp != NULL );
|
|
insert ( begin(), codebookp->begin(), codebookp->end() );
|
|
insert ( begin(), codebookp->begin(), codebookp->end() );
|
|
|
|
+
|
|
|
|
+ this->distanceType = this->p_conf->gS( this->s_section, "distanceType", "euclidean" );
|
|
|
|
+ this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
|
|
}
|
|
}
|
|
|
|
|
|
-void CodebookPrototypes::vote ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
|
|
|
|
|
|
+void CodebookPrototypes::voteVQ ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
|
|
{
|
|
{
|
|
const double *xp = feature.getDataPointer();
|
|
const double *xp = feature.getDataPointer();
|
|
size_t k = 0;
|
|
size_t k = 0;
|
|
@@ -55,34 +69,50 @@ void CodebookPrototypes::vote ( const NICE::Vector & feature, int & codebookEntr
|
|
size_t my_cluster = 0;
|
|
size_t my_cluster = 0;
|
|
|
|
|
|
int len = feature.size();
|
|
int len = feature.size();
|
|
- for ( vector<Vector>::const_iterator i = begin();
|
|
|
|
- i != end();
|
|
|
|
- i++,k++ )
|
|
|
|
|
|
+ // iterate over all cluster centers
|
|
|
|
+ for ( std::vector<NICE::Vector>::const_iterator i = this->begin();
|
|
|
|
+ i != this->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;
|
|
|
|
- }
|
|
|
|
|
|
+ const NICE::Vector & cluster = *i;
|
|
|
|
+ //slow ICE variant
|
|
|
|
+ //double distance = (x - cluster).Length();
|
|
|
|
+
|
|
|
|
+// const double *clusterp = cluster.getDataPointer();
|
|
|
|
+ double distance = this->distancefunction->calculate(*i, feature);
|
|
|
|
+
|
|
|
|
+ //old version without distance function
|
|
|
|
+// 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;
|
|
codebookEntry = my_cluster;
|
|
weight = 1.0;
|
|
weight = 1.0;
|
|
distance = mindist;
|
|
distance = mindist;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+void CodebookPrototypes::voteVA ( const NICE::Vector & feature, NICE::Vector & votes ) const
|
|
|
|
+{
|
|
|
|
+ votes.set( 0.0 );
|
|
|
|
+ //TODO
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void CodebookPrototypes::voteVA ( const NICE::Vector & feature, NICE::SparseVector & votes ) const
|
|
|
|
+{
|
|
|
|
+ votes.clear();
|
|
|
|
+ //TODO
|
|
|
|
+}
|
|
|
|
+
|
|
void CodebookPrototypes::add ( const Codebook *codebook )
|
|
void CodebookPrototypes::add ( const Codebook *codebook )
|
|
{
|
|
{
|
|
informativeMeasure.append ( codebook->getInformativeMeasure() );
|
|
informativeMeasure.append ( codebook->getInformativeMeasure() );
|
|
@@ -93,7 +123,7 @@ void CodebookPrototypes::add ( const Codebook *codebook )
|
|
assert ( codebookp != NULL );
|
|
assert ( codebookp != NULL );
|
|
insert ( begin(), codebookp->begin(), codebookp->end() );
|
|
insert ( begin(), codebookp->begin(), codebookp->end() );
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
Codebook *CodebookPrototypes::clone () const
|
|
Codebook *CodebookPrototypes::clone () const
|
|
{
|
|
{
|
|
return (new CodebookPrototypes());
|
|
return (new CodebookPrototypes());
|
|
@@ -109,6 +139,8 @@ void CodebookPrototypes::restore ( istream & is, int format )
|
|
{
|
|
{
|
|
Codebook::restore ( is, format );
|
|
Codebook::restore ( is, format );
|
|
VVector::restore ( is, format );
|
|
VVector::restore ( is, format );
|
|
|
|
+
|
|
|
|
+ std::cerr << "initial Codebook : " << std::endl; VVector::print ( std::cerr );
|
|
}
|
|
}
|
|
|
|
|
|
void CodebookPrototypes::store ( ostream & os, int format ) const
|
|
void CodebookPrototypes::store ( ostream & os, int format ) const
|
|
@@ -119,47 +151,57 @@ void CodebookPrototypes::store ( ostream & os, int format ) const
|
|
|
|
|
|
void CodebookPrototypes::displayCodebook ( int xsize, int ysize ) const
|
|
void CodebookPrototypes::displayCodebook ( int xsize, int ysize ) const
|
|
{
|
|
{
|
|
- NICE::Image bigimg ( xsize * 5 , ysize * size() );
|
|
|
|
|
|
+ NICE::Image bigimg ( xsize * 5 , ysize * this->size() );
|
|
bigimg.set(0);
|
|
bigimg.set(0);
|
|
|
|
|
|
NICE::Image img_s (xsize, ysize);
|
|
NICE::Image img_s (xsize, ysize);
|
|
- for ( int k = 0 ; k < (int)size() ; k++ )
|
|
|
|
|
|
+ for ( int k = 0 ; k < (int)this->size() ; k++ )
|
|
{
|
|
{
|
|
- NICE::Vector vimg = *(begin() + k);
|
|
|
|
- NICE::Image img ((int)sqrt(vimg.size()), (int)sqrt(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
|
|
|
|
- }
|
|
|
|
|
|
+ NICE::Vector vimg = *(this->begin() + k);
|
|
|
|
+ NICE::Image img ((int)sqrt(vimg.size()), (int)sqrt(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");
|
|
bigimg.write ("/tmp/display.bmp");
|