void add(ImageFeature &a, const ImageFeature &b, const uint z) { for(uint x=0;x<a.xsize();++x) { for(uint y=0;y<a.ysize();++y) { a(x,y,z)+=b(x,y,0); } } }
ImageFeature fft(const ImageFeature &img) { ImageFeature result(img.xsize(),img.ysize(),2); #ifdef HAVE_FFT_LIBRARY fftw_complex FIMG[img.xsize()][img.ysize()]; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { FIMG[x][y].re=img(x,y,0); FIMG[x][y].im=0; } } fftwnd_plan plan = fftw2d_create_plan(img.xsize(),img.ysize(), FFTW_FORWARD, FFTW_ESTIMATE | FFTW_IN_PLACE); fftwnd_one(plan,&FIMG[0][0],NULL); fftwnd_destroy_plan(plan); for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { result(x,y,0)=FIMG[x][y].re; result(x,y,1)=FIMG[x][y].im; } } #else #warning "FFT is used without FFT library enabled." ERR << "FFT not available, thus returning empty image instead of fourier transformed version. All that comes now is probably bogus." << endl; #endif return result; }
vector<double> toVector(const ImageFeature &image) { vector<double> result(image.size()); for(uint i=0;i<image.size();++i) { result[i]=image[i]; } return result; }
void shift(ImageFeature &img, const double offset) { for(uint c=0;c<img.zsize();++c) { for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,c)+=offset; }}} }
//default values for fact_eq and offset are set in imagelib.hpp.... by paredes void equalize(ImageFeature &image, uint wndsize, double bnd, uint c, double fact_eq, uint offset) { double fact_orig=1.0-fact_eq; int xdim=image.xsize(); int ydim=image.ysize(); int i,j,xini,xend,yini,yend; ImageFeature acu(xdim,ydim,1); ImageFeature divisor(xdim,ydim,1); for(i=0; i < int(ydim-wndsize); i+=offset) { yini = i; yend = i+wndsize; for(j=0; j < int(xdim-wndsize); j+=offset) { xini = j; xend = j+wndsize; eq_histo(xini,xend,yini,yend,image,c,acu,divisor); } } for(j=0; j < ydim; j++) { for(i=0; i < xdim; i++) { if ( (image(i,j,c) > bnd) && (divisor(i,j,0) > 0.0) ) { image(i,j,c)=fact_eq*(acu(i,j,0)/divisor(i,j,0)) + fact_orig*image(i,j,c); // weighted average between original and equalized image } } } }
void LocalFeatureExtractor::extractPatches(ImageFeature &img, const ::std::vector<FeatureExtractionPosition> &positions, LocalFeatures &lf) { if(settings_.padding>0) { img=zeropad(img,settings_.padding, settings_.padding); } lf.winsize_=settings_.winsize; lf.padding_=settings_.padding; lf.numberOfFeatures_=positions.size(); lf.zsize_=img.zsize(); int windiameter=2*settings_.winsize+1; lf.dim_=windiameter*windiameter*img.zsize(); lf.imageSizeX_=img.xsize(); lf.imageSizeY_=img.ysize(); lf.filename_=img.filename(); int savesize=settings_.winsize*2+1; lf.positions_=positions; lf.data_.resize(positions.size()); for(uint i=0;i<positions.size();++i) { const FeatureExtractionPosition &pos=positions[i]; ImageFeature p=getPatch(img,pos.x,pos.y,pos.s); if(uint(pos.s)!=settings_.winsize) { p=scale(p,savesize,savesize); } lf.data_[i]=toVector(p); } }
double efficientLocalMean(const int x,const int y,const int k,const ImageFeature &laufendeSumme) { int k2=k/2; int dimx=laufendeSumme.xsize(); int dimy=laufendeSumme.ysize(); //wanting average over area: (y-k2,x-k2) ... (y+k2-1, x+k2-1) int starty=y-k2; int startx=x-k2; int stopy=y+k2-1; int stopx=x+k2-1; if(starty<0) starty=0; if(startx<0) startx=0; if(stopx>dimx-1) stopx=dimx-1; if(stopy>dimy-1) stopy=dimy-1; double unten, links, oben, obenlinks; if(startx-1<0) links=0; else links=laufendeSumme(startx-1,stopy,0); if(starty-1<0) oben=0; else oben=laufendeSumme(stopx,starty-1,0); if((starty-1 < 0) || (startx-1 <0)) obenlinks=0; else obenlinks=laufendeSumme(startx-1,starty-1,0); unten=laufendeSumme(stopx,stopy,0); // cout << "obenlinks=" << obenlinks << " oben=" << oben << " links=" << links << " unten=" <<unten << endl; int counter=(stopy-starty+1)*(stopx-startx+1); return (unten-links-oben+obenlinks)/counter; }
void gammaCorrection(ImageFeature &img, double gamma) { for(uint c=0;c<img.zsize();++c) { for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,c)=exp(gamma*log(img(x,y,c))); } } } }
void absolute(ImageFeature &img1) { for (uint z = 0; z < img1.zsize(); z++) for (uint x = 0; x < img1.xsize(); x++) for (uint y = 0; y < img1.ysize(); y++) { if (img1(x, y, z) < 0.0) { img1(x, y, z) = -img1(x, y, z); } } }
void divide(ImageFeature &a, const double &d) { for(uint x=0;x<a.xsize();++x) { for(uint y=0;y<a.ysize();++y) { for(uint z=0;z<a.zsize();++z) { a(x,y,z)/=d; } } } }
void multiply(ImageFeature &img, const double s) { for(uint z=0;z<img.zsize();++z) { for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,z)*=s; } } } }
void rect(ImageFeature &img, const uint x, const uint y, const uint w, const uint h, const ::std::vector<double> &color, bool dash) { uint dashCounterMax = 3; uint dashCounter = dashCounterMax; uint dashMode = 1; // upper line for(uint i = x; i < x + w; ++i) { dashCounter--; if (dashCounter == 0) { dashCounter = dashCounterMax; dashMode = 1 - dashMode; } for(uint c = 0; c < img.zsize(); ++c) { if (!dash || (dashMode == 1)) { img(i, y, c) = color[c]; } } } // right line for(uint i = y; i < y + h; ++i) { dashCounter--; if (dashCounter == 0) { dashCounter = dashCounterMax; dashMode = 1 - dashMode; } for(uint c = 0; c < img.zsize(); ++c) { if (!dash || (dashMode == 1)) { img(x + w - 1, i, c) = color[c]; } } } // lower line for(int i = x + w - 1; i >= (int) x; --i) { dashCounter--; if (dashCounter == 0) { dashCounter = dashCounterMax; dashMode = 1 - dashMode; } for(uint c = 0; c < img.zsize(); ++c) { if (!dash || (dashMode == 1)) { img(i, y + h - 1, c)=color[c]; } } } // left line for(int i = y + h - 1; i >= (int) y; --i) { dashCounter--; if (dashCounter == 0) { dashCounter = dashCounterMax; dashMode = 1 - dashMode; } for(uint c = 0; c < img.zsize(); ++c) { if (!dash || (dashMode == 1)) { img(x, i, c) = color[c]; } } } }
ImageFeature DifferenceOfGaussian::difference(const ImageFeature& img1, const ImageFeature& img2) { ImageFeature newImage = ImageFeature(img1.xsize(), img1.ysize(), 1); for (int x = 0; x < (int) newImage.xsize(); x++) { for (int y = 0; y < (int) newImage.ysize(); y++) { newImage(x, y, 0) = img1(x, y, 0) - img2(x, y, 0); } } return newImage; }
double maximum(const ImageFeature &img, uint c) { double max=-1.0; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { max=::std::max(max,img(x,y,c)); } } return max; }
double minimum(const ImageFeature &img, uint c) { double min=numeric_limits<double>::max(); for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { min=::std::min(min,img(x,y,c)); } } return min; }
void cutoff(ImageFeature &img, const double minimum, const double maximum) { for(uint c=0;c<img.zsize();++c) { for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,c)=max(minimum,img(x,y,c)); img(x,y,c)=min(maximum,img(x,y,c)); } } } }
ImageFeature localvariance(const ImageFeature &image, const uint winsize) { ImageFeature result(image.xsize(),image.ysize(),1); for(uint x=winsize;x<image.xsize()-winsize;++x) { for(uint y=winsize;y<image.ysize()-winsize;++y) { result(x,y,0)=localvariance(image, winsize, x,y); } } return result; }
void contrast(ImageFeature &img,const double min, const double max) { double m=max-min; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { for(uint z=0;z<img.zsize();++z) { img(x,y,z)=img(x,y,z)*m+min; } } } }
void diagcross(ImageFeature &img, const uint x, const uint y,const vector<double>& color,const uint len) { //mark center if (inImage(img,x,y)) {for(uint c=0;c<img.zsize();++c) {img(x,y,c)=color[c];}} //mark outer pars for(uint i=1;i<=len;++i) { if(inImage(img,x+i,y+i)) {for(uint c=0;c<img.zsize();++c) {img(x+i,y+i,c)=color[c];}} if(inImage(img,x-i,y-i)) {for(uint c=0;c<img.zsize();++c) {img(x-i,y-i,c)=color[c];}} if(inImage(img,x-i,y+i)) {for(uint c=0;c<img.zsize();++c) {img(x-i,y+i,c)=color[c];}} if(inImage(img,x+i,y-i)) {for(uint c=0;c<img.zsize();++c) {img(x+i,y-i,c)=color[c];}} } }
void box(ImageFeature &img, const uint centerx, const uint centery, const vector<double> &color, const uint winsize) { for(uint x=centerx-winsize;x<=centerx+winsize;++x) { if(inImage(img,x,centery+winsize)) {for(uint c=0;c<img.zsize();++c) {img(x,centery+winsize,c)=color[c];}} if(inImage(img,x,centery-winsize)) {for(uint c=0;c<img.zsize();++c) {img(x,centery-winsize,c)=color[c];}} } for(uint y=centery-winsize;y<=centery+winsize;++y) { if(inImage(img,centerx+winsize,y)) {for(uint c=0;c<img.zsize();++c) {img(centerx+winsize,y,c)=color[c];}} if(inImage(img,centerx-winsize,y)) {for(uint c=0;c<img.zsize();++c) {img(centerx-winsize,y,c)=color[c];}} } }
void RGBtoHSV(ImageFeature &img) { HSVPixel hsv; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { hsv=img.hsvPixel(x,y); img(x,y,0)=hsv.h; img(x,y,1)=hsv.s; img(x,y,2)=hsv.v; } } }
void power(ImageFeature &img1, int p) { for (uint z = 0; z < img1.zsize(); z++) for (uint x = 0; x < img1.xsize(); x++) for (uint y = 0; y < img1.ysize(); y++) { double tmp = 1.0; double val = img1(x, y, z); for (int t = 0; t < p; t++) { tmp *= val; } img1(x, y, z) = tmp; } }
void DifferenceOfGaussian::convolution(ImageFeature& image, double sigma) { ImageFeature hMask = makeFilter(sigma); // horizontal convolution convolve(image, hMask); // vertical convolution ImageFeature vMask(1, hMask.xsize(), 1); for (int y = 0; y < (int) vMask.ysize(); y++) { vMask(0, y, 0) = hMask(y, 0, 0); } convolve(image, vMask); }
::std::pair<uint,uint> argmax(const ImageFeature &img,uint c) { pair<uint,uint> res; double max=0.0; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { if(max<img(x,y,c)) { max=img(x,y,c); res.first=x; res.second=y; } } } return res; }
::std::pair<uint,uint> argmin(const ImageFeature &img,uint c) { pair<uint,uint> res; double min=numeric_limits<double>::max(); for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { if(min>img(x,y,c)) { min=img(x,y,c); res.first=x; res.second=y; } } } return res; }
double energImg(const ImageFeature &img, const uint x, const uint y,const uint w,const uint h,const vector<double>& mean) { double sum=.0; double im; for(uint i=x;i<x+w;++i) { for(uint j=y;j<y+h;++j) { if(j<img.ysize() && i<img.xsize()) im=img(i,j,0); else im=0.0; double tmp=im-mean[(j-y)*w+(i-x)]; sum+=tmp*tmp; } } return sum; }
int main(int argc, char** argv) { GetPot cl(argc,argv); //command line parsing if(cl.search(2,"--help","-h")) USAGE(); if(cl.size()<2) USAGE(); string suffix=cl.follow("gtf.vec.gz","--suffix"); //get list of files to be processed vector<string> infiles; if(cl.search("--images")) { string filename=cl.next(" ");; while(filename!=" ") { infiles.push_back(filename); filename=cl.next(" "); } } else if (cl.search("--filelist")) { string filename="test"; igzstream ifs; ifs.open(cl.follow("list","--filelist")); if(!ifs.good() || !ifs) { ERR << "Cannot open filelist " <<cl.follow("list","--filelist") << ". Aborting." << endl; exit(20); } while(!ifs.eof() && filename!="") { getline(ifs,filename); if(filename!="") { infiles.push_back(filename); } } ifs.close(); } else { USAGE(); exit(20); } ImageFeature img; VectorFeature vec; // processing the files for(uint i=0;i<infiles.size();++i) { string filename=infiles[i]; DBG(10) << "Processing '" << filename << "' (" << i+1<< "/" << infiles.size() << ")." << endl; img.load(filename); vec=getGlobalTextureFeature(img); vec.save(filename+"."+suffix); DBG(20) << "Finished with '" << filename << "'." << endl; } DBG(10) << "cmdline was: "; printCmdline(argc,argv); }
void signedPower(ImageFeature &img1, int p) { for (uint z = 0; z < img1.zsize(); z++) for (uint x = 0; x < img1.xsize(); x++) for (uint y = 0; y < img1.ysize(); y++) { double tmp = 1.0; for (int t = 0; t < p; t++) { tmp *= img1(x, y, z); } if ((img1(x, y, z) < 0.0) && ((p % 2) == 0)) { tmp *= -1.0; } img1(x, y, z) = tmp; } }
void meanAndVarianceNormalization(ImageFeature &img, const double mu, const double sigma) { double mean, variance,stddev; for(uint c=0;c<img.zsize();++c) { meanandvariance(img,mean,variance,c); mean+=mu; stddev=sqrt(variance); stddev/=sqrt(sigma); for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,c)=(img(x,y,c)-mean)/stddev; } } } }
// default for type is set in imagelib.hpp // type = 0: maximum (as in HSV) // type = 1: mean // type = 2: luminance (use only for RGB images!) ImageFeature makeGray(const ImageFeature &img, const uint type) { ImageFeature result(img.xsize(), img.ysize(),1) ; double value; if( (type == 2) && (img.zsize() != 3) ) { ERR << "Image has invalid color resolution!" << endl; return result; } for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { value=0.0; switch(type){ case 0: //max for(uint c=0;c<img.zsize();++c) { value=::std::max(value,img(x,y,c)); } break; case 1: //mean for(uint c=0;c<img.zsize();++c) { value+=img(x,y,c); } value/=img.zsize(); break; case 2: // luminance value = 0.3 * img(x,y,0) + 0.59 * img(x,y,1) + 0.11 * img(x,y,2); break; } result(x,y,0)=value; } } return result; }