RFBoV.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifdef NICE_USELIB_OPENMP
  2. #include <omp.h>
  3. #endif
  4. #include "vislearning/features/regionfeatures/RFBoV.h"
  5. #include <iostream>
  6. #include "core/basics/Timer.h"
  7. using namespace OBJREC;
  8. using namespace std;
  9. using namespace NICE;
  10. RFBoV::RFBoV( const Config *_conf, LocalFeature *_lf ):RegionFeatures(_conf)
  11. {
  12. lf = _lf;
  13. string sscales = conf->gS("SIFTTest", "scales", "1+2.0+3.0");
  14. grid = conf->gI("SIFTTest", "grid", 20);
  15. string::size_type pos = 0;
  16. string::size_type oldpos = 0;
  17. while(pos != string::npos)
  18. {
  19. pos = sscales.find("+", oldpos);
  20. string val;
  21. if(pos == string::npos)
  22. val = sscales.substr(oldpos);
  23. else
  24. val = sscales.substr(oldpos, pos-oldpos);
  25. double d = atof(val.c_str());
  26. scales.push_back(d);
  27. oldpos = pos+1;
  28. }
  29. gmm = NULL;
  30. pca = NULL;
  31. }
  32. void RFBoV::setPCA(PCA *_pca){
  33. pca = _pca;
  34. }
  35. void RFBoV::setGMM(GMM *_gmm){
  36. gmm = _gmm;
  37. }
  38. void RFBoV::extract ( const NICE::Image & img, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  39. {
  40. VVector positions, features;
  41. getPositions(rg, mask, positions);
  42. lf->getDescriptors(img, positions, features);
  43. assert(features.size() == positions.size());
  44. convert(mask, rg, features, feats, positions);
  45. }
  46. void RFBoV::extractRGB ( const NICE::ColorImage & cimg, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
  47. {
  48. assert(cimg.width() == (int)mask.rows());
  49. assert(cimg.height() == (int)mask.cols());
  50. VVector positions, features;
  51. getPositions(rg, mask, positions);
  52. lf->getDescriptors(cimg, positions, features);
  53. assert(features.size() == positions.size());
  54. convert(mask, rg, features, feats, positions);
  55. }
  56. void RFBoV::convert(const Matrix &mask, const RegionGraph &rg, VVector &infeats, VVector &outfeats, VVector &positions)
  57. {
  58. int fsize = gmm->getSize();
  59. for(int i = 0; i < (int)rg.size(); i++)
  60. {
  61. Vector v(fsize);
  62. v.set(0.0);
  63. outfeats.push_back(v);
  64. }
  65. vector<int> counter(rg.size(), 0);
  66. #if 0
  67. #pragma omp parallel for
  68. for(int i = 0; i < (int)infeats.size(); i++)
  69. {
  70. infeats[i] = pca->getFeatureVector ( infeats[i], true );
  71. SparseVector probs;
  72. gmm->getProbs(infeats[i], probs);
  73. int r = mask(positions[i][0], positions[i][1]);
  74. #pragma omp critical
  75. {
  76. for(int j = 0; j < fsize; j++)
  77. {
  78. outfeats[r][j] += probs.get(j);
  79. }
  80. counter[r]++;
  81. }
  82. }
  83. #else
  84. #pragma omp parallel for
  85. for(int i = 0; i < (int)infeats.size(); i++)
  86. {
  87. infeats[i] = pca->getFeatureVector ( infeats[i], true );
  88. SparseVector probs;
  89. //gmm->getProbs(infeats[i], probs);
  90. int bc = gmm->getBestClass(infeats[i]);
  91. int r = mask(positions[i][0], positions[i][1]);
  92. assert(r < (int) rg.size());
  93. #pragma omp critical
  94. {
  95. outfeats[r][bc]++;
  96. counter[r]++;
  97. }
  98. }
  99. #endif
  100. for(int i = 0; i < (int)rg.size(); i++)
  101. {
  102. outfeats[i] /= (double)counter[i];
  103. }
  104. }
  105. void RFBoV::getPositions(const RegionGraph &rg, const NICE::Matrix &mask, VVector &positions)
  106. {
  107. assert(rg.size() > 0);
  108. vector<int> featinreg(rg.size(), 0);
  109. int x0 = grid/2;
  110. for(int y = 0; y < (int)mask.cols(); y+=grid)
  111. {
  112. for(int x = x0; x < (int)mask.rows(); x+=grid)
  113. {
  114. int r = (int)mask(x,y);
  115. for(int s = 0; s < (int)scales.size(); s++)
  116. {
  117. featinreg[r]++;
  118. Vector vec(3);
  119. vec[0] = x;
  120. vec[1] = y;
  121. vec[2] = scales[s];
  122. positions.push_back(vec);
  123. }
  124. }
  125. if(x0 == 0)
  126. {
  127. x0 = grid/2;
  128. }
  129. else
  130. {
  131. x0 = 0;
  132. }
  133. }
  134. for(int i = 0; i < (int)featinreg.size(); i++)
  135. {
  136. if(featinreg[i] == 0)
  137. {
  138. int x, y;
  139. rg[i]->getCentroid(x,y);
  140. for(int s = 0; s < (int)scales.size(); s++)
  141. {
  142. Vector vec(3);
  143. vec[0] = x;
  144. vec[1] = y;
  145. vec[2] = scales[s];
  146. positions.push_back(vec);
  147. //FIXME: Achtung es kann in seltenen Fällen vorkommen, dass der Mittelpunkt einer Region nicht zur Region gehört -> abfangen
  148. }
  149. }
  150. }
  151. }
  152. RFBoV::~RFBoV()
  153. {
  154. }