123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /**
- * @file Codebook.cpp
- * @brief feature codebook
- * @author Erik Rodner, Alexander Freytag
- * @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
- */
- #include <iostream>
- #include "vislearning/features/simplefeatures/Codebook.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- Codebook::Codebook ( )
- {
- this->i_noOfNearestClustersToConsidere = 1;
- }
- Codebook::Codebook ( const int & _noOfNearestClustersToConsidere )
- {
- this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
- }
- Codebook::Codebook( NICE::Config * _conf, const std::string & _section)
- {
- this->p_conf = _conf;
- this->s_section = _section;
-
- this->i_noOfNearestClustersToConsidere = this->p_conf->gI( _section, "noOfNearestClustersToConsidere", 1);
- }
- Codebook::~Codebook ( )
- {
- //NOTE the config does not need to be deleted, it is just a pointer to an external data structure
- }
- void Codebook::voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int &codebookEntry, double &weight, double &distance) const
- {
- this->voteVQ ( feature, codebookEntry, weight, distance );
- //VQ, but we directly increase the corresponding entry without setting histogram to zero before (useful if we work on multiple images)
- histogram[codebookEntry] += weight;
- }
- void Codebook::voteVQ ( const NICE::Vector & feature, NICE::SparseVector & histogram, int &codebookEntry, double &weight, double &distance ) const
- {
- this->voteVQ ( feature, codebookEntry, weight, distance );
-
- //is this cluster already non-zero in the histogram?
- NICE::SparseVector::iterator entryIt = histogram.find( codebookEntry ) ;
- if ( entryIt == histogram.end() )
- {
- //entry does not exist
- histogram.insert ( histogram.begin(), pair<int, double> ( codebookEntry, weight ) );
- }
- else
- {
- //entry already exists, so we increase the weight
- entryIt->second += weight;
- }
- }
- bool Codebook::allowsMultipleVoting () const
- {
- if ( this->i_noOfNearestClustersToConsidere > 1 )
- return true;
- else
- return false;
- }
- void Codebook::reinit ( int numCodebookEntries )
- {
- thresholds.resize ( numCodebookEntries );
- thresholds.set(0.0);
- informativeMeasure.resize ( numCodebookEntries );
- informativeMeasure.set(0.0);
- classnos.resize ( numCodebookEntries );
- classnos.resize(0);
- }
- void Codebook::clear ()
- {
- thresholds.clear();
- informativeMeasure.clear();
- classnos.clear();
- }
- void Codebook::restore ( istream & is, int format )
- {
- is >> thresholds;
- is >> informativeMeasure;
- // is >> classnos;
-
- //TODO use a flag for compatibility with old systems?
- /* try
- {
-
- is >> i_noOfNearestClustersToConsidere;
- is >> s_section;
- this->p_conf->restore( is, format );
- }
- catch( ... )
- {
- std::cerr << "something went wrong while Loading the codebook - use default values instead" << std::endl;
- //TODO use suitable default values
- } */
- }
- void Codebook::store ( ostream & os, int format ) const
- {
- os << this->thresholds << endl;
- os << this->informativeMeasure << endl;
- os << this->classnos << endl;
- // os << this->i_noOfNearestClustersToConsidere << endl;
- // os << this->s_section << endl;
- //
- // this->p_conf->store( os, format );
- }
- void Codebook::copy ( const Codebook *codebook )
- {
- this->reinit ( codebook->thresholds.size() );
- thresholds = codebook->thresholds;
- informativeMeasure = codebook->informativeMeasure;
- classnos = codebook->classnos;
- this->p_conf = codebook->p_conf;
- this->setNoOfNearestClustersToConsidere ( codebook->getNoOfNearestClustersToConsidere() );
- }
- void Codebook::setNoOfNearestClustersToConsidere ( const int & _noOfNearestClustersToConsidere )
- {
- this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
- }
- int Codebook::getNoOfNearestClustersToConsidere ( ) const
- {
- return this->i_noOfNearestClustersToConsidere;
- }
- void Codebook::setHardAssignment( const bool & _hardAssignment )
- {
- this->b_hardAssignment = _hardAssignment;
- }
- bool Codebook::getHardAssignment ( ) const
- {
- return this->b_hardAssignment;
- }
|