www.pudn.com > firev0.01.rar > em-segmentation.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 

#include "image.hpp"
#include "rgbimage.hpp"
#include "getpot.hpp"

#include "basefeature.hpp"
#include "genericfeature.hpp"
#include "texturefeature.hpp"
#include "baseclusterer.hpp"
#include "em.hpp"
#include "distances.hpp"
#include "regionfeature.hpp"

using namespace std;
using namespace img;

const unsigned char maxVote(const BaseImage& segments,const int ypos,const int xpos, const int range=2) {
  
  //  cout << "y="<< ypos<< " x=" << xpos << " ";

  vector votes(16,0);
  for(int y=ypos-range;y<=ypos+range;++y) {
    for(int x=xpos-range;x<=xpos+range;++x) {
      if(y>=0 && y=0 && xcls) {
          ++votes[cls];
        } else {
          votes.resize(cls+1,0);
          ++votes[cls];
        }
      }else{
        //cout <<"-";
      }
    }
  }
  //  cout << endl;
  
  int mVote=0;
  int pos=-1;
  for(unsigned int i=0;i" << endl
       << " mandatory parameters" << endl
       << "   -file" << endl
       << " optional parameters" << endl
       << "   -suffix" << endl
       << "   -maxSplits" << endl
       << "   -iter" << endl
       << "   -minObs" << endl
       << "   -epsilon" << endl
       << "   -distubMode" << endl
       << "   -poolMode" << endl
       << "   -d" << endl
       << endl;

}
int main(int argc, char **argv) {
  GetPot cl(argc, argv);
  vector unidentified=cl.unidentified_options(10,
                                                      "-suffix",
                                                      "-file",
                                                      "-maxSplits",
                                                      "-iter",
                                                      "-minObs",
                                                      "-epsilon",
                                                      "-disturbMode",
                                                      "-smoothRange",
                                                      "-poolMode",
                                                      "-d");
  if(unidentified.size()!=0) {
    cout << "Unknown parameters: ";
    for(unsigned int i=0;i* > data;
  GenericFeature *genTmp;
  RGBImage input;
  
  DBG(DBG_MESSAGE) << "Loading image" << endl;
  input.load(cl.follow("","-file"));
  
  DBG(DBG_MESSAGE) << "Preprocessing texture features" << endl;
  TextureFeature tf;
  tf.calculate(input);
  RGBImage tfimage=tf.textureImage();
  
  DBG(DBG_MESSAGE) << "Putting data into vector" << endl;
  for(unsigned int y=0;y(8);
      genTmp->operator[](0)=y;
      genTmp->operator[](1)=x;


//        ColorPixel hsv=input(y,x).hsvPixel();
//        genTmp->operator[](2)=hsv.r();
//        genTmp->operator[](3)=hsv.g();
//        //       genTmp->operator[](4)=0;
//        genTmp->operator[](4)=hsv.b(); 

      genTmp->operator[](2)=input(y,x).r();
      genTmp->operator[](3)=input(y,x).g();
      genTmp->operator[](4)=input(y,x).b();
      if(isnan(tfimage(y,x).r())) tfimage(y,x).r()=0;
      genTmp->operator[](5)=tfimage(y,x).r();
      if(isnan(tfimage(y,x).g())) tfimage(y,x).g()=0;
      genTmp->operator[](6)=tfimage(y,x).g();
      if(isnan(tfimage(y,x).b())) tfimage(y,x).b()=0;
      genTmp->operator[](7)=tfimage(y,x).b();

//       genTmp->operator[](8)=input(y,x).r();
//       genTmp->operator[](9)=input(y,x).g();
//       genTmp->operator[](10)=input(y,x).b();


      data.push_back(genTmp);
   }
  }
  
  DBG(DBG_MESSAGE) << "Setting up clusterer" << endl;

  BaseClusterer* cluster=new EM;
  dynamic_cast* >(cluster)->maxSplits()=cl.follow(3,"-maxSplits");
  dynamic_cast* >(cluster)->iterationsBetweenSplits()=cl.follow(10,"-iter");
  dynamic_cast* >(cluster)->minObservationsPerCluster()=cl.follow(4,"-minObs");
  dynamic_cast* >(cluster)->epsilon()=cl.follow(0.1,"-epsilon");
  dynamic_cast* >(cluster)->disturbMode(cl.follow("varianceDisturb","-disturbMode"));
  dynamic_cast* >(cluster)->poolMode(cl.follow("clusterPooling","-poolMode"));
  
  string distancestring=cl.follow("euclidean","-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);
  }      
  
  DBG(DBG_MESSAGE) << "Starting clustering" << endl;
    
  vector clusterinformation;  
  cluster->run(data, clusterinformation);
  
  BaseImage segments(input.yDim(),input.xDim());
  
  for(unsigned int i=0;ioperator[](0));
    int x=int(data[i]->operator[](1));
    unsigned char color=clusterinformation[i];
    segments(y,x)=color;
  }
  
  DBGI(DBG_VERBOSE,segments.writePNG("segments-unproc.png"));
  
  for(unsigned int y=0;y segm(input.yDim(),input.xDim());

  for(unsigned int y=0;yaverageColor()[i]);}}}
  
  segm.writePNG(string(string(cl.follow("","-file"))+string(cl.follow(".regions","-suffix"))+string(".png")).c_str());
}