fonctions_de_base_classifieurs.c

Go to the documentation of this file.
00001 /*
00002 *    Copyright (c) 2007. The BATI team. All right reserved.
00003 *
00004 *    This file is part of BATI library.
00005 *
00006 *    BATI library is free software: you can redistribute it and/or modify
00007 *    it under the terms of the GNU General Public License as published by
00008 *    the Free Software Foundation, either version 3 of the License, or
00009 *    (at your option) any later version.
00010 *
00011 *    BATI library  is distributed in the hope that it will be useful,
00012 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *    GNU General Public License for more details.
00015 *
00016 *    You should have received a copy of the GNU General Public License
00017 *    along with BATI library.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 /* ************************************************************* */
00020 /*              fonctions_de_base_classifieurs.c                 */
00021 /* ************************************************************* */
00022 /*                                                               */
00023 /*      Fonction d'allocation et de transformation               */
00024 /*               des données en image                            */
00025 /*                                                               */
00026 /*  Dernière modif : Ramasso E. & Jullien S.  08/12/03           */
00027 /* ************************************************************* */
00028 
00029 
00030 #include "classifier.h"
00031 #include "macros.h"
00032 
00051 /* **********  ALLOCATION DONNEES ENTREE EN PLUSIEURS FOIS ***************/
00069 int alloc_classifier_data_input_one_by_one(data_input *data)
00070 {
00071   /* alloue le champ attributes de data */
00072   /* le champ nb_pts doit etre renseigne par format_XXXXX_classifier_input_init */
00073 
00074   if(data->nb_attr == 1)
00075     {
00076       if( (data->attributes = (vector *)malloc(sizeof(vector))) == NULL )
00077         {
00078           printf ("\n allocation attributs impossible\n");  
00079           exit(1);
00080         }
00081     }
00082 
00083   else 
00084     {   
00085       if( (data->attributes = (vector *)realloc(data->attributes,(data->nb_attr)*(sizeof(vector)))) == NULL )
00086         {
00087           printf ("\n allocation attributs impossible\n");  
00088           exit(1);
00089         }
00090     }
00091 
00092  
00093   /* tableau 1D donnant l'image en 1 seul bloc, ligne apres ligne       */
00094   if( (data->attributes[data->nb_attr - 1] = (vector)malloc((data->nb_pts)*(sizeof(float)))) == NULL)
00095     {
00096       printf ("\n allocation image impossible\n");  
00097       exit(1);
00098     }
00099 
00100   return 0;
00101 }
00102 
00103 
00104 
00105 /* **********  ALLOCATION DONNEES ENTREE EN UNE FOIS ***************/
00122 int alloc_classifier_data_input_completely(data_input *data)
00123 {
00124   int i;
00125 
00126   /* alloue le champ attributes de data */
00127   /* le champ nb_pts doit être renseigné par format_XXXXX_classifier_input_init */
00128 
00129   if(data->nb_attr != 0)
00130     {
00131       if( (data->attributes = (vector *)malloc(data->nb_attr*sizeof(vector))) == NULL )
00132         {
00133           printf ("\n allocation attributs impossible\n");  
00134           exit(1);
00135         }
00136     }
00137   else 
00138     {
00139       printf("champ nombre d attribut non rempli");
00140       exit(1);
00141     }
00142 
00143   /* on alloue une place memoire pour les donnees de chaque attribut      */
00144 
00145   for(i=0;i<data->nb_attr;i++)
00146     if( (data->attributes[i] = (vector)malloc((data->nb_pts)*(sizeof(float)))) == NULL)
00147       {
00148         printf ("\n allocation points impossible\n");  
00149         exit(1);
00150       }
00151 
00152   return 0;
00153 }
00154 
00156 
00157 
00158 
00159 
00164 /* *************************  ALLOCATION DONNEES SORTIE  ***************/
00179 int alloc_classifier_data_output(data_output *dataO)
00180 {
00181   int i;
00182 
00183   if(dataO->type == FUZZY_CHOICE)
00184     {
00185       if( (dataO->classes.fuzzy = (vector *)malloc(dataO->nb_class*sizeof(vector))) == NULL )
00186         {
00187           printf ("\n allocation classes fuzzy sortie impossible\n");  
00188           exit(1);
00189         }
00190 
00191       for(i=0;i<dataO->nb_class;i++)
00192         if( (dataO->classes.fuzzy[i] = (vector)malloc((dataO->nb_pts)*(sizeof(float)))) == NULL)
00193           {
00194             printf ("\n allocation points sortie fuzzy impossible\n");  
00195             exit(1);
00196           }
00197     }
00198   
00199   if(dataO->type == CRISP_CHOICE)
00200     {
00201       
00202       if( (dataO->classes.crisp = (int*)malloc((dataO->nb_pts)*(sizeof(int)))) == NULL)
00203         {
00204           printf ("\n allocation points sortie crisp impossible\n");  
00205           exit(1);
00206         }
00207     }
00208 
00209   return 0;
00210 }
00212 
00213 
00214 
00215 
00216 
00217 
00218 
00222 /* **********  TRANSFORMATION DONNEES NETTES EN IMAGE U1 ***************/
00237 int data_output_crisp_to_imageu1(data_output dataOC, imau1 *image_u1)
00238 {
00239   int i,j;
00240 
00241   /* allouer image de sortie */
00242   image_u1->nr = dataOC.equ_nb_rows;
00243   image_u1->nc = dataOC.equ_nb_cols;
00244   alloc_imau1(image_u1);
00245  
00246 
00247   if(dataOC.type == CRISP_CHOICE)
00248     {
00249       /* transferer les donnees */
00250       for(i=0;i<dataOC.equ_nb_rows;i++)
00251         for(j=0;j<dataOC.equ_nb_cols;j++)
00252            image_u1->p[i][j] = (pixu1)dataOC.classes.crisp[i*dataOC.equ_nb_cols + j];
00253     }
00254   else
00255     {
00256       printf("data_output_crisp_to_imageu1 : Resultat du mauvais type");
00257       exit(1);
00258     }
00259 
00260   return 0;
00261 }
00262 
00263 
00264 
00265 
00266 /* **********  TRANSFORMATION DONNEES FLOUES EN IMAGE U1 ***************/
00284 int data_output_fuzzy_to_imageu1(data_output dataOF, imau1 **image_u1)
00285 {
00286   int i,j,k;
00287 
00288   /* allocations des images de sorties */
00289   if(((*image_u1) = (imau1*)malloc(dataOF.nb_class*sizeof(imau1)))==NULL)
00290     {
00291       printf("\nErreur allocation images 2 dans data_output_fuzzy_to_imageu1 impossible\n");
00292       exit(1);
00293     }
00294 
00295 
00296   for(i=0;i<dataOF.nb_class;i++)
00297     {
00298       (*image_u1)[i].nc = dataOF.equ_nb_cols;
00299       (*image_u1)[i].nr = dataOF.equ_nb_rows;
00300       alloc_imau1(&((*image_u1)[i]));
00301     }
00302   
00303   /* remplissage */
00304   if(dataOF.type == FUZZY_CHOICE)
00305     {
00306       for(k=0;k<dataOF.nb_class;k++)
00307         for(i=0;i<dataOF.equ_nb_rows;i++)
00308           for(j=0;j<dataOF.equ_nb_cols;j++)
00309             (*image_u1)[k].p[i][j] = (int)(dataOF.classes.fuzzy[k][i*dataOF.equ_nb_cols + j]*255.0);
00310       
00311     }
00312   else
00313     {
00314       printf("data_output_fuzzy_to_imageu1 : Resultat du mauvais type");
00315       exit(1);
00316     }
00317 
00318   return 0;
00319 }
00321 
00322 
00323 
00324 
00325 
00329 
00330 /* **********  TRANSFORMATION DONNEES FLOUES EN IMAGE FL ***************/
00348 int data_output_fuzzy_to_imagefl(data_output dataOF, imafl *image_fl)
00349 {
00350   int i,j,k;
00351 
00352   
00353   if(dataOF.type == FUZZY_CHOICE)
00354     {
00355       for(k=0;k<dataOF.nb_class;k++)
00356         for(i=0;i<dataOF.equ_nb_rows;i++)
00357           for(j=0;j<dataOF.equ_nb_cols;j++)
00358             image_fl[k].p[i][j] = (pixfl)dataOF.classes.fuzzy[k][i*dataOF.equ_nb_cols + j];
00359       
00360     }
00361   else
00362     {
00363       printf("Resultat du mauvais type");
00364       exit(1);
00365     }
00366 
00367   return 0;
00368 }
00369 
00371 
00375 /* **********************LIBERATION DONNEES D ENTREE************************ */
00386 int free_data_input(data_input *dataI)
00387 {
00388   int i;
00389   for(i=0;i<dataI->nb_attr;i++)
00390     free(dataI->attributes[i]);
00391       
00392 
00393   free(dataI->attributes);
00394   return(0);
00395 }
00396 /* **********************LIBERATION DONNEES DE SORTIE************************ */
00409 int free_data_output(data_output *dataO)
00410 {
00411   int i;
00412 
00413  if(dataO->type == CRISP_CHOICE)
00414       free(dataO->classes.crisp);
00415      
00416  else 
00417    {
00418      if(dataO->type == FUZZY_CHOICE)
00419        {
00420          for(i=0;i<dataO->nb_class;i++)
00421            free(dataO->classes.fuzzy[i]);
00422          
00423          free(dataO->classes.fuzzy);
00424        }
00425      
00426      else 
00427        {
00428          printf("\nERREUR : free_data_output : Type inconnu \n");
00429          exit(1);
00430        }
00431    }
00432 }
00433 
00434 /* **********************NORMALISATION DE DATA INPUT************************ */
00452 int normaliser_data_input_sur_a_b(float a, float b, data_input in, data_input *out)
00453 {
00454   int i,j;
00455   float min,max;
00456 
00457   if(in.nb_pts != out->nb_pts)
00458     ERREUR("In et Out de taille differente\n");
00459 
00460   if(in.nb_attr != out->nb_attr)
00461     ERREUR("In et Out : nb attributs differents");
00462   
00463   if(a == b)
00464      ERREUR("a et b sont egaux => normalisation non logique\n");
00465 
00466   for(j=0;j<in.nb_attr;j++)
00467     {
00468       /* chercher le max et le min */
00469       min = in.attributes[j][0];
00470       max =in.attributes[j][0];
00471       
00472       for(i=1; i<in.nb_pts;i++)
00473         {
00474           max = (( in.attributes[j][i]) > max ?  in.attributes[j][i] : max);
00475           min  = (( in.attributes[j][i]) <  min  ?  in.attributes[j][i] : min);
00476           if(min == a && max == b)
00477             {
00478               printf("Normalisation : fin -> deja normalise sur intervalle specifie [%1.2f,%1.2f]!\n",a,b);
00479               return NO_ERROR;
00480             }
00481         }
00482 
00483       /* normaliser */
00484       for(i=0; i<in.nb_pts;i++)
00485         {
00486           /* normaliser sur [0 1] */
00487           out->attributes[j][i] = ( in.attributes[j][i] - min ) / ( max - min );
00488           
00489           if((a != 0 && b != 1) || (a != 1 && b != 0))
00490             {
00491               /* normaliser sur [a b] */
00492               if(a < b) out->attributes[j][i] = out->attributes[j][i] * (b - a) + a;
00493               
00494               else  out->attributes[j][i] = out->attributes[j][i] * (a - b) + b; 
00495               
00496             }
00497         }       
00498       
00499     }
00500   
00501   
00502   if(a<b)
00503     printf("\nNormalisation data input sur [%1.3f,%1.3f] faite.\n",a,b);
00504   
00505   if(a>b)
00506     printf("\nNormalisation data input sur [%1.3f,%1.3f] faite.\n",b,a);
00507 
00508   return NO_ERROR;
00509 }
00510 
00511 
00512 
00513 
00514 
00515 /* **********************NORMALISATION DE DATA OUTPUT************************ */
00533 int normaliser_data_output_sur_a_b(float a, float b, data_output in, data_output *out)
00534 {
00535   int i,j;
00536   float min,max;
00537 
00538   if(in.nb_pts != out->nb_pts)
00539     ERREUR("In et Out de taille differente\n");
00540 
00541   if(in.type != out->type)
00542     ERREUR("In et Out de type differents");
00543   
00544   if(a == b)
00545      ERREUR("a et b sont egaux => normalisation non logique\n");
00546 
00547 
00548   switch(in.type)
00549     {
00550     case CRISP_CHOICE :
00551       /* sur chaque classe faire */
00552       
00553       /* chercher le max et le min */
00554       min = in.classes.crisp[0];
00555       max = in.classes.crisp[0];
00556       
00557       for(i=1; i<in.nb_pts;i++)
00558         {
00559           max = (( in.classes.crisp[i]) > max ?  in.classes.crisp[i] : max);
00560           min  = (( in.classes.crisp[i]) <  min  ?  in.classes.crisp[i] : min);
00561           if(min == a && max == b)
00562             {
00563               printf("Normalisation : fin car deja normalise sur intervalle specifie [%1.2f,%1.2f]!\n",a,b);
00564               return NO_ERROR;
00565             }
00566         }
00567       
00568       /* normaliser sur [0 1] puis sur [a b]*/
00569       for(i=0; i<in.nb_pts;i++)
00570         {
00571           /* normaliser sur [0 1] */
00572           out->classes.crisp[i] = ( in.classes.crisp[i] - min ) / ( max - min );
00573           
00574           if((a != 0 && b != 1) || (a != 1 && b != 0))
00575             {
00576               /* normaliser sur [a b] */
00577               if(a < b) out->classes.crisp[i] = out->classes.crisp[i] * (b - a) + a;
00578 
00579               else  out->classes.crisp[i] = out->classes.crisp[i] * (a - b) + b; 
00580               
00581             }
00582         }       
00583       
00584       break;
00585 
00586     case FUZZY_CHOICE :
00587       
00588       /* sur chaque classe faire */
00589       for(j=0;j<in.nb_class;j++)
00590         {
00591           /* chercher le max et le min */
00592           min = in.classes.fuzzy[j][0];
00593           max = in.classes.fuzzy[j][0];
00594           
00595           for(i=1; i<in.nb_pts;i++)
00596             {
00597               max = (( in.classes.fuzzy[j][i]) > max ?  in.classes.fuzzy[j][i] : max);
00598               min  = (( in.classes.fuzzy[j][i]) <  min  ?  in.classes.fuzzy[j][i] : min);
00599             }
00600           //printf("max %f min %f\n",max,min);
00601           if(min == a && max == b)
00602             {
00603               printf("Normalisation : fin car deja normalise sur intervalle specifie [%1.2f,%1.2f]!\n",a,b);
00604               return NO_ERROR;
00605             }
00606 
00607           /* normaliser sur [0 1] puis sur [a b]*/
00608           for(i=0; i<in.nb_pts;i++)
00609             {
00610               /* normaliser sur [0 1] */
00611               out->classes.fuzzy[j][i] = ( in.classes.fuzzy[j][i] - min ) / ( max - min );
00612               
00613               if((a != 0 && b != 1) || (a != 1 && b != 0))
00614                 {
00615                   /* normaliser sur [a b] */
00616                   if(a < b)  out->classes.fuzzy[j][i] = out->classes.fuzzy[j][i] * (b - a) + a;
00617                   else out->classes.fuzzy[j][i] = out->classes.fuzzy[j][i] * (a - b) + b;
00618                 }
00619             }
00620         }
00621       
00622       break;
00623       
00624     default : ERREUR("Type problematique\n");
00625     }
00626   
00627   if(a<b)
00628     printf("\nNormalisation data output sur [%1.3f,%1.3f] faite.\n",a,b);
00629   if(a>b)
00630     printf("\nNormalisation data output sur [%1.3f,%1.3f] faite.\n",b,a);
00631 
00632   return NO_ERROR;
00633 }
00634  
00635 

Generated on Tue Apr 22 13:31:02 2008 for donnee1D by  doxygen 1.5.3