PSSLocalizationPrior.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file PSSLocalizationPrior.cpp
  3. * @brief incorporate prior from localization results
  4. * @author Erik Rodner
  5. * @date 03/19/2009
  6. */
  7. #ifdef NOVISUAL
  8. #include <objrec/nice_nonvis.h>
  9. #else
  10. #include <objrec/nice.h>
  11. #endif
  12. #include <iostream>
  13. #include <limits>
  14. #include "PSSLocalizationPrior.h"
  15. #include "objrec/baselib/StringTools.h"
  16. #include "objrec/baselib/Globals.h"
  17. #include "objrec/baselib/FileMgt.h"
  18. #include "objrec/cbaselib/PascalResults.h"
  19. using namespace OBJREC;
  20. using namespace std;
  21. // refactor-nice.pl: check this substitution
  22. // old: using namespace ice;
  23. using namespace NICE;
  24. // refactor-nice.pl: check this substitution
  25. // old: PSSLocalizationPrior::PSSLocalizationPrior( const string & detectiondir,
  26. PSSLocalizationPrior::PSSLocalizationPrior( const std::string & detectiondir,
  27. const ClassNames *classNames,
  28. double alphaDetectionPrior,
  29. int subsamplex, int subsampley )
  30. {
  31. this->subsamplex = subsampley;
  32. this->subsampley = subsamplex;
  33. this->alphaDetectionPrior = alphaDetectionPrior;
  34. loadDetectionResults ( detectiondir, detresults, classNames );
  35. }
  36. PSSLocalizationPrior::~PSSLocalizationPrior()
  37. {
  38. }
  39. // refactor-nice.pl: check this substitution
  40. // old: void PSSLocalizationPrior::loadDetectionResults ( const string & dir,
  41. void PSSLocalizationPrior::loadDetectionResults ( const std::string & dir,
  42. map<string, LocalizationResult *> & results,
  43. const ClassNames *classNames )
  44. {
  45. vector<string> files;
  46. FileMgt::DirectoryRecursive ( files, dir );
  47. int backgroundClassNo = classNames->getBackgroundClass();
  48. for ( vector<string>::const_iterator i = files.begin();
  49. i != files.end(); i++ )
  50. {
  51. // refactor-nice.pl: check this substitution
  52. // old: string file = *i;
  53. std::string file = *i;
  54. // refactor-nice.pl: check this substitution
  55. // old: string classtext = StringTools::baseName ( file, false );
  56. std::string classtext = StringTools::baseName ( file, false );
  57. int classno = classNames->classno(classtext);
  58. if ( classno < 0 ) {
  59. fprintf (stderr, "Unable to find class %s\n", classtext.c_str() );
  60. fprintf (stderr, "dir %s file %s classtext %s\n", dir.c_str(),
  61. file.c_str(), classtext.c_str() );
  62. }
  63. PascalResults::read ( results, file, classno, backgroundClassNo, true /*calibrate*/ );
  64. }
  65. }
  66. // refactor-nice.pl: check this substitution
  67. // old: void PSSLocalizationPrior::postprocess ( Image & result, GenericImage<double> & probabilities )
  68. void PSSLocalizationPrior::postprocess ( NICE::Image & result, GenericImage<double> & probabilities )
  69. {
  70. // refactor-nice.pl: check this substitution
  71. // old: string currentFilename = Globals::getCurrentImgFN();
  72. std::string currentFilename = Globals::getCurrentImgFN();
  73. // refactor-nice.pl: check this substitution
  74. // old: string base = StringTools::baseName ( currentFilename, false );
  75. std::string base = StringTools::baseName ( currentFilename, false );
  76. map<string, LocalizationResult *>::const_iterator i = detresults.find ( base );
  77. if ( i == detresults.end() )
  78. {
  79. fprintf (stderr, "NO detection results found for %s !\n", base.c_str());
  80. return;
  81. }
  82. fprintf (stderr, "Infering detection prior\n");
  83. LocalizationResult *ldet = i->second;
  84. int maxClassNo = probabilities.numChannels - 1;
  85. int xsize = probabilities.xsize;
  86. int ysize = probabilities.ysize;
  87. FullVector *priormap = new FullVector [ xsize * ysize ];
  88. for ( long k = 0 ; k < xsize * ysize ; k++ )
  89. priormap[k].reinit(maxClassNo);
  90. for ( LocalizationResult::const_iterator j = ldet->begin();
  91. j != ldet->end();
  92. j++ )
  93. {
  94. const SingleLocalizationResult *slr = *j;
  95. int xi, yi, xa, ya;
  96. const NICE::Region & r = slr->getRegion();
  97. int classno = slr->r->classno;
  98. double confidence = slr->r->confidence();
  99. r.getRect ( xi, yi, xa, ya );
  100. for ( int y = yi; y <= ya; y++ )
  101. for ( int x = xi; x <= xa; x++ )
  102. {
  103. if ( (y<0) || (x<0) || (x>xsize-1) || (y>ysize-1) )
  104. continue;
  105. if ( r.inside ( x*subsamplex, y*subsampley ) )
  106. priormap[x + y*xsize][classno] += confidence;
  107. }
  108. long k = 0;
  109. for ( int y = 0 ; y < ysize ; y++ )
  110. for ( int x = 0 ; x < xsize ; x++,k++ )
  111. {
  112. FullVector & prior = priormap[k];
  113. if ( prior.sum() < 10e-6 )
  114. continue;
  115. prior.normalize();
  116. double sum = 0.0;
  117. for ( int i = 0 ; i < (int)probabilities.numChannels; i++ )
  118. {
  119. probabilities.data[i][k] *= pow ( prior[i], alphaDetectionPrior );
  120. sum += probabilities.data[i][k];
  121. }
  122. if ( sum < 10e-6 )
  123. continue;
  124. int maxindex = 0;
  125. double maxvalue = - numeric_limits<double>::max();
  126. for ( int i = 0 ; i < (int)probabilities.numChannels; i++ )
  127. {
  128. probabilities.data[i][k] /= sum;
  129. if ( probabilities.data[i][k] > maxvalue )
  130. {
  131. maxindex = i;
  132. maxvalue = probabilities.data[i][k];
  133. }
  134. }
  135. result.setPixel(x,y,maxindex);
  136. }
  137. }
  138. delete [] priormap;
  139. }