00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00085 int medhist_init(median_hist_t *des, imau1 im0, imau1 *imres){
00086
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
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;
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
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
00132 (*n)-=hist[*m];
00133 }
00134
00135
00136
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
00154
00155 *(col_gauche + k - d)= ima[k*largeur+j-th];
00156
00157
00158
00159 *(col_droite + k - d)= ima[k*largeur+j+th+1];
00160 }
00161 }
00162
00163
00164
00165
00166
00167
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]--;
00185 if (g<*m)
00186 (*n)--;
00187
00188 g=*(col_droite+k);
00189 hist[g]++;
00190 if (g<*m)
00191 (*n)++;
00192 }
00193
00194
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
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;
00225 int M,*m ;
00226 int N ,*n;
00227 int *col_gauche;
00228 int *col_droite;
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)
00236 {
00237 prem_fenetre_2d(ima_src,i,hist,th,m,n,tf,largeur);
00238
00239 ima_dest[i*largeur+th] = M;
00240 for(j=th ; j<largeur-th-1; ++j)
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
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