www.pudn.com > firev0.01.rar > extractlfv2.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
*/
#include "image.hpp"
#include "rgbimage.hpp"
#include "getpot.hpp"
#include "imagetools.hpp"
#include 

using namespace std;
using namespace img;

typedef pair Coordinates;
typedef pair* > ColorLF;
typedef pair* > GrayLF;

void USAGE() {
  cout << "NYI"  << endl
       << endl;
}  


///-------------- methods for color images

double localVar(const RGBImage & img, const int y, const int x, const int winsize) {
  ColorPixel mean(0), variance(0);
  
  for(int i=y-winsize;i<=y+winsize;++i) {
    for(int j=x-winsize;j<=x+winsize;++j) {
      for(int c=0;c<3;++c) {
	mean[c]+=img(i,j,c);
	variance[c]+=img(i,j,c)*img(i,j,c);
      }
    }
  }

  double divid=2*winsize+1;
  divid*=divid;

  for(int c=0;c<3;++c) {
    variance[c]/=divid;
    mean[c]/=divid;
  }
  
  for(int c=0;c<3;++c) {
    variance[c]-=mean[c]*mean[c];
  }
  return ((variance[0]+variance[1]+variance[2])/3.0);
}

RGBImage* extractFeature(const RGBImage & img, const pair pos, const int winsize) {
  RGBImage* result=new RGBImage(2*winsize+1,2*winsize+1);
  
  int ri=0;
  int rj=0;
  
  for(int i=pos.first-winsize;i<=pos.first+winsize;++i) {
    rj=0;
    for(int j=pos.second-winsize;j<=pos.second+winsize;++j) {
      (*result)(ri,rj)=img(i,j);
      ++rj;
    }
    ++ri;
  }
  
  return result;
}
vector< ColorLF > extractlf(const RGBImage &img,
                            int winsize,
                            double threshold, 
                            int nOfFeatures, 
                            int subsampling, 
                            int) {

  

  vector< pair > localVarVector;

  vector result;
  
  if(nOfFeatures!= -1) {
    for(int i=winsize;i<=int(img.yDim())-winsize;i+=subsampling) {
      for(int j=winsize;j<=int(img.xDim())-winsize;j+=subsampling) {
	localVarVector.push_back(pair(localVar(img,i,j,winsize),pair(i,j)));
      }
    }
    DBG(DBG_MESSAGE) << "Got local Variances"  << endl;
    
    sort(localVarVector.rbegin(), localVarVector.rend());
    DBG(DBG_MESSAGE) << "Sorted"  << endl;
    
    for(int i=0;i threshold) {
	  result.push_back(ColorLF(Coordinates(i,j),extractFeature(img, pair(i,j), winsize)));
	}
      }
    }
  }
  DBG(DBG_MESSAGE) << "Got "<< result.size() << " features."  << endl;
  return result;
}

//----------- methods for gray images

double localVar(const BaseImage & img, const int y, const int x, const int winsize) {
  double mean=0.0, variance=0.0;
  
  for(int i=y-winsize;i<=y+winsize;++i) {
    for(int j=x-winsize;j<=x+winsize;++j) {
      mean+=img(i,j);
      variance+=img(i,j)*img(i,j);
    }
  }
  
  double divid=2*winsize+1;
  divid*=divid;
  
  variance/=divid;
  mean/=divid;
  
  variance-=mean*mean;

  return variance;
}

BaseImage* extractFeature(const BaseImage & img, const pair pos, const int winsize) {
  BaseImage* result=new BaseImage(2*winsize+1,2*winsize+1);
  
  int ri=0;
  int rj=0;
  
  for(int i=pos.first-winsize;i<=pos.first+winsize;++i) {
    rj=0;
    for(int j=pos.second-winsize;j<=pos.second+winsize;++j) {
      (*result)(ri,rj)=img(i,j);
      ++rj;
    }
    ++ri;
  }
  
  return result;
}

vector< GrayLF > extractlf(const BaseImage &img,
                           int winsize,
                           double threshold, 
                           int nOfFeatures, 
                           int subsampling, 
                           int) {
  
  vector< pair > localVarVector;

  vector result;
  
  if(nOfFeatures!= -1) {
    for(int i=winsize;i<=int(img.yDim())-winsize;i+=subsampling) {
      for(int j=winsize;j<=int(img.xDim())-winsize;j+=subsampling) {
        localVarVector.push_back(pair(localVar(img,i,j,winsize),pair(i,j)));
      }
    }
    DBG(DBG_MESSAGE) << "Got local Variances"  << endl;
    
    sort(localVarVector.rbegin(), localVarVector.rend());
    DBG(DBG_MESSAGE) << "Sorted"  << endl;
    
    for(int i=0;i threshold) {
          result.push_back(GrayLF(Coordinates(i,j),extractFeature(img, pair(i,j), winsize)));
        }
      }
    }
  }
  DBG(DBG_MESSAGE) << "Got "<< result.size() << " features."  << endl;
  return result;
}
////--------------- main program

int main(int argc, char **argv) {
  GetPot cl(argc, argv);
  
  string cmdline="";
  for(int i=0;i img;
    img.load(filename);

    if(usePadding) {
      img=padding(img, img.yDim()+2*winsize, img.xDim()+2*winsize);
    }
    
    vector< ColorLF > vec=extractlf(img,winsize,threshold, nOfFeatures, subsampling, saveImages);
    
    if(cl.search("-images")) {
      ostringstream fn;
      for(int i=0;iwritePNG(fn.str().c_str());
      }
    }
    
    ofstream ofs(string(filename+suffix).c_str());
    if(!ofs) {
      ERR << "Cannot open '" << filename << suffix << "' for writing." << endl;
    } else {
      ofs << "# local features for color image "<< filename << endl
          << "# cmdline " << cmdline << endl
	  << "# format: posy posx [data1 ... datan]" << endl
	  << "winsize " << winsize << endl
	  << "threshold " << threshold << endl
	  << "subsampling " << subsampling << endl
	  << "pixeldim 3" << endl;
      for(int i=0;iyDim());++y) {
	    for(int x=0;xxDim());++x) {
	      ofs << int((*(vec[i].second))(y,x,c)) << " ";;
	    }
	  }
	  ofs << "    ";
	}
	ofs << endl;
      }
    }
    ofs.close();
    
  } else if(cl.search("-gray")) {
    DBG(DBG_MESSAGE) << "Processing gray images"  << endl;
    BaseImage img;
    img.load(filename);
    
    if(usePadding) {
      img=padding(img, img.yDim()+2*winsize, img.xDim()+2*winsize);
    }
    
    vector< GrayLF > vec=extractlf(img,winsize,threshold, nOfFeatures, subsampling, saveImages);
    
    if(cl.search("-images")) {
      ostringstream fn;
      for(int i=0;iwritePNG(fn.str().c_str());
      }
    }
    
    ofstream ofs(string(filename+suffix).c_str());
    if(!ofs) {
      ERR << "Cannot open '" << filename << suffix << "' for writing." << endl;
    } else {
      ofs << "# local features for gray image "<< filename << endl
          << "# cmdline: " << cmdline << endl
          << "# format: posy posx [data1 ... datan]" << endl
          << "winsize " << winsize << endl
          << "threshold " << threshold << endl
          << "subsampling " << subsampling << endl
          << "pixeldim 1" << endl;
      for(int i=0;iyDim());++y) {
          for(int x=0;xxDim());++x) {
            ofs << int((*(vec[i].second))(y,x)) << " ";;
          }
        }
        ofs << endl;
      }
    }
    ofs.close();
    
  } else {
    USAGE();
  }
}