lmedhist.c

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 */
00036 #include <math.h>
00037 #include <sys/param.h>
00038 #include "image.h"
00039 #include "proto2D.h"
00040 #include <stdlib.h>
00041 #include <fcntl.h>
00042 #include "medhist.h"
00043 
00044 
00045 /* **********************  LECTURE DE PARAMETRES   *************************/
00057 param *medhist_lect(median_hist_t *des, param *ptp, char *debq){
00058   char question[500];
00059 
00060   sprintf(question, "%s taillefenetre (impaire)", debq); 
00061   lec_param(question, ptp);
00062   des->tf = atoi(ptp->rep);
00063   ptp = ptp->next; 
00064 
00065   if( (des->tf)%2 == 0){
00066       printf("\n>> ERREUR taillefenetre : dimensions incorrectes\n");
00067       exit(1);
00068   }
00069 
00070   return(ptp);
00071 }
00072 
00073 
00074 /* **********************  INITIALISATION  *****************************/
00085 int medhist_init(median_hist_t *des, imau1 im0, imau1 *imres){
00086   /* image resultat */ 
00087   imres->nc = im0.nc;
00088   imres->nr = im0.nr;
00089   sprintf(imres->nom, "%s(medhist %d)", im0.nom, des->tf);
00090   alloc_imau1(imres);
00091 
00092 
00093   return(0);  
00094 }
00095 
00096 
00097 
00098 /* **************************************************************************/
00099 /*                  premiere fenetre 2D - init. histogramme                    */
00100 /* **************************************************************************/
00101 void prem_fenetre_2d(unsigned char *ima, 
00102                      int i,
00103                      int *hist,
00104                      int th,
00105                      int *m,
00106                      int *n,
00107                      int tf,
00108                      int largeur)
00109 
00110 {
00111 register int   k, l ,tz;
00112 
00113 tz=(tf*tf)/2;
00114 for(k=0 ; k<=255 ; ++k) hist[k]=0;       /*initialisation du tableau hist[]*/
00115 
00116 for(k=i-th; k<=i+th ; ++k)
00117         for(l=0 ; l<tf    ; ++l)
00118                           hist[ima[k*largeur+l]]++;
00119                           
00120 /*----calculer M ---*/
00121 
00122 k=0;
00123 *n=hist[k];
00124 while (*n<=tz) 
00125         {
00126         k++;
00127         (*n)+=hist[k];
00128         }
00129 *m=k;
00130 
00131 /*--calculer N --*/
00132 (*n)-=hist[*m];
00133 }
00134 
00135 /* **************************************************************************/
00136 /*                                   traitement ligne                          */
00137 /* **************************************************************************/
00138 void traitligne_2d(unsigned char *ima,
00139                    int i,
00140                    int j,
00141                    int *col_gauche,
00142                    int *col_droite,
00143                    int tf,
00144                    int largeur)
00145 
00146 {
00147 register  int  k, d=i-(tf/2);
00148 int     th=tf/2;
00149 
00150 for(k=i-th ; k<=i+th ; ++k)
00151         {
00152         
00153         /*mettre colonne gauche de l'ancienne fenetre dans col_gauche[]*/
00154         
00155         *(col_gauche + k - d)= ima[k*largeur+j-th];
00156 
00157         /*mettre colonne droite de la nouvelle fenetre dans col_droite[]*/
00158         
00159         *(col_droite + k - d)= ima[k*largeur+j+th+1];
00160         }
00161  }
00162  
00163 
00164 
00165 
00166 /* **************************************************************************/
00167 /*                                  Calcul de M et N                       */
00168 /* **************************************************************************/
00169 void calculM_2d(int *n, 
00170                 int *m,
00171                 int th,
00172                 int hist[],
00173                 int *col_gauche,
00174                 int *col_droite,
00175                 int taillefenetre)
00176 
00177 {
00178 register int  k,g,tz;
00179 
00180 tz=(taillefenetre*taillefenetre)/2;
00181 for(k=0 ; k<taillefenetre ;  ++k )
00182    { 
00183      g=*(col_gauche+k);
00184      hist[g]--;           /* enlever la col. gauche de l'ancienne fenetre */
00185      if (g<*m)            /* et mettre a jour hist[] et N */
00186         (*n)--;
00187         
00188      g=*(col_droite+k);   
00189      hist[g]++;           /*ajouter la col. droite de la nouvelle fenetre*/
00190      if (g<*m)            /*et mettre a jour hist[] et N*/
00191          (*n)++;
00192    }
00193    
00194    /*calcul de M*/
00195    
00196 if (*n>tz)
00197         while(*n>tz) 
00198         {
00199         (*m)--;
00200         (*n)-=hist[*m];
00201         }
00202 else
00203         {
00204     *n +=hist[*m];
00205     while (*n<=tz)
00206         { 
00207         (*m)++;
00208         (*n)+=hist[*m];
00209         }
00210         (*n)-=hist[*m];
00211       }
00212 }
00213 
00214 
00215 /* **************************************************************************/
00216 /*                               MEDIAN STANDARD 2D                        */
00217 /* **************************************************************************/
00218 void mstd2d(unsigned char *ima_src,
00219             unsigned char *ima_dest,
00220             int largeur,
00221             int hauteur,
00222             int tf)
00223 {
00224 int     hist[256],*h;   /*histogramme*/
00225 int     M,*m ;          /*valeur  du median dans la fenetre*/
00226 int     N ,*n;          /*nbre de pixels ayant un niveau de gris < M */
00227 int     *col_gauche;    /*colonne gauche de la fenetre */
00228 int     *col_droite;    /*colonne droite de la fenetre */
00229 register int    i, j, th=tf/2;
00230 
00231 h=&hist[256];
00232 m=&M; n=&N;
00233 col_gauche=(int *)calloc(tf,sizeof(int));
00234 col_droite=(int *)calloc(tf,sizeof(int));
00235 for(i=th; i<hauteur-th; ++i)    /*pour chaque ligne i faire : */
00236         {
00237         prem_fenetre_2d(ima_src,i,hist,th,m,n,tf,largeur);
00238                            /*construire l'histogramme de la premiere fenetre*/
00239         ima_dest[i*largeur+th] = M;
00240         for(j=th  ; j<largeur-th-1;  ++j)       /*j:numero de colonne*/ 
00241                 {
00242                 traitligne_2d(ima_src,i,j,col_gauche,col_droite,tf,largeur);
00243                 calculM_2d(n,m,th,hist,col_gauche,col_droite,tf);
00244                 ima_dest[i*largeur+j+1]=M;      
00245                 }
00246         }
00247 cfree(col_gauche);
00248 cfree(col_droite);
00249 }
00250 
00251 
00252 
00253 /* *************************  CALCUL  ***************************/
00264 int medhist_calc(median_hist_t *des, imau1 im0, imau1 *imres){
00265   
00266   mstd2d( im0.p[0], imres->p[0], im0.nc , im0.nr, des->tf);
00267   return(0);
00268 }
00269 
00270 

Generated on Tue Apr 22 13:31:04 2008 for ima2D by  doxygen 1.5.3