ldefuzzyfication_voisinage_3x3.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 /*              ldefuzzyfication_voisinage_3x3.c                 */
00021 /* ************************************************************* */
00022 /*                                                               */
00023 /*      descripteurs et prototypes associés à l'opérateur        */
00024 /*                  defuzzyfication_voisinage_3x3                */
00025 /*                                                               */
00026 /*  Dernière modif : Ramasso E. & Jullien S.  08/12/03           */
00027 /*                                                               */
00028 /* ***************************************************************/
00029 
00035 #include "classifier.h"
00036 #include "defuzzyfication_voisinage_3x3.h"
00037 #include "image.h"
00038 #include "proto2D.h"
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 
00042 
00043 
00058 
00059 
00060 /* *************************  LECTURE  *******************************/
00073 param *defuzzyfication_voisinage_3x3_lect(defuzzyfication_voisinage_3x3_t *desc, param *ptp, char *debq)
00074 {
00075 
00076   /* Rien besoin de faire pour la lecture car par supposition, cet opérateur est relié à un classifieur */  
00077 
00078   return ptp;
00079 }
00080 
00081 
00082 
00083 
00084 
00085 /* *************************  INITIALISATION  ***************************/
00111 int defuzzyfication_voisinage_3x3_init(defuzzyfication_voisinage_3x3_t *desc, data_output dataOF, data_output *dataOC )
00112 {
00113   int i;
00114 
00115   printf("\nDefuzzyfication init...");
00116 
00117   /* on remplie les champs de la structure de sortie */
00118   desc->nb_classes = dataOF.nb_class;
00119   dataOC->nb_class = desc->nb_classes;
00120   dataOC->nb_pts = dataOF.nb_pts;
00121   dataOC->type = CRISP_CHOICE;
00122   dataOC->equ_nb_cols = dataOF.equ_nb_cols;
00123   dataOC->equ_nb_rows = dataOF.equ_nb_rows;
00124   
00125   /*on alloue une structure de sortie */
00126   alloc_classifier_data_output(dataOC); 
00127   
00128   /* allocation des variables */
00129   if((desc->somme_coeff_appartenance=(float*)malloc(desc->nb_classes*sizeof(float)))==NULL ||
00130      (desc->position_data_incertaines=(int*)malloc((dataOF.nb_pts)*sizeof(int)))==NULL)
00131     {
00132       printf("Erreur defuzz init memoire insuffisante\n");
00133       exit(1);
00134     }
00135 
00136   
00137   /* remplir desc->position_data_incertaines avec 0 car le malloc ne le fait pas */
00138   for(i=0;i<dataOF.nb_pts;i++)
00139       desc->position_data_incertaines[i] = 0;
00140 
00141   
00142   /* initialization nb. de pixels incertains */
00143   desc->nb_data_incertaines_finales = 0; 
00144   desc->nb_data_incertaines_initiales = 0; 
00145   
00146   printf("OK\n");
00147 
00148 }
00149 
00150 
00151 
00152 /* *************************  CALCUL  ***************************/
00172 int defuzzyfication_voisinage_3x3_calc(defuzzyfication_voisinage_3x3_t *desc, data_output dataOF, data_output *dataOC )
00173 {
00174 
00175   int i, j, x, y, indice, compteur, max_multiple;
00176   
00177   printf("\nDefuzzyfication calcul...");
00178 
00179   for(i=0;i<dataOF.nb_pts;i++)
00180     {
00181       desc->max_coeff_appartenance = 0;
00182       indice = 0;
00183       for(j=0;j<desc->nb_classes;j++) 
00184         if (dataOF.classes.fuzzy[j][i]>desc->max_coeff_appartenance) 
00185           {
00186             desc->max_coeff_appartenance=dataOF.classes.fuzzy[j][i];  
00187             /* trouve le coeff. d'appartenance desc->max_coeff_appartenance.  */
00188             indice=j;
00189           }
00190       
00191       max_multiple=0;
00192       for(j=0;j<desc->nb_classes;j++)
00193         if(dataOF.classes.fuzzy[j][i]==desc->max_coeff_appartenance) 
00194           (max_multiple)++; /* calculer le nb de desc->max_coeff_appartenance. */
00195       
00196       /* Les pixels du bord de l'image ne sont pas pris en compte dans la vérification dans la fenêtre 3x3 */
00197       if((i<dataOF.equ_nb_cols)||
00198          (i%(dataOF.equ_nb_cols) == 0)|| 
00199          ((i+1)%(dataOF.equ_nb_cols) == 0) || 
00200          (i>(dataOF.nb_pts - dataOF.equ_nb_cols))) 
00201         dataOC->classes.crisp[i] = indice;                
00202       
00203       else if (max_multiple == 1) /* vérification si il y a seulement un max */
00204         dataOC->classes.crisp[i] = indice; /* si oui -> defuzzyfication  */
00205       
00206       else 
00207         {
00208           /* si non il y en a plusieurs, sauvegarder la position de ce pixel incertain*/
00209           desc->position_data_incertaines[i] = 1; 
00210 
00211           /* incrementer nb. de pixels incertains */
00212           desc->nb_data_incertaines_initiales++; 
00213         }
00214     }
00215   
00216   printf("Nb. de données incertaines initiales = %d.",desc->nb_data_incertaines_initiales);
00217  
00218   if(desc->nb_data_incertaines_initiales == 0) 
00219     {
00220       printf("Donc pas besoin de fenêtre de vérification.\n");
00221       return 0;
00222     }
00223 
00224   else if(desc->nb_data_incertaines_initiales > 0)
00225     {
00226       printf(" Incorrect. \nNécessité d'un post traitement des données incertaines avec fenêtre 3x3...\n");
00227       
00228       /* traitement des pixels incertains  */ 
00229       desc->nb_data_incertaines_finales = 0;
00230       
00231       for(i=0;i<dataOF.nb_pts;i++)
00232         if (desc->position_data_incertaines[i] == 1)
00233           {
00234             for(j=0;j<desc->nb_classes;j++)
00235               {
00236                 desc->somme_coeff_appartenance[j]=0;
00237                 for(x=-1;x<2;x++)
00238                   for(y=-1;y<2;y++)
00239                     {
00240                       /* calculer les coefficients d'appartenence  */
00241                       desc->somme_coeff_appartenance[j] = desc->somme_coeff_appartenance[j]
00242                                                         + dataOF.classes.fuzzy[j][i+x*(dataOF.equ_nb_cols)+y]; 
00243                       /* moyenne, dans un voisinage 3x3 */
00244                     }
00245               }
00246             
00247             desc->max_coeff_appartenance = 0;
00248             indice = 0;
00249             
00250             for(j=0;j<desc->nb_classes;j++)
00251               {
00252                 if (desc->somme_coeff_appartenance[j] > desc->max_coeff_appartenance) 
00253                   {
00254                     desc->max_coeff_appartenance = desc->somme_coeff_appartenance[j];
00255                     indice=j;
00256                   }
00257               }
00258             
00259             compteur=0;
00260             
00261             for(j=0;j<desc->nb_classes;j++)
00262               if (desc->max_coeff_appartenance == desc->somme_coeff_appartenance[j]) compteur++;
00263             
00264             if (compteur > 1) desc->nb_data_incertaines_finales++;
00265             
00266             dataOC->classes.crisp[i] = indice;
00267           }
00268       
00269       if(desc->nb_data_incertaines_finales > 0) 
00270         printf("\nAprès la vérification avec la fenêtre 3x3 il y a encore %d incertitudes.\n",desc->nb_data_incertaines_finales);
00271       
00272       else printf("\nLa fenêtre 3x3 a supprimé les incertitudes\n");
00273       
00274       return 0;
00275     }
00276 
00277   else 
00278     {
00279       printf("Erreur defuzz calc : nb de data < 0\n");
00280       exit(1);
00281     }
00282 
00283   printf("OK.\n");
00284 
00285 }
00287 
00288 

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