Main Page   Compound List   File List   Compound Members   File Members  

MakeClusterAtt.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*/
00016 /*---------------------------------------------------------------------------*/
00017 #include <iostream>
00018 #include <string>  
00019 #include <vector>
00020 #include <fstream>
00021 
00022 #include <cstdio>
00023 #include <cstdlib>
00024 #include <cmath>
00025 #include <cassert>
00026 #include <ctime>
00027 
00028 #include "Image_basics.hh"
00029 
00030 #define PIX  float
00031 
00032 using namespace std;
00033 
00038 class Mapping
00039 {               
00040         public:
00042                 void Acquire(char * path_in, char * path_in_class, char * path_out, int dimx_tmp, int dimy_tmp,int ofx_tmp, int ofy_tmp, int Dy_tmp, int subx_tmp, int suby_tmp) 
00043                 {
00044                         Acquire_path(path_in,path_in_class,path_out);
00045                         Acquire_parameters_allocateImage(dimx_tmp,dimy_tmp,ofx_tmp,ofy_tmp,Dy_tmp,subx_tmp,suby_tmp);
00046                         Allocate_OutMeanCov();
00047                         
00048                 }
00049                 
00050                 /*--------------------------------------------------------------------------------------------*/
00052                  void Create()
00053                  {
00054                         Calculate();
00055                         Write_OutMeanCov();
00056                  }
00057                 
00058                 /*--------------------------------------------------------------------------------------------*/
00059                 
00060         private:
00061 
00062                 /*--------------------------------------------------------------------------------------------*/ 
00064                 Image<PIX> data;
00066                 Image<PIX> CurrentIm;// to be removed because using a single image
00068                 Image<unsigned char> classfile;
00070                 Image<float> OutMeanCov;
00072                 char * PathOutPut;
00074                 char ***PathInPut;
00076                 char ***PathInPutClass;
00078                 unsigned long int * effectifSave;
00079                 /*--------------------------------------------------------------------------------------------*/
00080                 
00081                 
00082                 /*--------------------------------------------------------------------------------------------*/
00084                 void Acquire_path(char * path_in, char *path_in_class,char * path_out)  
00085                 {
00086                         
00087                         int m,k,nb_images=1;
00088                         Image<unsigned char> pippo;
00089                         int nb_bands=pippo.NumberImages_pathfiles_in(path_in,nb_images);
00090 
00091                         PathInPut= new char **[nb_images];
00092                         for(m=0;m<nb_images;++m){PathInPut[m] =new char*[nb_bands];} 
00093                         for(m=0;m<nb_images;++m){for(k=0;k<nb_bands;++k){PathInPut[m][k]=new char[128];}}                                                       
00094                         pippo.Read_pathfiles_in(path_in,PathInPut,nb_bands);
00095 
00096                         data.nb_images=1;
00097                         data.nb_bands=nb_bands;
00098 
00099                         PathInPutClass=new char** [1];PathInPutClass[0]=new char*[1];
00100                         PathInPutClass[0][0]=path_in_class;
00101                         PathOutPut=path_out;
00102                                                                 
00103                         
00104                 }               
00105                 /*--------------------------------------------------------------------------------------------*/
00107                 void Acquire_parameters_allocateImage(int dimx, int dimy, int ofx, int ofy, int Dy, int subx, int suby)
00108                 {
00109                         data.Allocate((int)(dimx/subx),(int)(dimy/suby),data.nb_bands,1);
00110                         CurrentIm.Allocate((int)(dimx/subx),(int)(dimy/suby),data.nb_bands,1);
00111                         classfile.Allocate((int)(dimx),(int)(dimy),1,1);
00112                         data.Read_Cut_SubSample(PathInPut,ofx,ofy,Dy,subx,suby);
00113                         classfile.Read_Cut_SubSample(PathInPutClass,0,0,dimy,1,1);
00114                         
00115                 }
00116                 /*--------------------------------------------------------------------------------------------*/
00118                 void Allocate_OutMeanCov()
00119                 {
00120                         OutMeanCov.Allocate(255,(data.nb_bands+1)*data.nb_bands,1,1);
00121                         for (int j=0;j<(data.nb_bands+1)*data.nb_bands*255;j++)
00122                                 *(OutMeanCov.position+j)=0;
00123                 }
00124                 /*--------------------------------------------------------------------------------------------*/
00126                 void Calculate()
00127                 {
00128                         int k,j,j1,j2;
00129                         int nb_class=(int)classfile.MAX(0)+1;
00130                         //cout<<"nb class : "<<nb_class<<endl;
00131                         float *mean=new float[data.nb_bands];
00132                         int auxB;
00133                         float auxCov;
00134                         unsigned long int effectif;
00135                         //for (j=0;j<data.nb_bands;j++){mean[j]=data.Mean(j);cout<<"Mean "<<j<<" : "<<mean[j]<<endl;}
00136                         //storing on the 1st line the nb of classes followed by zeros...
00137                         *(OutMeanCov.position)=nb_class;
00138                         effectifSave=new unsigned long int[nb_class];
00139                         unsigned long int nbSample=data.nx*data.ny;     
00140                 
00141                         for (k=0;k<nb_class;k++)
00142                         {
00143                                 effectif=0;
00144                                 for (unsigned long int i=0;i<nbSample;i++)
00145                                 {
00146                                           auxB=*(classfile.position+i);
00147                                         
00148                                           if (auxB==k)
00149                                           {     
00150                                                 effectif++;
00151                                                 for (j=0;j<data.nb_bands;j++){
00152                                                    *(CurrentIm.position+(j)*nbSample+i)=*(data.position+(j)*nbSample+i);
00153                                                 }
00154                                           }
00155                                           else
00156                                           {
00157                                                 for (j=0;j<data.nb_bands;j++){
00158                                                    *(CurrentIm.position+(j)*nbSample+i)=0;
00159                                                 }
00160                                           }
00161                                         
00162                                 }
00163                                 effectifSave[k]=effectif;
00164                                 for (j=0;j<data.nb_bands;j++){mean[j]=(CurrentIm.Mean(j)*nbSample/(float)effectif);}
00165                                 
00166                                 //storing on each line :
00167                                 for (j=0;j<data.nb_bands;j++)
00168                                 {
00169                                         //the cluster centroids
00170                                         *(OutMeanCov.position+(k+1)*OutMeanCov.ny+j)=mean[j];//
00171         
00172                                 }
00173                                 for (j1=0;j1<data.nb_bands;j1++)
00174                                 {
00175                                         for (j2=j1;j2<data.nb_bands;j2++)
00176                                         {
00177                                             auxCov=0;
00178                                             for (unsigned long int i=0;i<nbSample;i++){
00179                                               auxB=*(classfile.position+i);
00180                                               if (auxB==k){
00181                                                    auxCov=auxCov+(*(CurrentIm.position+(j1)*nbSample+i)-mean[j1])*(*(CurrentIm.position+(j2)*nbSample+i)-mean[j2]);     
00182                                               }
00183                                             }
00184                                             auxCov=(auxCov/(float)effectif);
00185                                            //the cluster covariance matrix
00186                                           *(OutMeanCov.position+(k+1)*OutMeanCov.ny+(j1+1)*data.nb_bands+j2)=auxCov;
00187                                         }
00188                                 }
00189                                 for (j1=0;j1<data.nb_bands;j1++){
00190                                         for (j2=j1;j2<data.nb_bands;j2++){
00191                                                 *(OutMeanCov.position+(k+1)*OutMeanCov.ny+(j2+1)*data.nb_bands+j1)=*(OutMeanCov.position+(k+1)*OutMeanCov.ny+(j1+1)*data.nb_bands+j2);
00192                                         }
00193                                 }
00194                                 
00195 
00196                         }
00197                         
00198                         
00199                         
00200                 }
00201                         
00203                 void Write_OutMeanCov()
00204                         {
00205                         
00206                         OutMeanCov.Write_singleImage(PathOutPut);
00207                         cout<<data.nb_bands<<" "<<*(OutMeanCov.position)<<endl;
00208                         for (int k=0;k<*(OutMeanCov.position);k++){
00209                                 cout<<k<<" "<<(float)effectifSave[k]/(float)(data.nx*data.ny)<<endl;
00210                                 for (int j=0;j<data.nb_bands;j++){
00211                                         cout<<*(OutMeanCov.position+(k+1)*OutMeanCov.ny+j)<<" ";
00212                                 }
00213                                 cout<<""<<endl;
00214                                 for (int j1=0;j1<data.nb_bands;j1++)
00215                                 {
00216                                         for (int j2=0;j2<data.nb_bands;j2++)
00217                                         {
00218                                                 cout<<*(OutMeanCov.position+(k+1)*OutMeanCov.ny+(j1+1)*data.nb_bands+j2)<<" ";
00219                                         }
00220                                 }
00221                                 cout<<""<<endl;
00222                         }
00223                         }
00224                 /*--------------------------------------------------------------------------------------------*/        
00225                 
00226 }; 
00227 
00228 
00229 
00232 void usage(std::string argv0) {
00233     std::cout << "\nusage:                              " << ">"
00234               << argv0 
00235               << "input_images_file_name input_image_classfile.cl output_classfile.att nb_lignes ncolonnes " << std::endl; 
00236     std::cout << "option for cutting:            " 
00237               << "offset_colonnes offset_lignes nb_colonnes_original"<< std::endl;
00238     std::cout << "option for subsampling :       " 
00239               << "sub_sample_ligne sub_sample_colonne ([ny]div[ny] must = 0 )"<< std::endl;
00240   }
00241 
00260 int main(int argc, char *argv []){    
00261 std::string ifn1;
00262 std::string ifn2;
00263 std::string ifn3;
00264 unsigned long  int          dimx           = 0;
00265 unsigned long   int         dimy           = 0;
00266 unsigned long   int         ofx            = 0;
00267 unsigned long   int         ofy            = 0;
00268 unsigned long   int         Dy             = 0;
00269 unsigned long   int         subx           = 1;
00270 unsigned long   int         suby           = 1;
00271 switch (argc-1) {
00272         case 10: suby                   = atoi(argv[10]);
00273         case 9:  subx                   = atoi(argv[9]);
00274         case 8:  Dy                     = atoi(argv[8]);
00275         case 7:  ofy                    = atoi(argv[7]);
00276         case 6:  ofx                    = atoi(argv[6]);
00277         case 5:  dimy                   = atoi(argv[5]);
00278         case 4:  dimx                   = atoi(argv[4]);
00279         case 3:  ifn3                   = std::string(argv[3]);
00280         case 2:  ifn2                   = std::string(argv[2]);
00281         case 1:  ifn1                   = std::string(argv[1]); break;
00282         case 0:
00283         default: usage(argv[0]); return -1;
00284     }
00285     if ((argc-1) < 5) { usage(argv[0]); return -1;}
00286     if ((argc-1)==5) {Dy=dimy;} 
00287     std::cout << "> " << argv[0] 
00288               << " " << ifn1 
00289               << " " << ifn2
00290               << " " << ifn3
00291               << " " << dimx 
00292               << " " << dimy 
00293               << " " << ofx
00294               << " " << ofy  
00295               << " " << Dy 
00296               << " " << subx  
00297               << " " << suby<< std::endl;
00298          
00299 /*******mapping **********************************************************/
00300         srand((unsigned)time(NULL));
00301         time_t tdeb, t1,t2;
00302         time(&tdeb);
00303         t1=tdeb;
00304         Mapping map;
00305         map.Acquire((char *)ifn1.c_str(),(char *)ifn2.c_str(),(char *)ifn3.c_str(),dimx,dimy,ofx,ofy,Dy,subx,suby);
00306         map.Create();
00307         time(&t2);
00308         return 0;
00309 }
00310 
00311 /*****************************************************************/

Generated on Thu Feb 17 11:02:29 2005 for Generation of a Classification Attribute File by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002