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);
}
예제 #2
0
int main(int argc, char** argv) {

  if (argc < 3) {
    printf("Usage: salienpoints <image file> <# of salien points>\n");
    exit(1);
  }
  ImageFeature imgFeature;
  imgFeature.load(argv[1]);
  SalientPoints sp(imgFeature);
  
  vector<Point> points = sp.getSalientPoints(atoi(argv[2]));
  
  DBG(10) << points.size() << " salient points extracted, wanted " << atoi(argv[2]) << endl;
  for (uint i = 0; i < points.size(); i++) {
    Point p = points[i];
    DBG(10) <<"(" << p.x << ", " << p.y << ")" << endl;
  } 

  ImageFeature newImage;
  newImage.createFromPixelset(imgFeature.xsize(), imgFeature.ysize(), sp.getPixels());
    
  newImage.display();

}
int main(int argc, char** argv) {

  GetPot cl(argc,argv);
  if (cl.search(2,"--help","-h")) {
    USAGE();
  }

  vector<string> images;
  if (cl.search("--images")) {
    string filename =  cl.next(" ");
    while (filename != " ") {
      images.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!="") {
        images.push_back(filename);
      }
    }
    ifs.close();
  } else {
    USAGE();
    exit(20);
  }

  bool forceGray;
  if (cl.search(1, "--gray")) {
    forceGray = true;
  } else if (cl.search(1, "--color")) {
    forceGray = false;
  } else {
    USAGE();
    exit(20);
  }

  int numPatches = 200;
  if (cl.search(2, "-n", "--num")) {
    numPatches = cl.next(200);
  }

  for(uint i = 0; i < images.size(); i++) {
    string filename=images[i];
    DBG(10) << "Processing '"<< filename << "'.(" << i << "/" << images.size()<< ")" <<endl;
    ImageFeature img; img.load(filename,forceGray);
    DifferenceOfGaussian sift(img);
    vector<InterestPoint> interestPoints = sift.getInterestPoints(numPatches);
    
    ImageFeature padded = img;
    vector<double> color(3,1.0);
    
    for (int i = 0; i < (int) interestPoints.size(); i++) {
      InterestPoint ip = interestPoints[i];
      box(padded, ip.x, ip.y, color, ip.scale / 2);
    }
    padded.display();

    break;
  }


  DBG(10) << "cmdline was: "; printCmdline(argc,argv);

}
예제 #4
0
void LocalFeatureExtractor::extractFromDatabase(const Database &db) {
  string suffix=settings_.suffix();

  DBG(10) << "Settings for extraction:" << endl
          << settings_ << endl;

  PCA pca(settings_.dim());
  if(settings_.loadpca) {
    pca.load(settings_.pcafile);
  }

  string tmppath=settings_.tmppath+"/"+getenv("USER");
  mkdir(tmppath.c_str(), 0xFFFFFF);
  tmppath+="/extractlf";
  mkdir(tmppath.c_str(), 0xFFFFFF);

  for(uint n=0;n<db.size();++n) {
    string imagefilename=db.path()+"/"+db.filename(n);
    string imagebasename=db.filename(n).substr(db.filename(n).find_last_of("/")+1);

    LocalFeatures lf;
    ImageFeature img;
    DBG(10) << "Extracting features from '" <<imagefilename << "': " << n+1 << "/" << db.size() << endl;
    img.load(imagefilename);
    lf=extractFromImage(img,lf);
    if(settings_.pcaonly){
    	lf.save(tmppath+"/"+imagebasename+"."+suffix);
    } else {
    	lf.save(imagefilename+"."+suffix);
    }

    if(settings_.pca and not settings_.loadpca) {
      for(uint l=0;l<lf.data_.size();++l) {
        pca.putData(lf[l]);
      }
    } else if(settings_.loadpca) {
      pcaTransform(pca,lf);
      lf.save(imagefilename+".pca."+suffix);
    }
    DBG(10) << "Extracted " << lf.numberOfFeatures_ << " features." << endl;
  }

  if(settings_.pca and not settings_.loadpca) {
    DBG(10) << "Estimating PCA transformation matrix." << endl;
    pca.dataEnd();
    pca.calcPCA();
    pca.save(settings_.pcafile+"."+suffix+".pca.gz");

    for(uint n=0;n<db.size();++n) {
      string imagefilename=db.path()+"/"+db.filename(n);
      string imagebasename=db.filename(n).substr(db.filename(n).find_last_of("/")+1);
      DBG(10) << "PCA transforming features for '" << imagefilename << "': " << n+1 << "/" << db.size() << endl;

      LocalFeatures lf;
      if(settings_.pcaonly){
    	  lf.load(tmppath+"/"+imagebasename+"."+suffix);
    	  remove((tmppath+"/"+imagebasename+"."+suffix).c_str());
      } else {
    	  lf.load(imagefilename+"."+suffix);
      }

      pcaTransform(pca,lf);
      lf.save(imagefilename+".pca."+suffix);
    }
  }

  rmdir(tmppath.c_str());
}
예제 #5
0
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(".ar.vec.gz","--suffix");

  //get list of files to be processed
  vector<string> infiles;
  if(cl.search("--image")) {
    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);
  }
  

  // processing the files
  ImageFeature img;
  VectorFeature out(2);
  VectorFeature min(2), max(2), avg(2);
  min[0]=numeric_limits<double>::max();   min[1]=numeric_limits<double>::max(); 
    
  for(uint i=0;i<infiles.size();++i) {
    string filename=infiles[i];
    DBG(10) << "Processing '" << filename << "' (" << i+1<< "/" << infiles.size() << "): ";
    img.load(filename);
    out[0]=img.xsize();
    out[1]=img.ysize();
    out.save(filename+"."+suffix);
    
    BLINK(10) << out[0] << " " << out[1] << endl;

    if(min[0]>out[0]) min[0]=out[0];
    if(min[1]>out[1]) min[1]=out[1];
    
    if(max[0]<out[0]) max[0]=out[0];
    if(max[1]<out[1]) max[1]=out[1];

    avg[0]+=out[0]; avg[1]+=out[1];
    DBG(20) << "Finished with '" << filename << "'." << endl;
  }
  
  avg[0]/=infiles.size(); avg[1]/=infiles.size();
  DBG(10) << "min: " << min[0] << " " << min[1] << endl;
  DBG(10) << "max: " << max[0] << " " << max[1] << endl;
  DBG(10) << "avg: " << avg[0] << " " << avg[1] << endl;

  DBG(10) << "cmdline was: "; printCmdline(argc,argv);
}
예제 #6
0
int main(int argc, char**argv) {
  GetPot cl(argc,argv);
  srand((unsigned)time(0));
 
  vector<ImageFeature> inputimages;
  if (!cl.search("--images")) {
    USAGE();
    exit(20);
  }

  cl.search("--images");
  string filename=cl.next(" ");
  while(filename!=" ") {
    DBG(10) << "Loading " << filename << endl;
    ImageFeature tmp;
    tmp.load(filename);
    inputimages.push_back(tmp);
    filename=cl.next(" ");
  }
  
  uint dimx=inputimages[0].xsize();
  uint dimy=inputimages[0].ysize();
  uint dimz=inputimages[0].zsize();
  
 
  uint WINX=cl.follow(10,"--winx");
  uint WINY=cl.follow(10,"--winy");
  uint BORDERWIDTH=cl.follow(0,"--borderwidth");
  double AFFDEV=cl.follow(0.1,"--affineDeviation");
  uint winWidth=dimx/WINX;
  uint winHeight=dimy/WINY;
  uint POSDEV=cl.follow(20,"--positionDeviation");
  uint POSDEV2=POSDEV/2;
  if(POSDEV > winWidth/2 || POSDEV>winHeight/2) {
    POSDEV=min(winWidth/2,winHeight/2);
    DBG(10) << "Setting posdev=" << POSDEV << endl;
  }
  
  ImageFeature outputimage(dimx+(WINX+1)*BORDERWIDTH, dimy+(WINY+1)*BORDERWIDTH, dimz);

  DBG(10) "Patching the new image" << endl;
  for(uint winx=0;winx<WINX;++winx) {
    for(uint winy=0;winy<WINY;++winy) {
      
      uint sourceImage=randomInt(inputimages.size());
      BLINK(30) << " " << sourceImage << flush;
      int top=max(int(randomInt(POSDEV)+int(winy*winHeight)-int(POSDEV2)),0);
      int bottom=max(randomInt(POSDEV)+int((winy+1)*winHeight)-int(POSDEV2),0);
      int left=max(randomInt(POSDEV)+int(winx*winWidth)-int(POSDEV2),0);
      int right=max(randomInt(POSDEV)+int((winx+1)*winWidth)-int(POSDEV2),0);
      
      ImageFeature patch=getPatch(inputimages[sourceImage],left,top,right,bottom);
      
      AffineTransformation aff(1+randomDouble(AFFDEV),randomDouble(AFFDEV), randomDouble(AFFDEV), 1+randomDouble(AFFDEV),0,0);
      patch=affineTransformation(patch,aff);
      
      setPatch(outputimage,left+(winx+1)*BORDERWIDTH,top+(winy+1)*BORDERWIDTH,patch);
    }
    BLINK(30) << endl;
  }

  outputimage.save(cl.follow("output.png","--output"));
}
예제 #7
0
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);
  }
}