123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #endif
- #include "vislearning/features/regionfeatures/RFBoV.h"
- #include <iostream>
- #include "core/basics/Timer.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- RFBoV::RFBoV( const Config *_conf, LocalFeature *_lf ):RegionFeatures(_conf)
- {
- lf = _lf;
- string sscales = conf->gS("SIFTTest", "scales", "1+2.0+3.0");
- grid = conf->gI("SIFTTest", "grid", 20);
- string::size_type pos = 0;
- string::size_type oldpos = 0;
- while(pos != string::npos)
- {
- pos = sscales.find("+", oldpos);
- string val;
- if(pos == string::npos)
- val = sscales.substr(oldpos);
- else
- val = sscales.substr(oldpos, pos-oldpos);
- double d = atof(val.c_str());
- scales.push_back(d);
- oldpos = pos+1;
- }
- gmm = NULL;
- pca = NULL;
- }
- void RFBoV::setPCA(PCA *_pca){
- pca = _pca;
- }
- void RFBoV::setGMM(GMM *_gmm){
- gmm = _gmm;
- }
- void RFBoV::extract ( const NICE::Image & img, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
- {
- VVector positions, features;
- getPositions(rg, mask, positions);
- lf->getDescriptors(img, positions, features);
- assert(features.size() == positions.size());
- convert(mask, rg, features, feats, positions);
- }
- void RFBoV::extractRGB ( const NICE::ColorImage & cimg, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
- {
- assert(cimg.width() == (int)mask.rows());
- assert(cimg.height() == (int)mask.cols());
- VVector positions, features;
- getPositions(rg, mask, positions);
- lf->getDescriptors(cimg, positions, features);
- assert(features.size() == positions.size());
- convert(mask, rg, features, feats, positions);
- }
- void RFBoV::convert(const Matrix &mask, const RegionGraph &rg, VVector &infeats, VVector &outfeats, VVector &positions)
- {
- int fsize = gmm->getSize();
- for(int i = 0; i < (int)rg.size(); i++)
- {
- Vector v(fsize);
- v.set(0.0);
- outfeats.push_back(v);
- }
- vector<int> counter(rg.size(), 0);
-
- #if 0
- #pragma omp parallel for
- for(int i = 0; i < (int)infeats.size(); i++)
- {
- infeats[i] = pca->getFeatureVector ( infeats[i], true );
- SparseVector probs;
- gmm->getProbs(infeats[i], probs);
- int r = mask(positions[i][0], positions[i][1]);
- #pragma omp critical
- {
- for(int j = 0; j < fsize; j++)
- {
- outfeats[r][j] += probs.get(j);
- }
- counter[r]++;
- }
- }
- #else
- #pragma omp parallel for
- for(int i = 0; i < (int)infeats.size(); i++)
- {
-
- infeats[i] = pca->getFeatureVector ( infeats[i], true );
- SparseVector probs;
- //gmm->getProbs(infeats[i], probs);
- int bc = gmm->getBestClass(infeats[i]);
- int r = mask(positions[i][0], positions[i][1]);
- assert(r < (int) rg.size());
- #pragma omp critical
- {
- outfeats[r][bc]++;
- counter[r]++;
- }
- }
- #endif
- for(int i = 0; i < (int)rg.size(); i++)
- {
- outfeats[i] /= (double)counter[i];
- }
- }
- void RFBoV::getPositions(const RegionGraph &rg, const NICE::Matrix &mask, VVector &positions)
- {
- assert(rg.size() > 0);
- vector<int> featinreg(rg.size(), 0);
- int x0 = grid/2;
- for(int y = 0; y < (int)mask.cols(); y+=grid)
- {
- for(int x = x0; x < (int)mask.rows(); x+=grid)
- {
- int r = (int)mask(x,y);
- for(int s = 0; s < (int)scales.size(); s++)
- {
- featinreg[r]++;
- Vector vec(3);
- vec[0] = x;
- vec[1] = y;
- vec[2] = scales[s];
- positions.push_back(vec);
- }
- }
- if(x0 == 0)
- {
- x0 = grid/2;
- }
- else
- {
- x0 = 0;
- }
- }
- for(int i = 0; i < (int)featinreg.size(); i++)
- {
- if(featinreg[i] == 0)
- {
- int x, y;
- rg[i]->getCentroid(x,y);
- for(int s = 0; s < (int)scales.size(); s++)
- {
- Vector vec(3);
- vec[0] = x;
- vec[1] = y;
- vec[2] = scales[s];
- positions.push_back(vec);
- //FIXME: Achtung es kann in seltenen Fällen vorkommen, dass der Mittelpunkt einer Region nicht zur Region gehört -> abfangen
- }
- }
- }
- }
- RFBoV::~RFBoV()
- {
- }
|