コード例 #1
0
ファイル: eigenfacer.cpp プロジェクト: 418231020/fire-cbir
int main(int argc , char **argv) {
  
  GetPot cl(argc,argv);
  
  if(cl.search("-h")) {USAGE(); exit(0);}

  string  line;
  if(cl.search("-E")) {
    cout << "Estimating PCA" << endl;
    
    ifstream filelist(cl.follow("filelist","-E"));
    ImageFeature img;    
    getline(filelist,line);

    img.load(line,true);
    cout << line << endl;
    
    PCA pca(img.size());
    cout << img.size() << endl;
    
    pca.putData(img.layerVector(0));
    
    while(getline(filelist,line)) {
      cout << line << endl;
      img.load(line,true);
      pca.putData(img.layerVector(0));
    }
    pca.dataEnd();
    pca.save(cl.follow("covariance.pca","-c"));
    pca.calcPCA();
    pca.save(cl.follow("transformation.pca","-t"));
    filelist.close();
    
  } else if(cl.search("-T")) {
    PCA pca;
    pca.load(cl.follow("transformation.pca","-t"));
    int dim=cl.follow(20,"-d");
    
    ifstream filelist(cl.follow("filelist","-T"));
    vector<double> tmp;
    ImageFeature img;
    while(getline(filelist,line)) {
      cout << line << endl;
      img.load(line,true);
      tmp=pca.transform(img.layerVector(0),img.size());
      tmp.resize(dim);
      VectorFeature tmpvec(tmp);
      tmpvec.save(line+".pca.vec.gz");
    }
    filelist.close();
  } else if(cl.search("-B")) {
    PCA pca;
    pca.load(cl.follow("transformation.pca","-t"));
    int x=cl.follow(16,"-x");
    int y=cl.follow(16,"-y");
    ifstream filelist(cl.follow("filelist","-B"));
    vector<double> tmp;
    VectorFeature tmpvec;
    while(getline(filelist,line)) {
      cout << line << endl;
      tmpvec.load(line);
      vector<double> tmp;
      tmp=pca.backTransform(tmpvec.data());
      ImageFeature img(tmp,x,y);
      cutoff(img);
      img.save(line+".backpca.png");
    }
    filelist.close();
  } else if(cl.search("-M")) {
    ImageFeature img; img.load(cl.follow("image.png","-M"),true);
    PCA pca;
    vector<double> backproj,vec;
    pca.load(cl.follow("transformation.pca","-t"));
    int w=cl.follow(16,"-x");
    int h=cl.follow(16,"-y");
    double scalefac=cl.follow(0.8333,"-s");
    int dim=cl.follow(20,"-d");
    
    ImageFeature scimg(img);
    ImageFeature patch;
    uint minX=100000, minY=100000; 
    uint maxX=100000, maxY=100000; 
   
    while(int(scimg.xsize())>=w and int(scimg.ysize())>=h) {
      DBG(10) << VAR(scimg.xsize()) << " x " << VAR(scimg.ysize()) << endl;
      ImageFeature faceprobmap(scimg.xsize(),scimg.ysize(),1);
      double maxDist=0.0;
      double minDist=numeric_limits<double>::max();
      
      vector<double> tmpvec;

      for(uint x=0;x<scimg.xsize();++x) {
        DBG(10) << VAR(x) << endl;
        for(uint y=0;y<scimg.ysize();++y) {
          patch=getPatch(scimg,x,y,x+w,y+h);
          vec=patch.layerVector(0);
          
          vector<double> imgMinMean=vec;
          for(uint i=0;i<imgMinMean.size();++i) {
            imgMinMean[i]-=pca.mean()[i];
          }
          double energyImg=getEnergy(imgMinMean);
          
          tmpvec=pca.transform(vec,dim);
          double energyTrans=getEnergy(tmpvec);
          backproj=pca.backTransform(tmpvec);
          
          double energyBack=getEnergy(backproj);

          double d=0;
          double tmp;
          for(uint i=0;i<backproj.size();++i) {
            tmp=backproj[i]-vec[i];
            d+=tmp*tmp;
          }
          faceprobmap(x,y,0)=d;
          
          DBG(10) << VAR(energyImg) << " "
                  << VAR(energyTrans) << " " 
                  << VAR(energyBack) << " "
                  << VAR(energyImg-energyTrans) << " "
                  << VAR(d) << endl;


          if(minDist>d) {
            minDist=d;
            minX=x; minY=y;
          }
          if(maxDist<d) {
            maxDist=d;
            maxX=x; maxY=y;
          }
        }
      }
      
      DBG(10) << VAR(scimg.xsize()) << " " << VAR(scimg.ysize()) << endl;
      DBG(10) << VAR(minDist) << " " << VAR(minX) << " " << VAR(minY) << endl;
      DBG(10) << VAR(maxDist) << " " << VAR(maxX) << " " << VAR(maxY) << endl << endl;

      normalize(faceprobmap);
      ostringstream filenamestream;
      filenamestream << cl.follow("image.png","-M") << ".fpm." << (scimg.xsize()) <<".png";
      faceprobmap.save(filenamestream.str());
      
      uint newW=int(scimg.xsize()*scalefac);
      uint newH=int(scimg.ysize()*scalefac);
      scimg=scale(scimg,newW,newH);
    }
    
  } else if(cl.search("-F")) {
#ifdef HAVE_FFT_LIBRARY
    ImageFeature img; img.load(cl.follow("image.png","-F"),true);
    PCA pca; pca.load(cl.follow("transformation.pca","-t"));
    int w=cl.follow(16,"-x");
    int h=cl.follow(16,"-y");
    double scalefac=cl.follow(0.8333,"-s");
    uint dim=cl.follow(20,"-d");

    DBG(10) << "Eigenfaces loaded" << endl;

    
    ImageFeature scimg=img;
    while(int(scimg.xsize())>w and int(scimg.ysize())>h) {
      ImageFeature fpm=detect(scimg,pca,dim,w,h);

      pair<uint,uint> p;

      p=argmax(fpm);
      
      DBG(10) << scimg.xsize() << "x" << scimg.ysize() << " (" <<p.first<<", "<< p.second << ") " << VAR(maximum(fpm)) ;

      p=argmin(fpm);
      BLINK(10) << " (" <<p.first<<", "<< p.second << ") " << VAR(minimum(fpm)) << endl;

      normalize(fpm);
      
      ostringstream filenamestream;
      filenamestream << cl.follow("image.png","-F") << ".fpm." << (scimg.xsize()) <<".png";
      fpm.save(filenamestream.str());
      
      scimg=scale(scimg,int(scimg.xsize()*scalefac),int(scimg.ysize()*scalefac));
    }
  
    

#else
    DBG(10) << "compiled without FFT lib. this does not work. use -M option" << endl;
#endif
    
  } else {
    USAGE();
    exit(20);
  }
}
コード例 #2
0
int main(int argc, char**argv) {
  GetPot cl(argc, argv);
  
  if(cl.search(2,"-h","--help")) {USAGE(); exit(0);}
  string pcafile=cl.follow("pca.pca",2,"-p","--pca");
  string imagefile=cl.follow("image",2,"-i","--img");

  VectorFeature vec;
  PCA pca; pca.load(pcafile);
  
  uint width, height, depth;
  if(cl.search(2,"-c","--color")) {
    depth=3;
  } else {
    depth=1;
  }
    
  if(!cl.search(2,"-w","--width") && !cl.search(2,"-h","--height")) {
    width=uint(sqrt(double(pca.dim())));
    height=width;
    if(int(height*width)!=pca.dim()) {
      ERR << "pca not for squared images, specify width or height" << endl
          << "height=" << height << "* width=" << width << "!= size=" << pca.dim() << endl;
      exit(20);
    } 
  } else {
    if(cl.search(2,"-w","--width") && !cl.search(2,"-h","--height")) {
      width=cl.follow(10,2,"-w","--width");
      height=pca.dim()/width;
      if(int(height*width)!=pca.dim()) {
        ERR << "pca images of this width, specify valid values" << endl
            << "height=" << height << "* width=" << width << "!= size=" << pca.dim() << endl;
        exit(20);
      }
    } else if(!cl.search(2,"-w","--width") && cl.search(2,"-h","--height")) {
      height=cl.follow(10,2,"-j","--height");
      width=pca.dim()/height;
      if(int(height*width)!=pca.dim()) {
        ERR << "pca images of this height, specify valid values" << endl
            << "height=" << height << "* width=" << width << "!= size=" << pca.dim() << endl;
        exit(20);
      }
    } else {
      height=cl.follow(10,2,"-j","--height");
      width=cl.follow(10,2,"-w","--width");
      if(int(height*width)!=pca.dim()) {
        ERR << "pca images of this height and width, specify valid values" << endl
            << "height=" << height << "* width=" << width << "!= size=" << pca.dim() << endl;
        exit(20);
      }
    }
  }



  vector<double> backtransformed;
  
  if(cl.search(2,"-v","--vec") && !cl.search(2,"-b","--base") && !cl.search(2,"-M","--mean")) {
    string vecfile=cl.follow("vec.vec",2,"-v","--vec");
    DBG(10) << "Loading Vectorfile " << vecfile << endl;
    vec.load(vecfile);    
    DBG(10) << "Vector to be backtransformed" ;
    for(uint i=0;i<vec.size();++i) {
      BLINK(10) << " "<< vec[i];
    }
    BLINK(10) << endl;

    if(cl.search("-n1st"))  vec[0]=0;

    if(cl.search("--nopca")) {
      backtransformed=vec.data();
    } else {
      backtransformed=pca.backTransform(vec.data());
    }
    
    DBG(10) << "Backtransformed Vector" ;
    for(uint i=0;i<backtransformed.size();++i) {
      BLINK(10) << " " <<backtransformed[i];
    }
    BLINK(10) << endl;

  } else if(cl.search(2,"-b","--base") && !cl.search(2,"-v","--vec") && !cl.search(2,"-M","--mean")) {
    uint base=cl.follow(0,2,"-b","--base");
    backtransformed=pca.eigenvector(base);
  } else if(!cl.search(2,"-b","--base") && !cl.search(2,"-v","--vec") && cl.search(2,"-M","--mean")) {
    backtransformed=pca.mean();
  } else {
    USAGE();
    exit(20);
  }
  
  ImageFeature image(width,height,depth);
  
  if(cl.search(2,"-m","--minMean")) {
    DBG(10) << "Subtracting mean" << endl;
    for(uint i=0;i<backtransformed.size();++i) {
      backtransformed[i]-pca.mean()[i];
    }
  }
  
  for(uint i=0;i<backtransformed.size();++i) {
    image[i]=backtransformed[i];
  }

  
  if(cl.search(2,"-n","--norm")) {
    DBG(10) << "normalization" << endl;
    normalize(image);
  }
  
  
//  //make values positive
//  shift(image,-minimum(image,0));
//
  cutoff(image);
//

  DBG(10) << "Going to save:" ;
  for(uint i=0;i<image.size();++i) {
    BLINK(10) << " " <<image[i];
  }
  BLINK(10) << endl;



  image.save(imagefile);
  DBG(10) << "cmdline was: "; printCmdline(argc,argv);
}
コード例 #3
0
int main(int argc, char**argv) {
  GetPot cl(argc, argv);

  if(cl.search(2,"-h","--help") or !cl.search("-lf")) {USAGE(); exit(0);}

  bool nonorm = cl.search(1, "-nonorm");
  LocalFeatures lf; lf.load(cl.follow("test.lf.gz","-lf"));
  uint sc = cl.follow(1, "-scale");
  uint w=uint(lf.winsize()*2+1);
  ImageFeature img(w,w,lf.zsize());

  string savefilename = cl.follow("", "-save");

  bool backtransform = false;
  PCA pca;
  if(cl.search("-unpca"))
  {
	  if( !pca.load(cl.follow("", "-unpca")) )
	  {
		  ERR << "Error loading PCA file!" << endl;
		  abort();
	  }
	  backtransform = true;
  }

  bool dontshow = cl.search("-dontshow");

  for(uint i=0;i<lf.size();++i) {
    DBG(10) << "lf " << i << endl;
    if(!backtransform) {
    	for(uint j=0;j<lf.dim();++j) {
    		img[j] = lf.getData()[i][j];
    	}
    } else {
    	vector<double> backtransformed = pca.backTransform(lf.getData()[i]);
    	for(uint j=0;j<(uint)pca.dim();++j) {
    		img[j] =  backtransformed[j];
    	}
    }
    if (sc != 1) {
      img = scale(img, w * sc, w * sc);
    }

    if (!nonorm) {
      normalize(img);
    }
    if(!dontshow) {
    	if(cl.search("-layerwise")) {

    		for(uint c=0;c<img.zsize();++c) {
    			BLINK(10) << " " << c;
    			img.display(c,c,c);
    		}
    		BLINK(10) << endl;
    	} else {
    		BLINK(10) << "all layers" << endl;;
    		img.display();
    	}
    }

    if (savefilename != "") {
    	std::ostringstream filenamewithnr;
    	filenamewithnr << savefilename << "_" << setw(3) << setfill('0') << i << ".png";
    	img.save(filenamewithnr.str());
    }
  }
}