123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #ifdef NICE_USELIB_OPENMP
- #include <omp.h>
- #endif
- #include "vislearning/features/regionfeatures/RFStruct.h"
- #include <iostream>
- #include "vislearning/baselib/FastFilter.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- RFStruct::RFStruct( const Config *_conf):RegionFeatures(_conf)
- {
- numBins = conf->gI( "RFStruct", "numbins", 8 );
- usesigned = conf->gB( "RFStruct", "usesigned", false );
- }
- void RFStruct::extract ( const NICE::Image & img, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
- {
- const unsigned char *imgdraw = img.getPixelPointerXY(0,0);
- float *gradient = new float[ img.width() * img.height() ];
- unsigned char *graddir = new unsigned char[ img.width() * img.height() ];
-
- FastFilter::calcGradient<const unsigned char,float,unsigned char> ( imgdraw, img.width(), img.height(), gradient, graddir, numBins, usesigned );
- NICE::FloatImage imgd_gradient(gradient, img.width(), img.height(), GrayColorImageCommonImplementation::noAlignment);
- NICE::Image imgd_graddir(graddir, img.width(), img.height(), GrayColorImageCommonImplementation::noAlignment);
- /*
- Image tmp;
- floatToGray(imgd_gradient, &tmp);
- showImage(tmp, "Grad");*/
-
- extract ( imgd_gradient, imgd_graddir, rg, mask, feats );
-
- delete [] gradient;
- delete [] graddir;
- }
- void RFStruct::extractRGB ( const NICE::ColorImage & cimg, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
- {
- unsigned char *imgdraw[3];
- for(int i = 0; i < 3; i++)
- {
- imgdraw[i] = new unsigned char[ cimg.width() * cimg.height() ];
- long int k = 0;
- for(int y = 0; y < cimg.height(); y++)
- {
- for(int x = 0; x < cimg.width(); x++, k++)
- {
- imgdraw[i][k] = cimg.getPixel(x,y,i);
- }
- }
- }
- float *gradient = new float[ cimg.width() * cimg.height() ];
- unsigned char *graddir = new unsigned char[ cimg.width() * cimg.height() ];
-
- FastFilter::calcColorGradient<unsigned char,float,unsigned char> ( imgdraw[0],imgdraw[1],imgdraw[2], cimg.width(), cimg.height(), gradient, graddir, numBins, usesigned );
- NICE::FloatImage imgd_gradient(gradient, cimg.width(), cimg.height(), GrayColorImageCommonImplementation::noAlignment);
- NICE::Image imgd_graddir(graddir, cimg.width(), cimg.height(), GrayColorImageCommonImplementation::noAlignment);
- extract ( imgd_gradient, imgd_graddir, rg, mask, feats );
-
- for(int i = 0; i < 3; i++)
- {
- delete [] imgdraw[i];
- }
- delete [] gradient;
- delete [] graddir;
- }
- void RFStruct::extract ( const NICE::FloatImage &imgd_gradient, const NICE::Image &imgd_graddir, const RegionGraph &rg, const NICE::Matrix &mask, VVector & feats )
- {
- int rgsize = rg.size();
- feats.clear();
- for(int r = 0; r < rgsize; r++)
- {
- Vector f(numBins);
- f.set(0.0);
- feats.push_back(f);
- }
-
- #pragma omp parallel for
- for(int r = 0; r < rgsize; r++)
- {
- // ***** 6.3 Spatial/Orientation binning *****
- int x0,y0,x1,y1;
- rg[r]->getRect(x0,y0,x1,y1);
-
- for(int y = y0; y <= y1; y++)
- {
- for(int x = x0; x <= x1; x++)
- {
- int reg = mask(x, y);
- if(reg != r)
- continue;
-
- double gradient = imgd_gradient.getPixel(x, y);
- int graddir = imgd_graddir.getPixel(x, y);
- feats[r][graddir] += gradient;
- }
- }
-
- double sum = 0.0;
- for(int j = 0; j < (int) feats[r].size(); j++)
- {
- sum += feats[r][j];
- }
- for(int j = 0; j < (int) feats[r].size(); j++)
- {
- feats[r][j] /= sum;
- }
- }
- }
- RFStruct::~RFStruct()
- {
-
- }
|