123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /**
- * @file PSSLocalizationPrior.cpp
- * @brief incorporate prior from localization results
- * @author Erik Rodner
- * @date 03/19/2009
- */
- #ifdef NOVISUAL
- #include <objrec/nice_nonvis.h>
- #else
- #include <objrec/nice.h>
- #endif
- #include <iostream>
- #include <limits>
- #include "PSSLocalizationPrior.h"
- #include "objrec/baselib/StringTools.h"
- #include "objrec/baselib/Globals.h"
- #include "objrec/baselib/FileMgt.h"
- #include "objrec/cbaselib/PascalResults.h"
- using namespace OBJREC;
- using namespace std;
- // refactor-nice.pl: check this substitution
- // old: using namespace ice;
- using namespace NICE;
- // refactor-nice.pl: check this substitution
- // old: PSSLocalizationPrior::PSSLocalizationPrior( const string & detectiondir,
- PSSLocalizationPrior::PSSLocalizationPrior( const std::string & detectiondir,
- const ClassNames *classNames,
- double alphaDetectionPrior,
- int subsamplex, int subsampley )
- {
- this->subsamplex = subsampley;
- this->subsampley = subsamplex;
- this->alphaDetectionPrior = alphaDetectionPrior;
- loadDetectionResults ( detectiondir, detresults, classNames );
- }
- PSSLocalizationPrior::~PSSLocalizationPrior()
- {
- }
- // refactor-nice.pl: check this substitution
- // old: void PSSLocalizationPrior::loadDetectionResults ( const string & dir,
- void PSSLocalizationPrior::loadDetectionResults ( const std::string & dir,
- map<string, LocalizationResult *> & results,
- const ClassNames *classNames )
- {
- vector<string> files;
- FileMgt::DirectoryRecursive ( files, dir );
- int backgroundClassNo = classNames->getBackgroundClass();
- for ( vector<string>::const_iterator i = files.begin();
- i != files.end(); i++ )
- {
- // refactor-nice.pl: check this substitution
- // old: string file = *i;
- std::string file = *i;
- // refactor-nice.pl: check this substitution
- // old: string classtext = StringTools::baseName ( file, false );
- std::string classtext = StringTools::baseName ( file, false );
- int classno = classNames->classno(classtext);
- if ( classno < 0 ) {
- fprintf (stderr, "Unable to find class %s\n", classtext.c_str() );
- fprintf (stderr, "dir %s file %s classtext %s\n", dir.c_str(),
- file.c_str(), classtext.c_str() );
- }
- PascalResults::read ( results, file, classno, backgroundClassNo, true /*calibrate*/ );
- }
- }
-
- // refactor-nice.pl: check this substitution
- // old: void PSSLocalizationPrior::postprocess ( Image & result, GenericImage<double> & probabilities )
- void PSSLocalizationPrior::postprocess ( NICE::Image & result, GenericImage<double> & probabilities )
- {
- // refactor-nice.pl: check this substitution
- // old: string currentFilename = Globals::getCurrentImgFN();
- std::string currentFilename = Globals::getCurrentImgFN();
- // refactor-nice.pl: check this substitution
- // old: string base = StringTools::baseName ( currentFilename, false );
- std::string base = StringTools::baseName ( currentFilename, false );
- map<string, LocalizationResult *>::const_iterator i = detresults.find ( base );
- if ( i == detresults.end() )
- {
- fprintf (stderr, "NO detection results found for %s !\n", base.c_str());
- return;
- }
- fprintf (stderr, "Infering detection prior\n");
-
- LocalizationResult *ldet = i->second;
- int maxClassNo = probabilities.numChannels - 1;
- int xsize = probabilities.xsize;
- int ysize = probabilities.ysize;
- FullVector *priormap = new FullVector [ xsize * ysize ];
- for ( long k = 0 ; k < xsize * ysize ; k++ )
- priormap[k].reinit(maxClassNo);
- for ( LocalizationResult::const_iterator j = ldet->begin();
- j != ldet->end();
- j++ )
- {
- const SingleLocalizationResult *slr = *j;
- int xi, yi, xa, ya;
- const NICE::Region & r = slr->getRegion();
- int classno = slr->r->classno;
- double confidence = slr->r->confidence();
- r.getRect ( xi, yi, xa, ya );
- for ( int y = yi; y <= ya; y++ )
- for ( int x = xi; x <= xa; x++ )
- {
- if ( (y<0) || (x<0) || (x>xsize-1) || (y>ysize-1) )
- continue;
- if ( r.inside ( x*subsamplex, y*subsampley ) )
- priormap[x + y*xsize][classno] += confidence;
- }
- long k = 0;
- for ( int y = 0 ; y < ysize ; y++ )
- for ( int x = 0 ; x < xsize ; x++,k++ )
- {
- FullVector & prior = priormap[k];
- if ( prior.sum() < 10e-6 )
- continue;
- prior.normalize();
- double sum = 0.0;
- for ( int i = 0 ; i < (int)probabilities.numChannels; i++ )
- {
- probabilities.data[i][k] *= pow ( prior[i], alphaDetectionPrior );
- sum += probabilities.data[i][k];
- }
- if ( sum < 10e-6 )
- continue;
- int maxindex = 0;
- double maxvalue = - numeric_limits<double>::max();
- for ( int i = 0 ; i < (int)probabilities.numChannels; i++ )
- {
- probabilities.data[i][k] /= sum;
- if ( probabilities.data[i][k] > maxvalue )
- {
- maxindex = i;
- maxvalue = probabilities.data[i][k];
- }
- }
- result.setPixel(x,y,maxindex);
- }
-
- }
- delete [] priormap;
- }
|