www.pudn.com > firev0.01.rar > gabor.cpp


/*
This file is part of the FIRE -- Flexible Image Retrieval System

FIRE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FIRE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FIRE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/**
 * @file   gaborize.cpp
 * @author Thomas Deselaers
 * @date   Mon May 12 15:07:04 2003
 * 
 * @brief Methods used to generate gabor features for texture analysis
 * of images.
 * 
 */
#include 
#include 
#include 
#include "getpot.hpp"
#include "image.hpp"
#include "hdimage.hpp"
#include "rgbimage.hpp"
#include "diag.hpp"

#ifdef __HAVE_NUMERICAL_RECIPES__
#include "fourn.hpp"
#endif
#include "gaussian.hpp"
#include "imagetools.hpp"
#include "gzstream.hpp"
using namespace img;
using namespace std;

#include "gabor.hpp"


vector* > loadGaborFeatures(string filename, bool withcoords) {
  igzstream ifs(filename.c_str());
  vector* > result;
  vector* feature;
  int pixeldim, winsize,y,x;
  int nof;
  bool withcoord=withcoords;
  double threshold;
  if(!ifs) {
    ERR << "[gabor.cpp::loadGaborFeatures] Cannot open '" << filename << "' to read gabor features." << endl;
    return result;
  } else {
    string keyword, tmp;
    while(!ifs.eof()) {
      ifs >> keyword;
      if(!ifs.eof()) {
        if(keyword=="all") {
          char devnull[1024];
          ifs.getline(devnull,1024);
        } else if (keyword=="dim") {
          ifs >> pixeldim;
        } else if (keyword=="nOfFeatures") {
          ifs >> nof;
        } else if (keyword=="withoutcoord") {
          withcoord=false;
        } else if (keyword=="threshold") {
          ifs >>threshold;
        } else if (keyword=="winsize") {
          ifs >> winsize;
          pixeldim=2*winsize+1;
          pixeldim*=pixeldim;
        } else if (keyword=="#") {
          //comment line, skip
          char devnull[1024];
          ifs.getline(devnull,1024);
        } else if (keyword=="feature") {
          //read a feature
          if(withcoord) {
            ifs >> y >> x;
          }
          feature=new ::std::vector(pixeldim);
          for(int i=0;i> (*feature)[i];
          }
          result.push_back(feature);
        } else {
          cout << "[gabor.cpp::loadGaborFeatures] Unknown keyword: '" << keyword << "'. Skipping rest of line" << endl;
          char devnull[1024];
          ifs.getline(devnull,1024);
        }
      }
    }
    return result;
  }
}

/**
 * save the gabor features from an image into a text file.
 * @param image the HDImage containing the gaborfeatures.
 * @param filename the filename of the file, where the features shall be saved.
 *
 */ 
int saveGaborFeatures(HDImage image, string filename) {
  ofstream ofs(filename.c_str());
  if(!ofs) {
    ERR << "Cannot save gabor features to '"< & inImage, const HDImage &decomp,double threshold, unsigned int winsize) {
  ofstream ofs(filename.c_str());
  if(!ofs) {
    ERR << "Cannot save gabor features to '"< image=padding(inImage,dimh,dimw);
  
  ofs << "# gaborfeatures " << endl
      << "# posy posx [data1... datan]" << endl
      << "all false" << endl
      << "threshold " << threshold << endl
      << "winsize " << winsize << endl
      << "dim " << decomp.pixelDim() << endl;
  int counter=0;

  cout << dimh << " " << dimw << endl;

  for(unsigned int y=0;ythreshold) {
        ++counter;
        ofs <<"feature " << y << " " << x << " ";
        for(unsigned int c=0;c & inImage, const HDImage &decomp,int nOfFeatures, unsigned int winsize) {
  ofstream ofs(filename.c_str());
  if(!ofs) {
    ERR << "Cannot save gabor features to '"< image=padding(inImage,dimh,dimw);
  
  ofs << "# gaborfeatures " << endl
      << "# posy posx [data1... datan]" << endl
      << "all false" << endl
      << "nOfFeatures " < > > toSort;
  
  for(unsigned int y=0;y >(lv,pair(y,x)));
    }
  }
  sort(toSort.begin(),toSort.end());
  
  for(int i=0;i & inImage, const HDImage &decomp,double threshold, unsigned int winsize) {
  ofstream ofs(filename.c_str());
  if(!ofs) {
    ERR << "Cannot save gabor features to '"< image=padding(inImage,dimh,dimw);
  
  ofs << "# gaborfeatures " << endl
      << "# posy posx [data1... datan]" << endl
      << "all false" << endl
      << "threshold " << threshold << endl
      << "winsize " << winsize << endl
      << "dim " << decomp.pixelDim() << endl;
  int counter=0;

  cout << dimh << " " << dimw << endl;

  for(unsigned int y=0;ythreshold) {
        ++counter;
        ofs <<"feature "  << y << " " << x << " ";
        for(unsigned int c=0;c & inImage, const HDImage &decomp,int nOfFeatures, unsigned int winsize) {
  ofstream ofs(filename.c_str());
  if(!ofs) {
    ERR << "Cannot save gabor features to '"< image=padding(inImage,dimh,dimw);
  
  ofs << "# gaborfeatures " << endl
      << "# posy posx [data1... datan]" << endl
      << "all false" << endl
      << "nOfFeatures " << nOfFeatures << endl
      << "winsize " << winsize << endl
      << "dim " << decomp.pixelDim() << endl;
  int counter=0;

  cout << dimh << " " << dimw << endl;

  vector< pair > > toSort;

  for(unsigned int y=0;y >(lv,pair(y,x)));
    }
  }
  sort(toSort.begin(),toSort.end());
  
  for(int i=0;i &result , const vector &VtoTransform,const unsigned int height,const unsigned int width, const unsigned int maxnn, const unsigned  int filterno) {
  

  int xoffset=maxnn/2-width/2;
  int yoffset=maxnn/2-height/2;

  for(unsigned int y=0;y &result,const vector &HStoTransform, const vector &VtoTransform,const unsigned int height,const unsigned int width, const unsigned int maxnn, const unsigned  int filterno) {
  
  int HSno=filterno*2;
  int Vno=HSno+1;
  
  int xoffset=maxnn/2-width/2;
  int yoffset=maxnn/2-height/2;
  
  for(unsigned int y=0;y &HStoTransform, 
                        const vector &VtoTransform,
                        double freq, double deviation, double pha, double ratio,
                        unsigned int height, unsigned int width,unsigned int maxnn) {
  
  BaseImage V(maxnn,maxnn);
  BaseImage HS(maxnn,maxnn);
  
  double Vmax=-1, Vmin=9999;
  double HSmax=-1, HSmin=9999;

  //copy data to those variables
  for(unsigned int y=0;y v=cropping(V,height,width);
  BaseImage hs=cropping(HS,height,width);

  ostringstream a;
  a<< "HS-"< > &filter, double freq, double deviation, double phase, double ratio) {
  
  unsigned int width=filter.size();
  width=int(sqrt(double(width)));
  unsigned int height=width;    
  
  BaseImage filterimg(width,width);
  double filtermax=-1, filtermin=999999;
  double v;
  for(unsigned int y=0;y decompose(RGBImage in, unsigned int numpha, int numfreq) {

  DBG(DBG_VERBOSE) << "[gabor.cpp::decompose(RGBImage...)] started" << endl;

  //padding
  RGBImage inputimage=padding(in);

  unsigned int height=inputimage.yDim();
  unsigned int width=inputimage.xDim();
  unsigned int dim=height*width;
  unsigned int maxnn=height;
  int hh=height/2;
  int hb=width/2;

  //fourier transformation
  vector saveHStoTransform(dim*2,0.0);
  vector saveVtoTransform(dim*2,0.0);
  
  vector HStoTransform(dim*2,0.0);
  vector VtoTransform(dim*2,0.0);
  
  ColorPixel tmpPix;
  for(unsigned int y=0;y nn(2);
  nn[0]=maxnn;
  nn[1]=maxnn;
  
#ifdef __HAVE_NUMERICAL_RECIPES__
  NR::fourn(HStoTransform,nn,1);
  NR::fourn(VtoTransform,nn,1);
#else 
  ERR << "Need Fourier Transformation from Numerical recipes, or changements in code and another FFT" << endl;
  exit(20);
#endif

  saveHStoTransform=HStoTransform;
  saveVtoTransform=VtoTransform;
  
  //some initialization
  
  double alpha = sqrt(log(2.0)/2.0);
  if(numfreq==-1) {
    numfreq = (int) (floor((log(double(width))/log(2.0))-1));
  } 
    
  int numelem = numfreq * numpha;

  HDImage result(in.yDim(),in.xDim(),numelem*2);
  
  double phasendiff= 2.0 * M_PI / ((double)numpha);

  DBG(DBG_VERBOSE) << phasendiff << endl;
  double sigmafactor = alpha/(tan(phasendiff/2.0)*M_PI);
  double ratio = (3.0 * alpha) / (M_PI * sigmafactor);

  //generate filters
  for(int aktFreq=0;aktFreq > filter(dim);
      
      for (int y = -hh ; y< hh; y++) {
        for (int x = -hb; x(s*(delta1*delta1+delta2*delta2),0.0));
        }
      }
      
      filter[idx(hh,hb,width)]=0;
      
      DBGI(DBG_VERBOSE,saveFilter(filter,freq,deviation,pha,ratio));
      
      for(unsigned int y=0;y decompose(BaseImage in, unsigned int numpha, int numfreq) {

  DBG(DBG_VERBOSE) << "[gabor.cpp::decompose(BaseImage...)] started" << endl;

  //padding
  BaseImage inputimage=padding(in);
  
  unsigned int height=inputimage.yDim();
  unsigned int width=inputimage.xDim();
  unsigned int dim=height*width;
  unsigned int maxnn=height;
  int hh=height/2;
  int hb=width/2;
  
  //fourier transformation
  vector saveVtoTransform(dim*2,0.0);

  vector VtoTransform(dim*2,0.0);
  
  for(unsigned int y=0;y nn(2);
  nn[0]=maxnn;
  nn[1]=maxnn;

#ifdef __HAVE_NUMERICAL_RECIPES__  
  NR::fourn(VtoTransform,nn,1);
#else 
  ERR << "Need Fourier Transformation from Numerical recipes, or changements in code and another FFT" << endl;
  exit(20);
#endif
  
  saveVtoTransform=VtoTransform;
  
  //some initialization

  double alpha = sqrt(log(2.0)/2.0);
  if(numfreq==-1) {
    numfreq = (int) (floor((log(double(width))/log(2.0))-1));
  }
  int numelem = numfreq * numpha;
  
  HDImage result(in.yDim(),in.xDim(),numelem);
  
  double phasendiff= 2.0 * M_PI / ((double)numpha);

  DBG(DBG_VERBOSE) << phasendiff << endl;
  double sigmafactor = alpha/(tan(phasendiff/2.0)*M_PI);
  double ratio = (3.0 * alpha) / (M_PI * sigmafactor);

  //generate filters
  for(int aktFreq=0;aktFreq > filter(dim);
      
      for (int y = -hh ; y< hh; y++) {
	for (int x = -hb; x(s*(delta1*delta1+delta2*delta2),0.0));
	  filter[idx(idxy,idxx,width)]=exp(complex(s*(delta1*delta1+delta2*delta2),0.0));
	}
      }
      
      filter[idx(hh,hb,width)]=0;

      DBGI(DBG_VERBOSE,saveFilter(filter,freq,deviation,pha,ratio));
      
      for(unsigned int y=0;y