www.pudn.com > firev0.01.rar > gabor-cluster.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 

#include "gabor.hpp"
#include "filelist.hpp"
#include "getpot.hpp"
#include "basefeature.hpp"
#include "genericfeature.hpp"
#include "baseclusterer.hpp"
#include "em.hpp"
#include "kmeans.hpp"
#include "basetools.hpp"

using namespace img;
using namespace std;

typedef vector FeatureVector;

void USAGE() {
  cout 
    << "USAGE: " <] " << endl
    << "   mandatory parameters:" << endl
    << "      -em | -kmeans        to select the algorithm to be used" << endl
    << "      -d (euclidean|chisquare|jsd|kld|his) to select the distance function" << endl
    << "   Options to kMeans " << endl
    << "      -nOfClusters (10)" < saves clusters after running" << endl
    << "      -withoutcoord load features without coordinate information" << endl
    << endl;
}

int getIRMAClass(string filename) {
  int i;
  for(i=filename.size()-1;i>=0 && filename[i]!='/';--i);
  
  char klasse[4];
  for(int k=1;k<=3;++k) klasse[k-1]=filename[i+k];
  klasse[3]=0;

  return atoi(klasse);
}

int main(int argc, char **argv) {
  GetPot cl(argc, argv);

  vector unident=cl.unidentified_options(15,"-em","-kmeans","-d","-nOfClusters","-iterations","-maxSplits","-iter","-minObs","-epsilon","-disturbMode","-poolMode","-h","-saveClusters","-withoutcoord","-dontSplitBelow");

  if(unident.size()!=0 || cl.search("-h") || argc<2 || (!cl.search("-em")&& !cl.search("-kmeans")) || !cl.search("-d")) {
    if(unident.size()!=0) {
      cout << "Unknown options: " ;
      for(unsigned int i=0;i* > database;
  vector databaseInformation;
  
  vector* > read;
  for(unsigned int i=0;i *feature;
    for(unsigned int k=0;k(*(read[k]));
      database.push_back(feature);
      databaseInformation.push_back(i);
    }
  }
  DBG(DBG_MESSAGE) << "Read " << database.size() << " features in total " << endl;
  
  
  if(cl.search("-project")) {
    int idx1=cl.follow(0, "-project");
    int idx2=cl.next(1);
    string filename=cl.next("dataToPlot.dat");
    
    ofstream ofs(filename.c_str());
    for(unsigned int i=0;ioperator[](idx1) << " " <<  database[i]->operator[](idx2) << endl;
    }
    ofs.close();
  }
  
  DBG(DBG_MESSAGE) << "Clustering NOW" << endl;
  
  BaseClusterer *cluster=NULL;
  if(cl.search("-em")) {
    cluster=new EM;
    
    dynamic_cast* >(cluster)->maxSplits()=cl.follow(4,"-maxSplits");
    dynamic_cast* >(cluster)->iterationsBetweenSplits()=cl.follow(10,"-iter");
    dynamic_cast* >(cluster)->minObservationsPerCluster()=cl.follow(4,"-minObs");
    dynamic_cast* >(cluster)->dontSplitBelow()=cl.follow(10,"-dontSplitBelow");
    dynamic_cast* >(cluster)->epsilon()=cl.follow(0.1,"-epsilon");
    dynamic_cast* >(cluster)->disturbMode(cl.follow("varianceDisturb","-disturbMode"));
    dynamic_cast* >(cluster)->poolMode(cl.follow("noPooling",2,"-poolMode"));
    
    
  } else if(cl.search("-kmeans")) {
    cluster=new kMeans;
    dynamic_cast* >(cluster)->nOfClusters()=cl.follow(10,"-nOfClusters");
    dynamic_cast* >(cluster)->iterations()=cl.follow(10,"-iterations");
  } else {
    ERR << "Unknown clustermethod '"<< cluster <<"'."<< endl;
    USAGE();
    exit(20);
  }
  
  if(cl.search("-d")) {
    string distancestring=cl.follow("","-d");
    cluster->distString()=distancestring;
    if(distancestring=="euclidean") {
      cluster->dist()=new EuclideanDist();
    } else if(distancestring=="chisquare") {
      cluster->dist()=new ChiSquareDistance();
    } else if(distancestring=="jsd") {
      cluster->dist()=new JensenShannonDivergence();
    } else if(distancestring=="kld") {
      cluster->dist()=new KullbachLeiblerDivergence();
    } else if(distancestring=="his") {
      cluster->dist()=new HistogramIntersectionDistance();
    } else {
      ERR << "Unknown clustermethod '"<< cluster <<"'."<< endl;
      USAGE();
      exit(20);
    }      
  }

  vector clusterinformation;  
  cluster->run(database,clusterinformation);
  
  DBG(DBG_MESSAGE) << "Clustering finished, now sorting" << endl;
  
  vector< pair > toSort(database.size());
  for(unsigned int i=0;isize();  
  int maxi=*(max_element(clusterinformation.begin(), clusterinformation.end()))+1;
  vector densities(maxi,GaussianDensity(dim));
  
  //  int oldCls=-1;
  
//   DBG(DBG_RESULT) << "Cluster " << oldCls;
//   for(unsigned int i=0;isaveClusters(cl.follow("clusters.jf","-saveClusters"));
  }

  printCmdline(argc,argv);
  exit(0);
  
}