Main Page   File List   File Members  

GaussLUT.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*/
00016 /*---------------------------------------------------------------------------*/
00017 
00018  
00019 
00020 #include <iostream>
00021 #include <string>  
00022 #include <vector>
00023 #include <fstream>
00024 
00025 #include <cstdio>
00026 #include <cstdlib>
00027 #include <cmath>
00028 #include <cassert>
00029 #include <ctime>
00030 
00031 
00032 #define inf 1e7
00033 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
00034 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
00035 //#define coller(debut,fin) debut ## fin
00037 #define PIX unsigned short 
00038 
00041 void usage(std::string argv0) {
00042     std::cout << "usage: " << ">"
00043               << argv0 
00044               << " input_image_file_name output_image_file_name nb_bandsTimesNb_images dimx dimy " << std::endl; 
00045     std::cout << "usage for cutting: " 
00046               << "column_offset Line_offset DIMY_original"<< std::endl;
00047 
00048 }
00049 
00053 float gaussian_distribution (float x);
00055 float qromb(float (*func)(float), float a, float b);
00056 
00057 /**************************************************************************/
00078 int main(int argc, char *argv []){   
00079 
00080  std::string ifn1;
00081  std::string ifn2;
00082  unsigned long   int         nb_bands   = 0;
00083  unsigned long  int         dimx   = 0;
00084  unsigned long   int         dimy   = 0;
00085  unsigned long   int         ofx   = 0;
00086  unsigned long   int         ofy   = 0;
00087  unsigned long   int         Dy  = 0;
00088   
00089    switch (argc-1) {
00090    
00091         case 8:  Dy                     = atoi(argv[8]);
00092         case 7:  ofy                    = atoi(argv[7]);
00093         case 6:  ofx                    = atoi(argv[6]);
00094         case 5:  dimy                   = atoi(argv[5]);
00095         case 4:  dimx                   = atoi(argv[4]);
00096         case 3:  nb_bands               = atoi(argv[3]);
00097         case 2:  ifn2                   = std::string(argv[2]);
00098         case 1:  ifn1                   = std::string(argv[1]); break;
00099         case 0:
00100         default: usage(argv[0]); return -1;
00101     }
00102     if ((argc-1) < 5) { usage(argv[0]); return -1;}
00103     if ((argc-1) == 5) {Dy=dimy;} 
00104     std::cout << "> " << argv[0] 
00105               << " " << ifn1 
00106               << " " << ifn2 
00107               << " " << nb_bands 
00108               << " " << dimx 
00109               << " " << dimy 
00110               << " " << ofx
00111               << " " << ofy  
00112               << " " << Dy << std::endl;
00113 
00114 
00115 unsigned long  int i=0,j=0,k=0,l=0,m=0, n=0;
00116 unsigned long  int dim=dimx*dimy;
00117 
00118 
00119 
00120 /******************************Reading the input_path.txt and output_path.txt files************/
00121 printf("Reading path files\n");
00122 
00123   
00124 string line1;
00125 ifstream inaux;
00126 inaux.open(ifn1.c_str(),ifstream::in);
00127 ifstream in;
00128 in.open(ifn1.c_str(),ifstream::in);
00129         
00130 n=0;
00131 while(getline(inaux,line1)) {n++;}      
00132 unsigned long  int nb_images=n/nb_bands;  
00133 unsigned long  int nb_dim=nb_images*nb_bands;
00134 
00135 cout << "number of images : " << nb_images <<" "<<nb_bands<<std::endl;
00136 char ***path= new char **[nb_images];
00137 for(m=0;m<nb_images;++m){path[m] =new char*[nb_bands];} 
00138 for(m=0;m<nb_images;++m){for(k=0;k<nb_bands;++k){path[m][k]=new char[100];}}
00139 char ***save_path= new char **[nb_bands/3];
00140 for(m=0;m<nb_bands/3;++m){save_path[m] =new char*[3];} 
00141 for(m=0;m<nb_bands/3;++m){for(k=0;k<3;++k){save_path[m][k]=new char[100];}}
00142 
00143 for (i=0;i<(unsigned long int)nb_images;i++){
00144         for (j=0;j<(unsigned long int)nb_bands;j++){
00145                         getline(in,line1);
00146                         strcpy(path[i][j],(char *)line1.c_str());
00147                         std::cout<<path[i][j]<<std::endl;
00148                         
00149         }       
00150 }
00151 
00152 string line2;
00153 ifstream inaux2;
00154 inaux2.open(ifn2.c_str(),ifstream::in);
00155 ifstream in2;
00156 in2.open(ifn2.c_str(),ifstream::in);
00157 
00158 cout<<" GLUT Save Files :"<<endl;                               
00159 for (i=0;i<(unsigned long int)nb_bands/3;i++){
00160         for (j=0;j<(unsigned long int)1;j++){
00161                         getline(in2,line2);
00162                         strcpy(save_path[i][j],(char *)line2.c_str());
00163                         std::cout<<save_path[i][j]<<std::endl;
00164         }       
00165 }
00166 
00167 
00168 
00169 /*******************************Reading the images*********************************************/
00170 printf("Reading images\n");
00171  
00172                  
00173         PIX **a= new PIX *[dimx]; //vector clust contains the data
00174         for(m=0;m<dimx;++m)
00175              a[m] =new PIX[dimy];        
00176         float **Y= new float *[nb_dim]; //    float data        
00177         for(m=0;m<nb_dim;++m)
00178         { Y[m]=new float[dim];}
00179         
00180          char * ifn;
00181          FILE * ifp;
00182          m=0;n=0;
00183          for (l = 0 ;l <nb_dim; l++){
00184            ifn=path[m][n];printf("%s\n",ifn); // Reading  dimensions R, G and B
00185            ifp=NULL;
00186            assert (ifp=fopen(ifn,"r"));
00187            assert (0==fseek(ifp,(sizeof(PIX)*Dy*ofy+sizeof(PIX)*ofx), SEEK_SET));
00188            assert (dimy==fread(&a[0][0],sizeof (PIX),dimy,ifp));    
00189            for (i=1;i<dimx;i++){
00190              assert (0==fseek(ifp,(sizeof(PIX)*(Dy-dimy)), SEEK_CUR));
00191              assert (dimy==fread(&a[i][0],sizeof(PIX),dimy,ifp));
00192            }
00193            fclose(ifp); 
00194            if (n==nb_bands-1){n=0;m++;}
00195            else{n++;}
00196            for (i = 0 ;i < (dimx); i++){ k=(dimy)*i;          
00197              for (j = 0; j <(dimy); j++) { 
00198                Y[l][k]=a[i][j];
00199                k=k+1;
00200              }
00201            }
00202          }   
00203          for(m=0;m<dimx;++m) {delete [] a[m];}
00204          delete [] a;            
00205  /**********************************************************************************************************/ 
00206 
00207 unsigned char *sauv= new unsigned char [dim*3];
00208 
00209 float *maximum= new float [nb_bands];
00210 
00211 
00212 /************************************Equalisation for each images by bands*********************************************/ 
00213   printf("Equalisation\n"); 
00214   float **Repartition= new float *[nb_bands];
00215   for(m=0;m<nb_bands;++m)
00216       { Repartition[m]=new float[1000];}
00217   for (j=0;j<nb_bands;j++){
00218     maximum[j]=-inf;    
00219     for (i=0;i<dim;i++){ 
00220       for (l=0;l<(nb_dim/nb_bands);l++){
00221 
00222         if  (Y[l*nb_bands+j][i]>maximum[j]) {maximum[j]=Y[l*nb_bands+j][i];}
00223       }
00224     }
00225   }
00226   for (j=0;j<nb_bands;j++){  printf("%s %d %s %f\n","maximum[",(int)j,"]:",maximum[j]);}
00227   for (l=0;l<(nb_dim/nb_bands);l++){
00228     for (i=0;i<nb_bands;i++){for (j=0;j<1000;j++){Repartition[i][j]=0;}}
00229     for (j=0;j<nb_bands;j++){
00230       for (i=0;i<dim;i++){
00231         Repartition[j][(int)(Y[l*nb_bands+j][i])]= Repartition[j][(int)(Y[l*nb_bands+j][i])]+(1.0);
00232       }
00233     }
00234     for (i=0;i<nb_bands;i++){
00235       Repartition[i][0]=Repartition[i][0]; 
00236       for (j=1;j<1000;j++){
00237         Repartition[i][j]=Repartition[i][j]+Repartition[i][j-1];  
00238       }
00239     }
00240     for (j=0;j<nb_bands;j++){
00241       for (i=0;i<dim;i++){
00242         Y[l*nb_bands+j][i]= (float)((double)(Repartition[j][(int)Y[l*nb_bands+j][i]])/(double)dim);
00243       }
00244     }
00245   }
00246 
00247 
00248 /************************************ GAUSS LUT*********************************************/ 
00249  //adequate for 16 bits with range [0,1]
00250   printf("%s\n","Gaussian Look up Table");
00251   float *GRepartition= new float [65536];//16 bits
00252   float *Inv_repartition= new float [65536];//16 bits
00253   for (j=0;j<65536;j++){Inv_repartition[j]=0;}
00254   GRepartition[0]=qromb(&gaussian_distribution,-5,0);
00255   for (j=1;j<65536;j++){
00256     GRepartition[j]=GRepartition[j-1]+qromb(&gaussian_distribution,(float)((double)(j-1)*(double)((double)6/(double)65536)),(float)((double)j*(double)((double)6/(double)65536)));
00257     Inv_repartition[(unsigned long int)((double)GRepartition[j]*(double)65535+(double)0.5)]=j;
00258   }
00259   for (j=1;j<65536;j++){  //hole combling
00260     if (Inv_repartition[j]==0){
00261       Inv_repartition[j]=Inv_repartition[j-1];   
00262     }
00263   }
00264   for (l=0;l<(nb_dim/nb_bands);l++){
00265     for (j=0;j<nb_bands;j++){
00266       for (i=0;i<dim;i++){
00267         Y[l*nb_bands+j][i]= (unsigned long int)(Inv_repartition[(unsigned long int)((double)Y[l*nb_bands+j][i]*(double)65535+(double)0.5)]/(double)65535*(double)255);// output in 8 bits
00268      }
00269     }
00270   }
00271   
00272   printf("Saving results\n");
00273   for(m=0;m<nb_images;m++){
00274     for(l=0;l<nb_bands/3;l++){ 
00275       for (i=0;i<dim;i++){for(k=0;k<3;k++){sauv[i*3+k]=(unsigned char)Y[nb_bands*m+l*3+k][i]; }}
00276       ifp=NULL; 
00277       ifn=save_path[l][0];
00278       assert (ifp=fopen(ifn,"wb"));
00279       assert ( dim*3==fwrite(&sauv[0], sizeof(unsigned char),dim*3,ifp));
00280       assert (EOF!=fclose(ifp)); 
00281     }
00282   }
00283 
00284 
00285 /*********************************************************************************************************/
00286 
00287 
00288 }
00289 
00290 /*********************************************************************************************************/
00291 float gaussian_distribution (float x) {  
00292 float s=1;
00293 float m=3;
00294  float res=1/(sqrt(s*2*M_PI))*exp(-(x-m)*(x-m)/(2*s*s));
00295 return res;
00296 }
00297 /*********************************************************************************************************/

Generated on Thu Feb 17 11:04:22 2005 for Histogram dynamic adaptation of image time-series by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002