Main Page   Compound List   File List   Compound Members   File Members  

Image_basics.hh

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*/
00006 /*---------------------------------------------------------------------------*/
00007 #include <iostream>
00008 #include <fstream>
00009 #include <string>  
00010 #include <vector>
00011 
00012 #include <cstdio>
00013 #include <cstdlib>
00014 #include <cmath>
00015 #include <cassert>
00016 
00017 #define InfINI 1e20
00018 
00019 template<class PIX>
00022 class Image {
00023         
00024  unsigned long int i,j,k,l,n,m;
00025  public:
00027         int nx;
00029         int ny;
00031         int nb_bands;
00033         int nb_images;
00035         PIX *position;
00037         Image::Image()
00038         {
00039                 nx=0;
00040                 ny=0;
00041                 nb_images=0;
00042                 nb_bands=0;
00043                 nb_dim=0;
00044                 dim=0;
00046                 position=NULL;
00047                 
00048         }
00049                 
00051         void Allocate(int nx_temp, int ny_temp, int nb_bands_temp, int nb_images_temp)
00052         {
00053                 nx=nx_temp;
00054                 ny=ny_temp;
00055                 nb_bands=nb_bands_temp;
00056                 nb_images=nb_images_temp;
00057                 nb_dim=nb_bands*nb_images;
00058                 dim=nx*ny;
00059                 PIX *Y= new PIX[nb_dim*dim];    
00060                 position=&Y[0];
00061         }
00062         
00063         //virtual ~Image()
00064                 //{             
00065                 //delete [] position;   
00066                 //}
00067                 
00069         PIX Get_pixel_value(char *path,int ligne, int colonne, int ny)// Monoband image in file only
00070                 
00071         {
00072                 PIX a[1];
00073                 FILE * ifp= NULL;
00074                 assert(ifp=fopen(path,"r"));
00075                 assert(0==fseek(ifp,(sizeof(PIX)*ny*ligne+sizeof(PIX)*colonne), SEEK_SET));
00076                 assert(1==fread(&a[0],sizeof(PIX),1,ifp));              
00077                 fclose(ifp);
00078                 return a[0];    
00079         }
00081         void Read_pathfiles_in (char *pathfile_in,char *** path_images_in, int nb_bands_temp)
00082         {
00083                 
00084                 string line;
00085                 ifstream inaux;
00086                 inaux.open(pathfile_in,ifstream::in);
00087                 ifstream in;
00088                 in.open(pathfile_in,ifstream::in);
00089                 
00090                 
00091                 n=0;
00092                 while(getline(inaux,line)) {n++;} 
00093                 nb_bands=nb_bands_temp;
00094                 nb_images=n/nb_bands;  
00095                 nb_dim=nb_images*nb_bands;
00096                 std::cout << "number of images :" << nb_images<< ",  nb_bands : " << nb_bands <<std::endl;
00097                 std::cout<<" input path : "<<std::endl;;
00098                 for (i=0;i<(unsigned long int)nb_images;i++){
00099                   for (j=0;j<(unsigned long int)nb_bands;j++){
00100                         getline(in,line);
00101                         strcpy(path_images_in[i][j],(char *)line.c_str());
00102                         std::cout<<path_images_in[i][j]<<std::endl;
00103                  }      
00104                 }       
00105                 
00106         }
00108         int NumberImages_pathfiles_in (char *pathfile_in, int nb_bands_temp)
00109         {
00110                 
00111                 string line;
00112                 ifstream in;
00113                 in.open(pathfile_in,ifstream::in);
00114                 
00115                 int n=0;
00116                 while (getline(in,line)) {n++;}
00117                 nb_bands=nb_bands_temp;
00118                 nb_images=n/nb_bands;  
00119                 nb_dim=nb_images*nb_bands;
00120                 
00121                 return nb_images;
00122         }
00124         void Read_pathfiles_out (char *pathfile_out, char ***path_images_out)
00125         {
00126                 
00127                 ifstream out;
00128                 out.open(pathfile_out,ifstream::in);
00129                         
00130                 string line;
00131                 for (i=0;i<(unsigned long int)nb_images;i++){
00132                         for (j=0;j<(unsigned long int)nb_bands;j++){
00133                                 getline(out,line);
00134                                 strcpy(path_images_out[i][j],(char *)line.c_str());
00135                                 std::cout << "save path : " << path_images_out[i][j]<< std::endl;
00136                         }
00137                 }
00138         
00139         }
00141         void Read_Cut_SubSample(char ***path_images_in,int ofx,int ofy,int Dy,int subx, int suby)
00142         {
00143                 
00144                 unsigned long  int jump=0;
00145                 int oldny=(int)(ny*suby);
00146                 
00147                 std::cout << "size image output:  " <<nx <<"x"<< ny << "x"<< nb_images  <<std::endl;
00148                 
00149                 
00150 
00151                 PIX **a= new PIX *[nx]; //vector clust contains the data
00152                 for(m=0;m<(unsigned long int)nx;++m)
00153                    a[m] =new PIX[ny];    
00154         
00155                 char *ifn=new char[128];
00156                 FILE * ifp;
00157                 m=0;n=0;
00158                 for (l = 0 ;l <(unsigned long int)nb_dim; l++){
00159                 
00160                         strcpy(ifn,path_images_in[m][n]);std::cout<<"Reading image "<<ifn<<std::endl; 
00161                         ifp=NULL;
00162                         assert (ifp=fopen(ifn,"r"));
00163                         assert (0==fseek(ifp,(sizeof(PIX)*Dy*ofx+sizeof(PIX)*ofy), SEEK_SET));
00164                         for (jump = 0 ;jump <(unsigned long int)ny; jump++){
00165                                 assert (1==fread(&a[0][jump],sizeof (PIX),1,ifp));    
00166                                 assert (0==fseek(ifp,(sizeof(PIX)*(suby-1)), SEEK_CUR));
00167                         }          
00168                         for (i=1;i<(unsigned long int)nx;i++){
00169                                 assert (0==fseek(ifp,(sizeof(PIX)*(Dy-oldny)+sizeof(PIX)*Dy*(subx-1)), SEEK_CUR));           
00170                                 for (jump = 0 ;jump <(unsigned long int)ny; jump++){
00171                                         assert (1==fread(&a[i][jump],sizeof (PIX),1,ifp));    
00172                                         assert (0==fseek(ifp,(sizeof(PIX)*(suby-1)), SEEK_CUR));
00173                                 }
00174              
00175                         }
00176                         fclose(ifp); 
00177                         if (n==(unsigned long int)nb_bands-1){n=0;m++;}
00178                         else{n++;}
00179                         for (i = 0 ;i < (unsigned long int)nx; i++){ k=(ny)*i;          
00180                                 for (j = 0; j <(unsigned long int)ny; j++) {    
00181                                         *(position+l*dim+k)=a[i][j];
00182                                         k=k+1;
00183                                 }
00184                         }
00185                 }   
00186                 for(m=0;m<(unsigned long int)nx;++m) {delete [] a[m];}
00187                 delete [] a;    
00188                 delete [] ifn;
00189                 
00190         }
00191         
00193         void Write(char ***path_images_out)
00194         {
00195                 PIX *sauv= new PIX [dim];
00196                 FILE *ifp;
00197                 char *ifn;
00198                   for(m=0;m<(unsigned long int)nb_images;m++){
00199                           for(l=0;l<(unsigned long int)nb_bands;l++){ 
00200                                 for (i=0;i<dim;i++){sauv[i]=*(position+dim*(m+l)+i); }  
00201                                 ifp=NULL; 
00202                                 ifn=path_images_out[m][l];
00203                                 assert (ifp=fopen(ifn,"wb"));
00204                                 assert ( dim==fwrite(&sauv[0], sizeof(PIX),dim,ifp));
00205                                 assert (EOF!=fclose(ifp)); 
00206                           }
00207                    }
00208                  delete [] sauv;
00209         }
00211         void Write_singleImage(char *path_images_out)
00212         {
00213                 PIX *sauv= new PIX[dim];
00214                 FILE *ifp;
00215                 char *ifn;
00216                   for(m=0;m<(unsigned long int)nb_images;m++){
00217                           for(l=0;l<(unsigned long int)nb_bands;l++){ 
00218                                 for (i=0;i<dim;i++){sauv[i]=*(position+dim*(m+l)+i);}   
00219                                 ifp=NULL; 
00220                                 ifn=path_images_out;
00221                                 assert (ifp=fopen(ifn,"wb"));
00222                                 assert ( dim==fwrite(&sauv[0], sizeof(PIX),dim,ifp));
00223                                 assert (EOF!=fclose(ifp)); 
00224                           }
00225                    }
00226                    
00227                  delete [] sauv;
00228         }
00230         float MAX(int image_nb_m)
00231         {
00232                 float m=-InfINI,aux;
00233                 for (i=0;i<dim;i++){aux=*(position+dim*(image_nb_m)+i);if (aux> m){m=aux;}}
00234                 return m;       
00235         }
00237         float MIN(int image_nb_m)
00238         {
00239                 float m=InfINI,aux;
00240                 for (i=0;i<dim;i++){aux=*(position+dim*(image_nb_m)+i);if (aux< m){m=aux;}}
00241                 return m;       
00242         }
00244         float Mean(int image_nb_m)
00245         {
00246                  float sum=0,aux;
00247                 for (i=0;i<dim;i++){sum=*(position+dim*(image_nb_m)+i)+sum; }
00248                 aux=sum/(float)dim;
00249                 return aux;     
00250         }
00252         float Stdev(int image_nb_m)
00253         {
00254                  float sum=0,aux,moy;
00255                  moy=Mean(image_nb_m);
00256                 for (i=0;i<dim;i++){sum=(*(position+dim*(image_nb_m)+i)-moy)*(*(position+dim*(image_nb_m)+i)-moy)+sum; }          
00257                 aux=sqrt(sum/(float)dim);
00258                 return aux;     
00259         }
00261         float Cov(int image_nb_m1,int image_nb_m2)
00262         {
00263                  float sum=0,aux,moy1,moy2;
00264                  moy1=Mean(image_nb_m1);
00265                  moy2=Mean(image_nb_m2);
00266                  
00267                 for (i=0;i<dim;i++){sum=(*(position+dim*(image_nb_m1)+i)-moy1)*(*(position+dim*(image_nb_m2)+i)-moy2)+sum; }      
00268                 aux=(sum/(float)dim);
00269                 return aux;     
00270         }
00272         void  Normalize()
00273         {
00274                   float moy, dev;
00275                   PIX aux;
00276                   for(int m=0;m<nb_images*nb_bands;m++){
00277                         moy=Mean(m);
00278                         dev=Stdev(m);
00279                         for (i=0;i<dim;i++)
00280                         {
00281                                 aux=GetPixel(m,x,0);
00282                                 PutPixel(m,x,0,(PIX)(aux-moy)/dev);     
00283                         }
00284                   }
00285                   
00286                 
00287         }
00288 
00289 
00290 private:
00291                 
00293                 int nb_dim;
00295                 unsigned long int dim;
00296                 
00298                 PIX Min(PIX a,PIX b)
00299                 {
00300                         PIX res;
00301                         if (a<b){res=a;}
00302                         else{res=b;}
00303                         return res;
00304                 
00305                 }
00306 
00307         };
00308         
00309                         

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