Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

TimeGaussMIX_MDL.hh

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*/
00005  /*---------------------------------------------------------------------------*/
00006 
00007 #include <iostream>
00008 #include <string>  
00009 #include <vector>
00010 #include <fstream>
00011 
00012 #include <cstdio>
00013 #include <cstdlib>
00014 #include <cmath>
00015 #include <cassert>
00016 
00017 #include "./Inliers.hh"
00018 
00019 using namespace std;
00021 class Temporal_MDL_classifier 
00022 {       
00023         public:
00024                 
00025                 Temporal_MDL_classifier(){};
00026                 /*--------------------------------------------------------------------------------------------*/
00029                 void Initialise(char * path_in, char * path_fileout, int dimx_tmp, int dimy_tmp, int nb_bands, int ofx_tmp, int ofy_tmp, int Dy_tmp, int subx_tmp, int suby_tmp,int nb) 
00030         
00031                  {
00032                         Acquire_path(path_in,nb_bands,path_fileout);
00033                         Acquire_parameters_allocateImage(dimx_tmp,dimy_tmp,ofx_tmp,ofy_tmp,Dy_tmp,subx_tmp,suby_tmp);
00034                         Curent_images_index=0;
00035                         Acquire_next_image();
00036                         Begin_procedure(nb);
00037                         Allocate_classfile();
00038                         Allocate_OutMeanCov();
00039                         
00040                         
00041                  }
00042                 /*--------------------------------------------------------------------------------------------*/
00049                  void Cluster_optimiser()
00050                  {
00051                         int nb_outliers_temp,nb_RefVector_temp;
00052                         int compt;
00053                         time_t tdeb, t1,t2,t3;
00054                         while (Curent_images_index<nb_images){
00055                                 if (Curent_images_index!=0)
00056                                 {
00057                                         Acquire_next_image();
00058                                         I[0].AddRandomRefVectors();
00059                                 }
00060                                 
00061 time(&tdeb);
00062 t1=tdeb;
00063                                 I[0].Adaptation();
00064 time(&tdeb);
00065 t2=tdeb;
00066                                 I[0].Selection();
00067 time(&tdeb);
00068 t3=tdeb;
00069 cout<<"Adaptation time : "<<t2-t1<<" sec / Selection time : "<<t3-t2<<" sec"<<endl;
00070 cout<<"Mean adaptation iteration : "<<(float)I[0].nbMeanIt/(float)I[0].nbMeanAdapt<<endl;
00071 
00072                                 I[0].Outliers_detection();
00073                                 nb_outliers_temp=0;
00074                                 nb_RefVector_temp=0;
00075                                 compt=0;
00076                                 while(nb_outliers_temp!=O->nb_outliers || nb_RefVector_temp!=I->nb_RefVector || I->Change) 
00077                                 { 
00078                                         if (compt > 25 && nb_outliers_temp==O->nb_outliers && nb_RefVector_temp==I->nb_RefVector)
00079                                         {
00080                                                 break;
00081                                         }
00082                                         nb_outliers_temp=O->nb_outliers;
00083                                         nb_RefVector_temp=I->nb_RefVector;
00084                                         compt++;
00085                                         //cout<< ""<<endl;
00086                                         //cout<< "Selection step number "<<compt<< ":            number of RefVector = "<<nb_RefVector_temp<<", number of Outliers = "<<nb_outliers_temp<<endl;
00087                                         I[0].Adaptation();
00088                                         I[0].Selection();       
00089                         
00090                                                 
00091                                 }
00092                                         cout<< "Number of Selection steps "<<compt<< ",   number of RefVector = "<<nb_RefVector_temp<<", number of Outliers = "<<nb_outliers_temp<<endl;
00093 time(&tdeb);
00094 t3=tdeb;
00095 cout<<"Optimization time duration : "<<t3-t1<<" sec"<<endl;
00096 
00097                         I[0].Print_Mean_Covariance(-1);
00098                         Create_classfile();
00099                         Create_OutMeanCov();
00100                         Curent_images_index++;  
00101 
00102                         }
00103                  }
00104                  
00105         private:
00107                 int nb_images;
00109                 int dimx; int dimy; int ofx; int ofy; int Dy; int subx; int suby;
00111                 char *** PathOutPut;
00113                 char ***PathInPut;
00115                 int Curent_images_index;
00117                 char ***Current_Path;
00119                 Image<PIXEL> data;
00121                 Image<unsigned char> classfile;
00123                 Image<float> OutMeanCov;
00125                 Inliers *I;
00127                 Outliers *O;
00128                 /*--------------------------------------------------------------------------------------------*/
00130                 void Acquire_parameters_allocateImage(int dimx_tmp, int dimy_tmp, int ofx_tmp, int ofy_tmp, int Dy_tmp, int subx_tmp, int suby_tmp)
00131                 {
00132                 dimx=dimx_tmp; dimy=dimy_tmp; ofx=ofx_tmp;  ofy=ofy_tmp; Dy=Dy_tmp; subx=subx_tmp; suby=suby_tmp;
00133                 data.Allocate((int)(dimx/subx),(int)(dimy/suby),data.nb_bands,1);
00134                                         
00135                 }
00136                 /*--------------------------------------------------------------------------------------------*/
00138                 void Acquire_next_image()
00139                 {
00140                         int m;
00141                         for (m=0;m<data.nb_bands;m++){Current_Path[0][m]=PathInPut[Curent_images_index][m];}
00142                         data.Read_Cut_SubSample(Current_Path,ofx,ofy,Dy,subx,suby);
00143                         
00144                 }
00145                 /*--------------------------------------------------------------------------------------------*/
00147                 void Acquire_path(char * path_in, int nb_bands, char* path_fileout)  
00148                 {
00149                         
00150                         int m,k;
00151                         Image<unsigned char> pippo;
00152                         nb_images=pippo.NumberImages_pathfiles_in(path_in,nb_bands);
00153                         
00154                         PathInPut= new char **[nb_images];
00155                         for(m=0;m<nb_images;++m){PathInPut[m] =new char*[nb_bands];} 
00156                         for(m=0;m<nb_images;++m){for(k=0;k<nb_bands;++k){PathInPut[m][k]=new char[128];}}                                                       
00157                         pippo.Read_pathfiles_in(path_in,PathInPut,nb_bands);
00158 
00159                         Current_Path= new char **[1];
00160                         for(m=0;m<1;++m){Current_Path[m] =new char*[nb_bands];} 
00161                         for(m=0;m<1;++m){for(k=0;k<nb_bands;++k){Current_Path[m][k]=new char[128];}}                            
00162                         
00163                         data.nb_images=1;
00164                         data.nb_bands=nb_bands;
00165                         //dataSmoothBin.nb_images=1;
00166                         //dataSmoothBin.nb_bands=nb_bands;
00167                         
00168                         Acquire_Pathoutput(path_fileout);
00169                         
00170                 }
00171                 /*--------------------------------------------------------------------------------------------*/
00173                 void Acquire_Pathoutput(char *path_fileout)
00174                 {
00175                 
00176                 int m;
00177                         
00178                         PathOutPut= new char **[nb_images];
00179                         for(m=0;m<nb_images;++m){PathOutPut[m] =new char*[1];PathOutPut[m][0]=new char[128];    } 
00180                         
00181                         Image<unsigned char> pippo;
00182                         pippo.nb_bands=1;
00183                         pippo.nb_images=nb_images;
00184                         pippo.Read_pathfiles_out(path_fileout,PathOutPut);
00185                 
00186                 }
00187                 /*--------------------------------------------------------------------------------------------*/
00189                 void Allocate_OutMeanCov()
00190                 {
00191                         OutMeanCov.Allocate(255,(data.nb_bands+1)*data.nb_bands,1,1);   
00192                 }
00193                 /*--------------------------------------------------------------------------------------------*/
00195                 void Make_OutMeanCov()
00196                 {
00197                         int k,j,j1,j2;
00198                         //storing on the 1st line the nb of classes followed by zeros...
00199                         *(OutMeanCov.position)=I[0].A[0].nb_ref;
00200                         //storing on each line :
00201                         for (k=0;k<I[0].A[0].nb_ref;k++)
00202                         {
00203                                 for (j=0;j<data.nb_bands;j++)
00204                                 {
00205                                         //the cluster centroids
00206                                         *(OutMeanCov.position+(k+1)*OutMeanCov.ny+j)=I[0].A[0][k][j];//
00207 
00208                                 }
00209                                 for (j1=0;j1<data.nb_bands;j1++)
00210                                 {
00211                                         for (j2=0;j2<data.nb_bands;j2++)
00212                                         {
00213                                                 //the cluster covariance matrix
00214                                                         *(OutMeanCov.position+(k+1)*OutMeanCov.ny+(j1+1)*data.nb_bands+j2)=I[0].covariance_cluster[k][j1][j2];
00215                                         }
00216                                 }
00217                         }
00218                 }       
00219                 /*--------------------------------------------------------------------------------------------*/        
00221                 void Write_OutMeanCov()
00222                         {
00223                         char *path=new char[128];
00224                         strcpy(path,"./ClusterAttributes_00");
00225                         path[20]=char((int)(Curent_images_index+1)/10+48);
00226                         path[21]=char((Curent_images_index+1)%10+48);
00227                         OutMeanCov.Write_singleImage(path);
00228                         
00229                         cout<<" Cluster Attributes of image "<< Curent_images_index+1 << " written"<<endl;
00230                         delete [] path;
00231                         }
00232                 /*--------------------------------------------------------------------------------------------*/
00234                  void Create_OutMeanCov()
00235                  {
00236                         Make_OutMeanCov();
00237                         //Write_OutMeanCov();
00238                  }
00239                 
00240                 /*--------------------------------------------------------------------------------------------*/
00242                 void Allocate_classfile()
00243                 {
00244                         classfile.Allocate(data.nx,data.ny,1,1);        
00245                 }
00246                 /*--------------------------------------------------------------------------------------------*/
00248                 void Make_classfile()
00249                         {
00250                         unsigned long int i;
00251                         I[0].im_point=&data;
00252                         I[0].NearestRefVector_StdDevVar();
00253                         for (i=0;i<(unsigned long int)data.nx*(unsigned long int)data.ny;i++)
00254                                 {
00255                                 *(classfile.position+i)=255;
00256                                 
00257                                 }
00258                         for (i=0;i<I[0].nb_inliers;i++)
00259                                 {
00260                                 *(classfile.position+(I[0].Index_Inliers[i]))=(unsigned char)I[0].firstNearestA[i];
00261                                 
00262                                 }
00263                         
00264                         
00265                         }       
00266                 /*--------------------------------------------------------------------------------------------*/
00268                 void Write_classfile()
00269                         {
00270                         char ***path=new char **[1];
00271                         path[0] =new char*[1];
00272                         path[0][0]=new char[128];
00273                         path[0][0]=PathOutPut[Curent_images_index][0];
00274                         classfile.Write(path);
00275                         cout<<" Classfile of image "<< Curent_images_index+1 << " written"<<endl;
00276                         delete [] path[0][0];
00277                         }
00278                 /*--------------------------------------------------------------------------------------------*/
00280                  void Create_classfile()
00281                  {
00282                         Make_classfile();
00283                         Write_classfile();
00284                  }
00285                 
00286                 /*--------------------------------------------------------------------------------------------*/
00290                 void Begin_procedure(int nb)
00291                 {
00292                         Image<PIXEL>* im_point=&data;
00293                         O=new Outliers[1]();
00294                         I=new Inliers[1](im_point,O,nb);
00295                         
00296                         
00297                 }
00298                 /*--------------------------------------------------------------------------------------------*/
00299                 
00300 
00301 
00302 
00303 
00304 };
00305 /**********************************************************************************************/

Generated on Thu Feb 17 11:01:08 2005 for Gaussian Mixture Modeling by the MDL principle by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002