www.pudn.com > firev0.01.rar > imagetools.hpp


/*
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
*/
#ifndef __imagetools_hpp
#define __imagetools_hpp

#include 
#include 
#include "diag.hpp"
#include "gzstream.hpp"
#include "image.hpp"
#include "hdimage.hpp"
#include "rgbimage.hpp"
using namespace std;
using namespace img;

namespace img {
  /** 
   * Crop the center part of the input image to the wished dimensions
   * 
   * @param in the input image
   * @param destHeight the desired height of the output image
   * @param destWidth the desired width of the output image
   * 
   * @return an image with the desired dimension containing the center part of the input image
   */
  template
  BaseImage cropping(const BaseImage &in,unsigned int destHeight, unsigned int destWidth) {
    int xoffset, yoffset;

    unsigned int srcWidth=in.xDim();
    unsigned int srcHeight=in.yDim();
  
    BaseImage  result(destHeight, destWidth);
  
    xoffset=srcWidth/2-destWidth/2;
    yoffset=srcHeight/2-destHeight/2;

    for(unsigned int y=0;y
  ::img::RGBImage padding(const ::img::RGBImage &in) {
    int xoffset, yoffset;
  
    unsigned int srcWidth=in.xDim();
    unsigned int srcHeight=in.yDim();
  
    vector yKbdWindow=stretchedgaussianwindow(srcHeight,10); 
    vector xKbdWindow=stretchedgaussianwindow(srcWidth,10);
  
    unsigned int maxnn=next2Power(srcWidth);
    if(maxnn< next2Power(srcHeight)) maxnn=next2Power(srcHeight);
  
    unsigned int destWidth=maxnn; unsigned int destHeight=maxnn;
  
    ::img::RGBImage  result(destHeight, destWidth);
  
    xoffset=destWidth/2-srcWidth/2;
    yoffset=destHeight/2-srcHeight/2;
  
    for(unsigned int y=0;y
  BaseImage padding(const BaseImage &in) {
    int xoffset, yoffset;
  
    unsigned int srcWidth=in.xDim();
    unsigned int srcHeight=in.yDim();
  

    vector yKbdWindow=stretchedgaussianwindow(srcHeight,10); 
    vector xKbdWindow=stretchedgaussianwindow(srcWidth,10);
  
    unsigned int maxnn=next2Power(srcWidth);
    if(maxnn< next2Power(srcHeight)) maxnn=next2Power(srcHeight);
  
    unsigned int destWidth=maxnn; unsigned int destHeight=maxnn;
  
    BaseImage  result(destHeight, destWidth);
  
    xoffset=destWidth/2-srcWidth/2;
    yoffset=destHeight/2-srcHeight/2;
  
    for(unsigned int y=0;y
  BaseImage padding(const BaseImage &in, const unsigned int destHeight, const unsigned int destWidth) {
    int xoffset, yoffset;
  
    unsigned int srcWidth=in.xDim();
    unsigned int srcHeight=in.yDim();
  
    BaseImage  result(destHeight, destWidth);
  
    xoffset=destWidth/2-srcWidth/2;
    yoffset=destHeight/2-srcHeight/2;
  
    for(unsigned int y=0;y
  RGBImage padding(const RGBImage &in, const unsigned int destHeight, const unsigned int destWidth) {
    int xoffset, yoffset;
  
    unsigned int srcWidth=in.xDim();
    unsigned int srcHeight=in.yDim();
  
    RGBImage  result(destHeight, destWidth);
  
    xoffset=destWidth/2-srcWidth/2;
    yoffset=destHeight/2-srcHeight/2;
  
    for(unsigned int y=0;y
  double localVariance(RGBImage image,const int y, const int x, const int winsize){
    int w2=winsize/2;
    int i=0,j=0;
    double med=0.0 ,var=0.0;
    double result=0.0;

    for(int c=0;c<3;++c) {
      med=0.0;
      var=0.0;
    
      for(i=y-w2;i<=y+w2;++i){
        for(j=x-w2;j<=x+w2;++j) {
          med += image(i,j,c);
        }
      }
      //cout << "med("<> nOfClasses >> dim;
      cout << "Will find " << nOfClasses << "classes " << endl;
      height=width=int(sqrt(double(dim)));
      if(height*width != dim) {height=1; width=dim;}
      DBG(DBG_MESSAGE) << "[imagetools.hpp::readJF] "<< "Reading images with height " << height << " and width " << width << "." << endl;
      while(!ifs.eof() && cls != -1) {
        ifs >> cls ;
        if(!ifs.eof() && cls != -1) {
          img=new BaseImage(height,width);
          img->cls()=cls;
          for(int y=0;y> tmp;
              (*img)(y,x)=T(tmp);
            }
          }
          result.push_back(img);
        }
      }
    }
    DBG(DBG_MESSAGE) << "[imagetools.hpp::readJF] " < 
  vector *> readColorJF(const string filename) {
    vector* > result;
    RGBImage* img;
    double tmp;
    int cls, height=0, width=0, dim, nOfClasses;
    
    igzstream ifs(filename.c_str());
    if(!ifs) {
      ERR << "[imagetools.hpp::readColorJF] "<< "Cannot open '"<< filename <<"' for reading." << endl;
    } else {
      ifs >> nOfClasses >> dim;
      cout << "Will find " << nOfClasses << "classes " << endl;
      height=width=int(sqrt(double(dim/3)));
      DBG(DBG_MESSAGE) << "[imagetools.hpp::readColorJF] "<< "Reading images with height " << height << " and width " << width << "." << endl;
      while(!ifs.eof() && cls != -1) {
        ifs >> cls ;
        if(!ifs.eof() && cls != -1) {
          img=new RGBImage(height,width);
          img->cls()=cls;
          for(int c=0;c<3;++c) {
            for(int y=0;y> tmp;
                (*img)(y,x,c)=T(tmp*255);
              }
            }
            result.push_back(img);
          }
        }
      }
    }
    DBG(DBG_MESSAGE) << "[imagetools.hpp::readJF] " <
  vector *> readEJF(const string filename) {
    vector* > result;
    BaseImage* img;
    
    int cls, height, width;
    double tmp;
    igzstream ifs(filename.c_str());
    if(!ifs) {
      ERR << "Cannot open '"<< filename <<"' for reading." << endl;
    } else {
      while(!ifs.eof()) {
        ifs >> cls >> width >> height;
        if(!ifs.eof()) {
          
          img=new BaseImage(height,width);
          img->cls()=cls;
          for(int y=0;y> tmp;
              (*img)(y,x)=T(tmp);
            }
          }
          result.push_back(img);
        }
      }
    }
    return result;
  }
  
}

#endif